summaryrefslogtreecommitdiffstats
path: root/tests/auto/concurrent/qtconcurrentfiltermapgenerated/generation_helpers.h
blob: 4ce901e34bd3ed3d109cf26a9b092149c8f5aa46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef QTBASE_GENERATION_HELPERS_H
#define QTBASE_GENERATION_HELPERS_H

#include "qglobal.h"

#include <vector>

struct tag_input
{
};
struct tag_mapped
{
};
struct tag_reduction
{
};

template<typename tag>
struct SequenceItem
{
    SequenceItem() = default;
    // bool as a stronger "explicit": should never be called inside of QtConcurrent
    SequenceItem(int val, bool) : value(val) { }

    bool operator==(const SequenceItem<tag> &other) const { return value == other.value; }
    bool isOdd() const { return value & 1; }
    void multiplyByTwo() { value *= 2; }

    int value = 0;
};

template<typename tag>
struct NoConstructSequenceItem
{
    NoConstructSequenceItem() = delete;
    // bool as a stronger "explicit": should never be called inside of QtConcurrent
    NoConstructSequenceItem(int val, bool) : value(val) { }

    bool operator==(const NoConstructSequenceItem<tag> &other) const
    {
        return value == other.value;
    }
    bool isOdd() const { return value & 1; }
    void multiplyByTwo() { value *= 2; }

    int value = 0;
};

template<typename tag>
struct MoveOnlySequenceItem
{
    MoveOnlySequenceItem() = default;
    ~MoveOnlySequenceItem() = default;
    MoveOnlySequenceItem(const MoveOnlySequenceItem &) = delete;
    MoveOnlySequenceItem(MoveOnlySequenceItem &&other) : value(other.value) { other.value = -1; }
    MoveOnlySequenceItem &operator=(const MoveOnlySequenceItem &) = delete;
    MoveOnlySequenceItem &operator=(MoveOnlySequenceItem &&other)
    {
        value = other.value;
        other.value = -1;
    }

    // bool as a stronger "explicit": should never be called inside of QtConcurrent
    MoveOnlySequenceItem(int val, bool) : value(val) { }

    bool operator==(const MoveOnlySequenceItem<tag> &other) const { return value == other.value; }
    bool isOdd() const { return value & 1; }
    void multiplyByTwo() { value *= 2; }

    int value = 0;
};

template<typename tag>
struct MoveOnlyNoConstructSequenceItem
{
    MoveOnlyNoConstructSequenceItem() = delete;
    ~MoveOnlyNoConstructSequenceItem() = default;
    MoveOnlyNoConstructSequenceItem(const MoveOnlyNoConstructSequenceItem &) = delete;
    MoveOnlyNoConstructSequenceItem(MoveOnlyNoConstructSequenceItem &&other) : value(other.value)
    {
        other.value = -1;
    }
    MoveOnlyNoConstructSequenceItem &operator=(const MoveOnlyNoConstructSequenceItem &) = delete;
    MoveOnlyNoConstructSequenceItem &operator=(MoveOnlyNoConstructSequenceItem &&other)
    {
        value = other.value;
        other.value = -1;
    }

    // bool as a stronger "explicit": should never be called inside of QtConcurrent
    MoveOnlyNoConstructSequenceItem(int val, bool) : value(val) { }

    bool operator==(const MoveOnlyNoConstructSequenceItem<tag> &other) const
    {
        return value == other.value;
    }
    bool isOdd() const { return value & 1; }
    void multiplyByTwo() { value *= 2; }

    int value = 0;
};

template<typename T>
bool myfilter(const T &el)
{
    return el.isOdd();
}

template<typename T>
class MyFilter
{
public:
    bool operator()(const T &el) { return el.isOdd(); }
};

template<typename T>
class MyMoveOnlyFilter
{
    bool movedFrom = false;

public:
    MyMoveOnlyFilter() = default;
    MyMoveOnlyFilter(const MyMoveOnlyFilter<T> &) = delete;
    MyMoveOnlyFilter &operator=(const MyMoveOnlyFilter<T> &) = delete;

    MyMoveOnlyFilter(MyMoveOnlyFilter<T> &&other) { other.movedFrom = true; }
    MyMoveOnlyFilter &operator=(MyMoveOnlyFilter<T> &&other) { other.movedFrom = true; }

    bool operator()(const T &el)
    {
        if (!movedFrom)
            return el.isOdd();
        else
            return -1;
    }
};

template<typename From, typename To>
To myMap(const From &f)
{
    return To(f.value * 2, true);
}

template<typename T>
void myInplaceMap(T &el)
{
    el.multiplyByTwo();
}

template<typename From, typename To>
class MyMap
{
public:
    To operator()(const From &el) { return To(el.value * 2, true); }
};

template<typename T>
class MyInplaceMap
{
public:
    void operator()(T &el) { el.multiplyByTwo(); }
};

template<typename From, typename To>
class MyMoveOnlyMap
{
    bool movedFrom = false;

public:
    MyMoveOnlyMap() = default;
    MyMoveOnlyMap(const MyMoveOnlyMap<From, To> &) = delete;
    MyMoveOnlyMap &operator=(const MyMoveOnlyMap<From, To> &) = delete;

    MyMoveOnlyMap(MyMoveOnlyMap<From, To> &&other) { other.movedFrom = true; }
    MyMoveOnlyMap &operator=(MyMoveOnlyMap<From, To> &&other) { other.movedFrom = true; }

    To operator()(const From &el)
    {
        if (!movedFrom)
            return To(el.value * 2, true);
        else
            return To(-1, true);
    }
};

template<typename T>
class MyMoveOnlyInplaceMap
{
    bool movedFrom = false;

public:
    MyMoveOnlyInplaceMap() = default;
    MyMoveOnlyInplaceMap(const MyMoveOnlyInplaceMap<T> &) = delete;
    MyMoveOnlyInplaceMap &operator=(const MyMoveOnlyInplaceMap<T> &) = delete;

    MyMoveOnlyInplaceMap(MyMoveOnlyInplaceMap<T> &&other) { other.movedFrom = true; }
    MyMoveOnlyInplaceMap &operator=(MyMoveOnlyInplaceMap<T> &&other) { other.movedFrom = true; }

    void operator()(T &el)
    {
        if (!movedFrom)
            el.multiplyByTwo();
    }
};

template<typename From, typename To>
void myReduce(To &sum, const From &val)
{
    sum.value += val.value;
}

template<typename From, typename To>
class MyReduce
{
public:
    void operator()(To &sum, const From &val) { sum.value += val.value; }
};

template<typename From, typename To>
class MyMoveOnlyReduce
{
    bool movedFrom = false;

public:
    MyMoveOnlyReduce() = default;
    MyMoveOnlyReduce(const MyMoveOnlyReduce<From, To> &) = delete;
    MyMoveOnlyReduce &operator=(const MyMoveOnlyReduce<From, To> &) = delete;

    MyMoveOnlyReduce(MyMoveOnlyReduce<From, To> &&other) { other.movedFrom = true; }
    MyMoveOnlyReduce &operator=(MyMoveOnlyReduce<From, To> &&other) { other.movedFrom = true; }

    void operator()(To &sum, const From &val)
    {
        if (!movedFrom)
            sum.value += val.value;
    }
};

QT_BEGIN_NAMESPACE

// pretty printing
template<typename tag>
char *toString(const SequenceItem<tag> &i)
{
    using QTest::toString;
    return toString(QString::number(i.value));
}

// pretty printing
template<typename T>
char *toString(const std::vector<T> &vec)
{
    using QTest::toString;
    QString result("");
    for (const auto &i : vec) {
        result.append(QString::number(i.value) + ", ");
    }
    if (result.size())
        result.chop(2);
    return toString(result);
}

QT_END_NAMESPACE

#endif // QTBASE_GENERATION_HELPERS_H