any_helpers.h
11.1 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef ANY_HELPERS_H
#define ANY_HELPERS_H
#include <typeinfo>
#include <type_traits>
#include <cassert>
namespace std { namespace experimental {} }
#include "test_macros.h"
#include "type_id.h"
#if !defined(TEST_HAS_NO_RTTI)
#define RTTI_ASSERT(X) assert(X)
#else
#define RTTI_ASSERT(X)
#endif
template <class T>
struct IsSmallObject
: public std::integral_constant<bool
, sizeof(T) <= sizeof(std::any) - sizeof(void*)
&& std::alignment_of<void*>::value
% std::alignment_of<T>::value == 0
&& std::is_nothrow_move_constructible<T>::value
>
{};
template <class T>
bool containsType(std::any const& a) {
#if !defined(TEST_HAS_NO_RTTI)
return a.type() == typeid(T);
#else
return a.has_value() && std::any_cast<T>(&a) != nullptr;
#endif
}
// Return 'true' if 'Type' will be considered a small type by 'any'
template <class Type>
bool isSmallType() {
return IsSmallObject<Type>::value;
}
// Assert that an object is empty. If the object used to contain an object
// of type 'LastType' check that it can no longer be accessed.
template <class LastType = int>
void assertEmpty(std::any const& a) {
using namespace std;
assert(!a.has_value());
RTTI_ASSERT(a.type() == typeid(void));
assert(any_cast<LastType const>(&a) == nullptr);
}
template <class Type>
constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)
{ return true; }
template <class> constexpr bool has_value_member(long) { return false; }
// Assert that an 'any' object stores the specified 'Type' and 'value'.
template <class Type>
std::enable_if_t<has_value_member<Type>(0)>
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
assertContains(std::any const& a, int value) {
assert(a.has_value());
assert(containsType<Type>(a));
assert(std::any_cast<Type const &>(a).value == value);
}
template <class Type, class Value>
std::enable_if_t<!has_value_member<Type>(0)>
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
assertContains(std::any const& a, Value value) {
assert(a.has_value());
assert(containsType<Type>(a));
assert(std::any_cast<Type const &>(a) == value);
}
// Modify the value of a "test type" stored within an any to the specified
// 'value'.
template <class Type>
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
void modifyValue(std::any& a, int value) {
using namespace std;
using namespace std::experimental;
assert(a.has_value());
assert(containsType<Type>(a));
any_cast<Type&>(a).value = value;
}
// A test type that will trigger the small object optimization within 'any'.
template <int Dummy = 0>
struct small_type
{
static int count;
static int copied;
static int moved;
static int const_copied;
static int non_const_copied;
static void reset() {
small_type::copied = 0;
small_type::moved = 0;
small_type::const_copied = 0;
small_type::non_const_copied = 0;
}
int value;
explicit small_type(int val = 0) : value(val) {
++count;
}
explicit small_type(int, int val, int) : value(val) {
++count;
}
small_type(std::initializer_list<int> il) : value(*il.begin()) {
++count;
}
small_type(small_type const & other) noexcept {
value = other.value;
++count;
++copied;
++const_copied;
}
small_type(small_type& other) noexcept {
value = other.value;
++count;
++copied;
++non_const_copied;
}
small_type(small_type && other) noexcept {
value = other.value;
other.value = 0;
++count;
++moved;
}
~small_type() {
value = -1;
--count;
}
private:
small_type& operator=(small_type const&) = delete;
small_type& operator=(small_type&&) = delete;
};
template <int Dummy>
int small_type<Dummy>::count = 0;
template <int Dummy>
int small_type<Dummy>::copied = 0;
template <int Dummy>
int small_type<Dummy>::moved = 0;
template <int Dummy>
int small_type<Dummy>::const_copied = 0;
template <int Dummy>
int small_type<Dummy>::non_const_copied = 0;
typedef small_type<> small;
typedef small_type<1> small1;
typedef small_type<2> small2;
// A test type that will NOT trigger the small object optimization in any.
template <int Dummy = 0>
struct large_type
{
static int count;
static int copied;
static int moved;
static int const_copied;
static int non_const_copied;
static void reset() {
large_type::copied = 0;
large_type::moved = 0;
large_type::const_copied = 0;
large_type::non_const_copied = 0;
}
int value;
large_type(int val = 0) : value(val) {
++count;
data[0] = 0;
}
large_type(int, int val, int) : value(val) {
++count;
data[0] = 0;
}
large_type(std::initializer_list<int> il) : value(*il.begin()) {
++count;
}
large_type(large_type const & other) {
value = other.value;
++count;
++copied;
++const_copied;
}
large_type(large_type & other) {
value = other.value;
++count;
++copied;
++non_const_copied;
}
large_type(large_type && other) {
value = other.value;
other.value = 0;
++count;
++moved;
}
~large_type() {
value = 0;
--count;
}
private:
large_type& operator=(large_type const&) = delete;
large_type& operator=(large_type &&) = delete;
int data[10];
};
template <int Dummy>
int large_type<Dummy>::count = 0;
template <int Dummy>
int large_type<Dummy>::copied = 0;
template <int Dummy>
int large_type<Dummy>::moved = 0;
template <int Dummy>
int large_type<Dummy>::const_copied = 0;
template <int Dummy>
int large_type<Dummy>::non_const_copied = 0;
typedef large_type<> large;
typedef large_type<1> large1;
typedef large_type<2> large2;
// The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
// and 'throws_on_move'.
struct my_any_exception {};
void throwMyAnyExpression() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
throw my_any_exception();
#else
assert(false && "Exceptions are disabled");
#endif
}
// A test type that will trigger the small object optimization within 'any'.
// this type throws if it is copied.
struct small_throws_on_copy
{
static int count;
static int copied;
static int moved;
static void reset() { count = copied = moved = 0; }
int value;
explicit small_throws_on_copy(int val = 0) : value(val) {
++count;
}
explicit small_throws_on_copy(int, int val, int) : value(val) {
++count;
}
small_throws_on_copy(small_throws_on_copy const &) {
throwMyAnyExpression();
}
small_throws_on_copy(small_throws_on_copy && other) throw() {
value = other.value;
++count; ++moved;
}
~small_throws_on_copy() {
--count;
}
private:
small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
};
int small_throws_on_copy::count = 0;
int small_throws_on_copy::copied = 0;
int small_throws_on_copy::moved = 0;
// A test type that will NOT trigger the small object optimization within 'any'.
// this type throws if it is copied.
struct large_throws_on_copy
{
static int count;
static int copied;
static int moved;
static void reset() { count = copied = moved = 0; }
int value = 0;
explicit large_throws_on_copy(int val = 0) : value(val) {
data[0] = 0;
++count;
}
explicit large_throws_on_copy(int, int val, int) : value(val) {
data[0] = 0;
++count;
}
large_throws_on_copy(large_throws_on_copy const &) {
throwMyAnyExpression();
}
large_throws_on_copy(large_throws_on_copy && other) throw() {
value = other.value;
++count; ++moved;
}
~large_throws_on_copy() {
--count;
}
private:
large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
int data[10];
};
int large_throws_on_copy::count = 0;
int large_throws_on_copy::copied = 0;
int large_throws_on_copy::moved = 0;
// A test type that throws when it is moved. This object will NOT trigger
// the small object optimization in 'any'.
struct throws_on_move
{
static int count;
static int copied;
static int moved;
static void reset() { count = copied = moved = 0; }
int value;
explicit throws_on_move(int val = 0) : value(val) { ++count; }
explicit throws_on_move(int, int val, int) : value(val) { ++count; }
throws_on_move(throws_on_move const & other) {
value = other.value;
++count; ++copied;
}
throws_on_move(throws_on_move &&) {
throwMyAnyExpression();
}
~throws_on_move() {
--count;
}
private:
throws_on_move& operator=(throws_on_move const&) = delete;
throws_on_move& operator=(throws_on_move &&) = delete;
};
int throws_on_move::count = 0;
int throws_on_move::copied = 0;
int throws_on_move::moved = 0;
struct small_tracked_t {
small_tracked_t()
: arg_types(&makeArgumentID<>()) {}
small_tracked_t(small_tracked_t const&) noexcept
: arg_types(&makeArgumentID<small_tracked_t const&>()) {}
small_tracked_t(small_tracked_t &&) noexcept
: arg_types(&makeArgumentID<small_tracked_t &&>()) {}
template <class ...Args>
explicit small_tracked_t(Args&&...)
: arg_types(&makeArgumentID<Args...>()) {}
template <class ...Args>
explicit small_tracked_t(std::initializer_list<int>, Args&&...)
: arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
TypeID const* arg_types;
};
static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
struct large_tracked_t {
large_tracked_t()
: arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
large_tracked_t(large_tracked_t const&) noexcept
: arg_types(&makeArgumentID<large_tracked_t const&>()) {}
large_tracked_t(large_tracked_t &&) noexcept
: arg_types(&makeArgumentID<large_tracked_t &&>()) {}
template <class ...Args>
explicit large_tracked_t(Args&&...)
: arg_types(&makeArgumentID<Args...>()) {}
template <class ...Args>
explicit large_tracked_t(std::initializer_list<int>, Args&&...)
: arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
TypeID const* arg_types;
int dummy[sizeof(std::any) / sizeof(int) + 1];
};
static_assert(!IsSmallObject<large_tracked_t>::value, "must not be small");
template <class Type, class ...Args>
void assertArgsMatch(std::any const& a) {
using namespace std;
using namespace std::experimental;
assert(a.has_value());
assert(containsType<Type>(a));
assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());
};
#endif