File: helper_classes.hpp

package info (click to toggle)
libpmemobj-cpp 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,388 kB
  • sloc: cpp: 136,076; sh: 1,022; perl: 381; ansic: 163; makefile: 13
file content (366 lines) | stat: -rw-r--r-- 7,398 bytes parent folder | download
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2019, Intel Corporation */

/**
 * Helper classes that represent C++ concepts
 */

#ifndef HELPER_CLASSES_HPP
#define HELPER_CLASSES_HPP

#include "unittest.hpp"

#include <utility>

/**
 * default_constructible_only - helper class
 * Instance of that type can be only default constructed
 */

class default_constructible_only {
public:
	static int count;

	default_constructible_only() : _val(1)
	{
		++count;
	}

	~default_constructible_only()
	{
		--count;
	};

	default_constructible_only(const default_constructible_only &) = delete;

	default_constructible_only &
	operator=(const default_constructible_only &) = delete;

	bool
	operator==(const default_constructible_only &other) const
	{
		return _val == other._val;
	}

private:
	int _val;
};

int default_constructible_only::count = 0;

/**
 * copy_assignable_copy_insertable
 * Instance of that type satisfies requirements of CopyAssignable and
 * CopyInsertable concepts.
 */
template <typename T>
struct copy_assignable_copy_insertable {
	T value;
	int copied = 0;
	int copied_assigned = 0;

	/* emplace ctor is needed to create first object */
	copy_assignable_copy_insertable(const T &val) : value(val){};

	copy_assignable_copy_insertable(
		const copy_assignable_copy_insertable &other)
	    : value(other.value), copied(other.copied + 1){};

	copy_assignable_copy_insertable &
	operator=(const copy_assignable_copy_insertable &other)
	{
		copied = other.copied;
		copied_assigned = other.copied_assigned + 1;
		value = other.value;
		return *this;
	}
};

/**
 * emplace_constructible - helper class
 * Instance of that type can be constructed in uninitialized storage
 */
template <typename T>
struct emplace_constructible {
	T value;

	emplace_constructible(T val) : value(val)
	{
	}

	emplace_constructible(const emplace_constructible &) = delete;
};

/**
 * emplace_constructible_and_move_insertable - helper class
 * Satisfies requirements:
 * - instance of that type can be constructed in uninitialized storage
 * - rvalue of the type can be copied in uninitialized storage
 */
template <typename T>
struct emplace_constructible_and_move_insertable {
	T value;
	int moved = 0;

	emplace_constructible_and_move_insertable(T val) : value(val)
	{
	}

	emplace_constructible_and_move_insertable(
		emplace_constructible_and_move_insertable &&other)
	    : value(other.value), moved(other.moved + 1)
	{
	}

	/* Since move constructor is user-declared, copy constructor and copy
	 * assignment operator are not implicitly declared by compiler */
};

/**
 * emplace_constructible_copy_insertable_and_move_insertable - helper class
 * Satisfies requirements:
 * - instance of that type can be copy-constructed in uninitialized storage
 * - rvalue of the type can be copied in uninitialized storage
 */
template <typename T>
struct emplace_constructible_copy_insertable_move_insertable {
	T value;
	int copied = 0;
	int moved = 0;

	emplace_constructible_copy_insertable_move_insertable(T val)
	    : value(val)
	{
	}

	emplace_constructible_copy_insertable_move_insertable(
		const emplace_constructible_copy_insertable_move_insertable
			&other)
	    : value(other.value), copied(other.copied + 1)
	{
	}

	emplace_constructible_copy_insertable_move_insertable(
		emplace_constructible_copy_insertable_move_insertable &&other)
	    : value(other.value), moved(other.moved + 1)
	{
	}
};

/**
 *  emplace_constructible_moveable_and_assignable - helper class
 *  Satisfies requirements:
 *  - instance of that type can be constructed in uninitialized storage
 *  - instance of the type can be constructed from an rvalue argument
 *  - instance of the type can be copy-assigned from an lvalue expression
 */
template <typename T>
struct emplace_constructible_moveable_and_assignable {
	T value;
	int moved = 0;
	int assigned = 0;

	emplace_constructible_moveable_and_assignable(T val) : value(val)
	{
	}

	emplace_constructible_moveable_and_assignable(
		emplace_constructible_moveable_and_assignable &&other)
	    : value(std::move(other.value)), moved(other.moved + 1)
	{
	}

	emplace_constructible_moveable_and_assignable &
	operator=(emplace_constructible_moveable_and_assignable &&other)
	{
		moved = other.moved;
		assigned = other.assigned + 1;
		value = std::move(other.value);
		return *this;
	}

	emplace_constructible_moveable_and_assignable &
	operator=(T val)
	{
		value = std::move(val);
		++assigned;
		return *this;
	}
};

/**
 * failing_reference_operator - helper structure
 * Instance of that type cannot use reference operator
 */
struct failing_reference_operator {
	failing_reference_operator() : val(0)
	{
	}

	failing_reference_operator(int i) : val(i)
	{
	}

	~failing_reference_operator()
	{
	}

	failing_reference_operator *operator&() const
	{
		UT_ASSERT(0);
		return nullptr;
	}

	int val;
};

/**
 *  move_only - helper class
 *  Instance of that type can be constructed from an rvalue argument only
 */
struct move_only {
	int value;

	move_only(int val = 1) : value(val)
	{
	}

	move_only(const move_only &) = delete;

	move_only &operator=(const move_only &) = delete;

	move_only &
	operator=(move_only &&other)
	{
		value = other.value;
		other.value = 0;
		return *this;
	}

	move_only(move_only &&other) : value(other.value)
	{
		other.value = 0;
	}

	bool
	operator==(const move_only &other) const
	{
		return value == other.value;
	}
};

/**
 *  move_assignable - helper class
 *  Instance of that type satisfies MoveAssignable concept requirements.
 */
struct move_assignable {
	int value;

	/* emplace ctor is needed to create first object */
	move_assignable(int val = 0) : value(val)
	{
	}

	move_assignable &
	operator=(move_assignable &&other)
	{
		value = other.value;
		other.value = 0;
		return *this;
	}
};

/**
 *  copy_insertable - helper class
 *  Instance of that type satisfies CopyInsertable concept requirements.
 */
struct copy_insertable {
	int value;

	/* emplace ctor is needed to create first object */
	copy_insertable(int val) : value(val)
	{
	}

	copy_insertable(const copy_insertable &other) : value(other.value)
	{
	}
};

/**
 *  move_insertable - helper class
 *  Instance of that type satisfies MoveInsertable concept requirements.
 */
struct move_insertable {
	int value;

	/* emplace ctor is needed to create first object */
	move_insertable(int val) : value(val)
	{
	}

	move_insertable(const copy_insertable &&other) : value(other.value)
	{
	}
};

struct CompoundType {
	int counter = 0;

	/* If counter holds this value it means object was
	 * initialized */
	static constexpr int INITIALIZED = 999999999;

	CompoundType(int c)
	{
		UT_ASSERT(counter != INITIALIZED);
		counter = c;
	}

	CompoundType()
	{
		UT_ASSERT(counter != INITIALIZED);
		counter = INITIALIZED;
	}

	CompoundType(CompoundType &&rhs)
	{
		UT_ASSERT(counter != INITIALIZED);
		counter = rhs.counter;
	}

	CompoundType(const CompoundType &rhs)
	{
		UT_ASSERT(counter != INITIALIZED);
		counter = rhs.counter;
	}

	~CompoundType()
	{
		counter = 0;
	}

	CompoundType &
	operator=(CompoundType &&rhs)
	{
		UT_ASSERT(counter == INITIALIZED);
		counter = rhs.counter;
		return *this;
	}

	CompoundType &
	operator=(const CompoundType &rhs)
	{
		UT_ASSERT(counter == INITIALIZED);
		counter = rhs.counter;
		return *this;
	}

	bool
	operator==(const CompoundType &rhs)
	{
		return counter == rhs.counter;
	}
};

#endif /* HELPER_CLASSES_HPP */