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