LLVM OpenMP
iterfor.cpp
Go to the documentation of this file.
1// RUN: %libomp-cxx20-compile -fopenmp-version=60 && %libomp-run \
2// RUN: | FileCheck %s --match-full-lines
3
4#include <cstdlib>
5#include <cstdarg>
6#include <cstdio>
7
8struct Reporter {
9 const char *name;
10
11 Reporter(const char *name) : name(name) { print("ctor"); }
12
13 Reporter() : name("<anon>") { print("ctor"); }
14
15 Reporter(const Reporter &that) : name(that.name) { print("copy ctor"); }
16
17 Reporter(Reporter &&that) : name(that.name) { print("move ctor"); }
18
19 ~Reporter() { print("dtor"); }
20
21 const Reporter &operator=(const Reporter &that) {
22 print("copy assign");
23 this->name = that.name;
24 return *this;
25 }
26
27 const Reporter &operator=(Reporter &&that) {
28 print("move assign");
29 this->name = that.name;
30 return *this;
31 }
32
33 struct Iterator {
34 const Reporter *owner;
35 int pos;
36
37 Iterator(const Reporter *owner, int pos) : owner(owner), pos(pos) {}
38
39 Iterator(const Iterator &that) : owner(that.owner), pos(that.pos) {
40 owner->print("iterator copy ctor");
41 }
42
43 Iterator(Iterator &&that) : owner(that.owner), pos(that.pos) {
44 owner->print("iterator move ctor");
45 }
46
47 ~Iterator() { owner->print("iterator dtor"); }
48
49 const Iterator &operator=(const Iterator &that) {
50 owner->print("iterator copy assign");
51 this->owner = that.owner;
52 this->pos = that.pos;
53 return *this;
54 }
55
56 const Iterator &operator=(Iterator &&that) {
57 owner->print("iterator move assign");
58 this->owner = that.owner;
59 this->pos = that.pos;
60 return *this;
61 }
62
63 bool operator==(const Iterator &that) const {
64 owner->print("iterator %d == %d", this->pos, that.pos);
65 return this->pos == that.pos;
66 }
67
68 bool operator!=(const Iterator &that) const {
69 owner->print("iterator %d != %d", this->pos, that.pos);
70 return this->pos != that.pos;
71 }
72
74 owner->print("iterator prefix ++");
75 pos += 1;
76 return *this;
77 }
78
80 owner->print("iterator postfix ++");
81 auto result = *this;
82 pos += 1;
83 return result;
84 }
85
86 int operator*() const {
87 owner->print("iterator deref: %d", pos);
88 return pos;
89 }
90
91 size_t operator-(const Iterator &that) const {
92 int result = this->pos - that.pos;
93 owner->print("iterator distance: %d", result);
94 return result;
95 }
96
97 Iterator operator+(int steps) const {
98 owner->print("iterator advance: %d += %d", this->pos, steps);
99 return Iterator(owner, pos + steps);
100 }
101 };
102
103 Iterator begin() const {
104 print("begin()");
105 return Iterator(this, 0);
106 }
107
108 Iterator end() const {
109 print("end()");
110 return Iterator(this, 4);
111 }
112
113 void print(const char *msg, ...) const {
114 va_list args;
115 va_start(args, msg);
116 printf("[%s] ", name);
117 vprintf(msg, args);
118 printf("\n");
119 va_end(args);
120 }
121};
122
123int main() {
124 printf("do\n");
125 Reporter range("range");
126#pragma omp split counts(1, omp_fill, 1)
127 for (auto it = range.begin(); it != range.end(); ++it)
128 printf("v=%d\n", *it);
129 printf("done\n");
130 return EXIT_SUCCESS;
131}
132
133// CHECK: do
134// CHECK: [range] ctor
135// CHECK: v=0
136// CHECK: v=1
137// CHECK: v=2
138// CHECK: v=3
139// CHECK: done
140// CHECK: [range] dtor
int result[2]
int main()
Definition iterfor.cpp:127
#define args
const Reporter * owner
Definition foreach.cpp:37
Iterator & operator++()
Definition iterfor.cpp:73
int operator*() const
Definition iterfor.cpp:86
const Iterator & operator=(const Iterator &that)
Definition iterfor.cpp:49
bool operator==(const Iterator &that) const
Definition iterfor.cpp:63
Iterator operator+(int steps) const
Definition iterfor.cpp:97
Iterator(const Iterator &that)
Definition iterfor.cpp:39
Iterator(const Reporter *owner, int pos)
Definition iterfor.cpp:37
Iterator operator++(int)
Definition iterfor.cpp:79
bool operator!=(const Iterator &that) const
Definition iterfor.cpp:68
size_t operator-(const Iterator &that) const
Definition iterfor.cpp:91
Iterator(Iterator &&that)
Definition iterfor.cpp:43
const Iterator & operator=(Iterator &&that)
Definition iterfor.cpp:56
void print(const char *msg,...) const
Definition foreach.cpp:114
Reporter(Reporter &&that)
Definition iterfor.cpp:17
const Reporter & operator=(const Reporter &that)
Definition iterfor.cpp:21
Iterator begin() const
Definition iterfor.cpp:103
const Reporter & operator=(Reporter &&that)
Definition iterfor.cpp:27
Iterator end() const
Definition iterfor.cpp:108
const char * name
Definition foreach.cpp:12
Reporter(const char *name)
Definition iterfor.cpp:11
~Reporter()
Definition iterfor.cpp:19
Reporter(const Reporter &that)
Definition iterfor.cpp:15