blob: afc85c97ff46d462138260603fcc4398e97ba9a3 [file] [log] [blame]
Eric Fiselierfa1e5db2016-01-19 21:52:04 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eric Fiselier7175a072015-07-31 02:24:58 +00009#ifndef ANY_HELPERS_H
10#define ANY_HELPERS_H
11
Eric Fiselier7175a072015-07-31 02:24:58 +000012#include <typeinfo>
13#include <type_traits>
14#include <cassert>
15
Eric Fiseliere739d542016-08-11 03:13:11 +000016namespace std { namespace experimental {} }
17
Eric Fiselier7175a072015-07-31 02:24:58 +000018#include "test_macros.h"
Eric Fiseliere739d542016-08-11 03:13:11 +000019#include "type_id.h"
Eric Fiselier7175a072015-07-31 02:24:58 +000020
21#if !defined(TEST_HAS_NO_RTTI)
22#define RTTI_ASSERT(X) assert(X)
23#else
24#define RTTI_ASSERT(X)
25#endif
26
Stephan T. Lavavej302557b2017-08-11 20:53:53 +000027template <class T>
Eric Fiselier7175a072015-07-31 02:24:58 +000028 struct IsSmallObject
29 : public std::integral_constant<bool
Stephan T. Lavavej302557b2017-08-11 20:53:53 +000030 , sizeof(T) <= (sizeof(void*)*3)
Eric Fiselier7175a072015-07-31 02:24:58 +000031 && std::alignment_of<void*>::value
Stephan T. Lavavej302557b2017-08-11 20:53:53 +000032 % std::alignment_of<T>::value == 0
33 && std::is_nothrow_move_constructible<T>::value
Eric Fiselier7175a072015-07-31 02:24:58 +000034 >
35 {};
36
Eric Fiseliere739d542016-08-11 03:13:11 +000037template <class T>
38bool containsType(std::any const& a) {
39#if !defined(TEST_HAS_NO_RTTI)
40 return a.type() == typeid(T);
41#else
42 return a.has_value() && std::any_cast<T>(&a) != nullptr;
43#endif
44}
Eric Fiselier7175a072015-07-31 02:24:58 +000045
46// Return 'true' if 'Type' will be considered a small type by 'any'
47template <class Type>
48bool isSmallType() {
Eric Fiselier7175a072015-07-31 02:24:58 +000049 return IsSmallObject<Type>::value;
Eric Fiselier7175a072015-07-31 02:24:58 +000050}
51
52// Assert that an object is empty. If the object used to contain an object
53// of type 'LastType' check that it can no longer be accessed.
54template <class LastType = int>
Eric Fiseliere739d542016-08-11 03:13:11 +000055void assertEmpty(std::any const& a) {
56 using namespace std;
57 assert(!a.has_value());
Eric Fiselier7175a072015-07-31 02:24:58 +000058 RTTI_ASSERT(a.type() == typeid(void));
Eric Fiseliere739d542016-08-11 03:13:11 +000059 assert(any_cast<LastType const>(&a) == nullptr);
Eric Fiselier7175a072015-07-31 02:24:58 +000060}
61
Eric Fiselier846edfb2016-10-16 02:51:50 +000062template <class Type>
63constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)
64{ return true; }
65template <class> constexpr bool has_value_member(long) { return false; }
66
67
Eric Fiselier7175a072015-07-31 02:24:58 +000068// Assert that an 'any' object stores the specified 'Type' and 'value'.
69template <class Type>
Eric Fiselier846edfb2016-10-16 02:51:50 +000070std::enable_if_t<has_value_member<Type>(0)>
71assertContains(std::any const& a, int value) {
Eric Fiseliere739d542016-08-11 03:13:11 +000072 assert(a.has_value());
73 assert(containsType<Type>(a));
74 assert(std::any_cast<Type const &>(a).value == value);
75}
76
Eric Fiselier846edfb2016-10-16 02:51:50 +000077template <class Type, class Value>
78std::enable_if_t<!has_value_member<Type>(0)>
79assertContains(std::any const& a, Value value) {
Eric Fiseliere739d542016-08-11 03:13:11 +000080 assert(a.has_value());
Eric Fiselier846edfb2016-10-16 02:51:50 +000081 assert(containsType<Type>(a));
82 assert(std::any_cast<Type const &>(a) == value);
Eric Fiselier7175a072015-07-31 02:24:58 +000083}
84
Eric Fiselier846edfb2016-10-16 02:51:50 +000085
Eric Fiselier7175a072015-07-31 02:24:58 +000086// Modify the value of a "test type" stored within an any to the specified
87// 'value'.
88template <class Type>
Eric Fiseliere739d542016-08-11 03:13:11 +000089void modifyValue(std::any& a, int value) {
90 using namespace std;
91 using namespace std::experimental;
92 assert(a.has_value());
93 assert(containsType<Type>(a));
94 any_cast<Type&>(a).value = value;
Eric Fiselier7175a072015-07-31 02:24:58 +000095}
96
97// A test type that will trigger the small object optimization within 'any'.
98template <int Dummy = 0>
99struct small_type
100{
101 static int count;
102 static int copied;
103 static int moved;
104 static int const_copied;
105 static int non_const_copied;
106
107 static void reset() {
108 small_type::copied = 0;
109 small_type::moved = 0;
110 small_type::const_copied = 0;
111 small_type::non_const_copied = 0;
112 }
113
114 int value;
115
Eric Fiseliere739d542016-08-11 03:13:11 +0000116 explicit small_type(int val = 0) : value(val) {
117 ++count;
118 }
119 explicit small_type(int, int val, int) : value(val) {
120 ++count;
121 }
122 small_type(std::initializer_list<int> il) : value(*il.begin()) {
Eric Fiselier7175a072015-07-31 02:24:58 +0000123 ++count;
124 }
125
Eric Fiseliere739d542016-08-11 03:13:11 +0000126 small_type(small_type const & other) noexcept {
Eric Fiselier7175a072015-07-31 02:24:58 +0000127 value = other.value;
128 ++count;
129 ++copied;
130 ++const_copied;
131 }
132
Eric Fiseliere739d542016-08-11 03:13:11 +0000133 small_type(small_type& other) noexcept {
Eric Fiselier7175a072015-07-31 02:24:58 +0000134 value = other.value;
135 ++count;
136 ++copied;
137 ++non_const_copied;
138 }
139
Eric Fiseliere739d542016-08-11 03:13:11 +0000140 small_type(small_type && other) noexcept {
Eric Fiselier7175a072015-07-31 02:24:58 +0000141 value = other.value;
142 other.value = 0;
143 ++count;
144 ++moved;
145 }
146
147 ~small_type() {
148 value = -1;
149 --count;
150 }
151
152private:
153 small_type& operator=(small_type const&) = delete;
154 small_type& operator=(small_type&&) = delete;
155};
156
157template <int Dummy>
158int small_type<Dummy>::count = 0;
159
160template <int Dummy>
161int small_type<Dummy>::copied = 0;
162
163template <int Dummy>
164int small_type<Dummy>::moved = 0;
165
166template <int Dummy>
167int small_type<Dummy>::const_copied = 0;
168
169template <int Dummy>
170int small_type<Dummy>::non_const_copied = 0;
171
172typedef small_type<> small;
173typedef small_type<1> small1;
174typedef small_type<2> small2;
175
176
177// A test type that will NOT trigger the small object optimization in any.
178template <int Dummy = 0>
179struct large_type
180{
181 static int count;
182 static int copied;
183 static int moved;
184 static int const_copied;
185 static int non_const_copied;
186
187 static void reset() {
188 large_type::copied = 0;
189 large_type::moved = 0;
190 large_type::const_copied = 0;
191 large_type::non_const_copied = 0;
192 }
193
194 int value;
195
Eric Fiseliere739d542016-08-11 03:13:11 +0000196 large_type(int val = 0) : value(val) {
Eric Fiselier7175a072015-07-31 02:24:58 +0000197 ++count;
198 data[0] = 0;
199 }
Eric Fiseliere739d542016-08-11 03:13:11 +0000200 large_type(int, int val, int) : value(val) {
201 ++count;
202 data[0] = 0;
203 }
204 large_type(std::initializer_list<int> il) : value(*il.begin()) {
205 ++count;
206 }
Eric Fiselier7175a072015-07-31 02:24:58 +0000207 large_type(large_type const & other) {
208 value = other.value;
209 ++count;
210 ++copied;
211 ++const_copied;
212 }
213
214 large_type(large_type & other) {
215 value = other.value;
216 ++count;
217 ++copied;
218 ++non_const_copied;
219 }
220
221 large_type(large_type && other) {
222 value = other.value;
223 other.value = 0;
224 ++count;
225 ++moved;
226 }
227
228 ~large_type() {
229 value = 0;
230 --count;
231 }
232
233private:
234 large_type& operator=(large_type const&) = delete;
235 large_type& operator=(large_type &&) = delete;
236 int data[10];
237};
238
239template <int Dummy>
240int large_type<Dummy>::count = 0;
241
242template <int Dummy>
243int large_type<Dummy>::copied = 0;
244
245template <int Dummy>
246int large_type<Dummy>::moved = 0;
247
248template <int Dummy>
249int large_type<Dummy>::const_copied = 0;
250
251template <int Dummy>
252int large_type<Dummy>::non_const_copied = 0;
253
254typedef large_type<> large;
255typedef large_type<1> large1;
256typedef large_type<2> large2;
257
258// The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
259// and 'throws_on_move'.
260struct my_any_exception {};
261
262void throwMyAnyExpression() {
263#if !defined(TEST_HAS_NO_EXCEPTIONS)
264 throw my_any_exception();
265#else
266 assert(false && "Exceptions are disabled");
267#endif
268}
269
270// A test type that will trigger the small object optimization within 'any'.
271// this type throws if it is copied.
272struct small_throws_on_copy
273{
274 static int count;
Eric Fiseliere739d542016-08-11 03:13:11 +0000275 static int copied;
276 static int moved;
277 static void reset() { count = copied = moved = 0; }
Eric Fiselier7175a072015-07-31 02:24:58 +0000278 int value;
279
280 explicit small_throws_on_copy(int val = 0) : value(val) {
281 ++count;
282 }
Eric Fiseliere739d542016-08-11 03:13:11 +0000283 explicit small_throws_on_copy(int, int val, int) : value(val) {
284 ++count;
285 }
Eric Fiselier7175a072015-07-31 02:24:58 +0000286 small_throws_on_copy(small_throws_on_copy const &) {
287 throwMyAnyExpression();
288 }
289
290 small_throws_on_copy(small_throws_on_copy && other) throw() {
291 value = other.value;
Eric Fiseliere739d542016-08-11 03:13:11 +0000292 ++count; ++moved;
Eric Fiselier7175a072015-07-31 02:24:58 +0000293 }
294
295 ~small_throws_on_copy() {
296 --count;
297 }
298private:
299 small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
300 small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
301};
302
303int small_throws_on_copy::count = 0;
Eric Fiseliere739d542016-08-11 03:13:11 +0000304int small_throws_on_copy::copied = 0;
305int small_throws_on_copy::moved = 0;
306
Eric Fiselier7175a072015-07-31 02:24:58 +0000307
308// A test type that will NOT trigger the small object optimization within 'any'.
309// this type throws if it is copied.
310struct large_throws_on_copy
311{
312 static int count;
Eric Fiseliere739d542016-08-11 03:13:11 +0000313 static int copied;
314 static int moved;
315 static void reset() { count = copied = moved = 0; }
Eric Fiselier7175a072015-07-31 02:24:58 +0000316 int value = 0;
317
318 explicit large_throws_on_copy(int val = 0) : value(val) {
319 data[0] = 0;
320 ++count;
321 }
Eric Fiseliere739d542016-08-11 03:13:11 +0000322 explicit large_throws_on_copy(int, int val, int) : value(val) {
323 data[0] = 0;
324 ++count;
325 }
Eric Fiselier7175a072015-07-31 02:24:58 +0000326 large_throws_on_copy(large_throws_on_copy const &) {
327 throwMyAnyExpression();
328 }
329
330 large_throws_on_copy(large_throws_on_copy && other) throw() {
331 value = other.value;
Eric Fiseliere739d542016-08-11 03:13:11 +0000332 ++count; ++moved;
Eric Fiselier7175a072015-07-31 02:24:58 +0000333 }
334
335 ~large_throws_on_copy() {
336 --count;
337 }
338
339private:
340 large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
341 large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
342 int data[10];
343};
344
345int large_throws_on_copy::count = 0;
Eric Fiseliere739d542016-08-11 03:13:11 +0000346int large_throws_on_copy::copied = 0;
347int large_throws_on_copy::moved = 0;
Eric Fiselier7175a072015-07-31 02:24:58 +0000348
349// A test type that throws when it is moved. This object will NOT trigger
350// the small object optimization in 'any'.
351struct throws_on_move
352{
353 static int count;
Eric Fiseliere739d542016-08-11 03:13:11 +0000354 static int copied;
355 static int moved;
356 static void reset() { count = copied = moved = 0; }
Eric Fiselier7175a072015-07-31 02:24:58 +0000357 int value;
358
359 explicit throws_on_move(int val = 0) : value(val) { ++count; }
Eric Fiseliere739d542016-08-11 03:13:11 +0000360 explicit throws_on_move(int, int val, int) : value(val) { ++count; }
Eric Fiselier7175a072015-07-31 02:24:58 +0000361 throws_on_move(throws_on_move const & other) {
362 value = other.value;
Eric Fiseliere739d542016-08-11 03:13:11 +0000363 ++count; ++copied;
Eric Fiselier7175a072015-07-31 02:24:58 +0000364 }
365
366 throws_on_move(throws_on_move &&) {
367 throwMyAnyExpression();
368 }
369
370 ~throws_on_move() {
371 --count;
372 }
373private:
374 throws_on_move& operator=(throws_on_move const&) = delete;
375 throws_on_move& operator=(throws_on_move &&) = delete;
376};
377
378int throws_on_move::count = 0;
Eric Fiseliere739d542016-08-11 03:13:11 +0000379int throws_on_move::copied = 0;
380int throws_on_move::moved = 0;
381
382struct small_tracked_t {
383 small_tracked_t()
384 : arg_types(&makeArgumentID<>()) {}
385 small_tracked_t(small_tracked_t const&) noexcept
386 : arg_types(&makeArgumentID<small_tracked_t const&>()) {}
387 small_tracked_t(small_tracked_t &&) noexcept
388 : arg_types(&makeArgumentID<small_tracked_t &&>()) {}
389 template <class ...Args>
390 explicit small_tracked_t(Args&&...)
391 : arg_types(&makeArgumentID<Args...>()) {}
392 template <class ...Args>
393 explicit small_tracked_t(std::initializer_list<int>, Args&&...)
394 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
395
396 TypeID const* arg_types;
397};
398static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
399
400struct large_tracked_t {
401 large_tracked_t()
402 : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
403 large_tracked_t(large_tracked_t const&) noexcept
404 : arg_types(&makeArgumentID<large_tracked_t const&>()) {}
405 large_tracked_t(large_tracked_t &&) noexcept
406 : arg_types(&makeArgumentID<large_tracked_t &&>()) {}
407 template <class ...Args>
408 explicit large_tracked_t(Args&&...)
409 : arg_types(&makeArgumentID<Args...>()) {}
410 template <class ...Args>
411 explicit large_tracked_t(std::initializer_list<int>, Args&&...)
412 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
413
414 TypeID const* arg_types;
415 int dummy[10];
416};
417
418static_assert(!IsSmallObject<large_tracked_t>::value, "must be small");
419
420
421template <class Type, class ...Args>
422void assertArgsMatch(std::any const& a) {
423 using namespace std;
424 using namespace std::experimental;
425 assert(a.has_value());
426 assert(containsType<Type>(a));
427 assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());
428};
Eric Fiselier7175a072015-07-31 02:24:58 +0000429
430
431#endif