1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef ANY_HELPERS_H
9 #define ANY_HELPERS_H
10 
11 #include <typeinfo>
12 #include <type_traits>
13 #include <cassert>
14 
15 namespace std { namespace experimental {} }
16 
17 #include "test_macros.h"
18 #include "type_id.h"
19 
20 #if !defined(TEST_HAS_NO_RTTI)
21 #define RTTI_ASSERT(X) assert(X)
22 #else
23 #define RTTI_ASSERT(X)
24 #endif
25 
26 template <class T>
27   struct IsSmallObject
28     : public std::integral_constant<bool
29         , sizeof(T) <= sizeof(std::any) - sizeof(void*)
30           && std::alignment_of<void*>::value
31              % std::alignment_of<T>::value == 0
32           && std::is_nothrow_move_constructible<T>::value
33         >
34   {};
35 
36 template <class T>
containsType(std::any const & a)37 bool containsType(std::any const& a) {
38 #if !defined(TEST_HAS_NO_RTTI)
39     return a.type() == typeid(T);
40 #else
41     return a.has_value() && std::any_cast<T>(&a) != nullptr;
42 #endif
43 }
44 
45 // Return 'true' if 'Type' will be considered a small type by 'any'
46 template <class Type>
isSmallType()47 bool isSmallType() {
48     return IsSmallObject<Type>::value;
49 }
50 
51 // Assert that an object is empty. If the object used to contain an object
52 // of type 'LastType' check that it can no longer be accessed.
53 template <class LastType = int>
assertEmpty(std::any const & a)54 void assertEmpty(std::any const& a) {
55     using namespace std;
56     assert(!a.has_value());
57     RTTI_ASSERT(a.type() == typeid(void));
58     assert(any_cast<LastType const>(&a) == nullptr);
59 }
60 
61 template <class Type>
62 constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)
63 { return true; }
has_value_member(long)64 template <class> constexpr bool has_value_member(long) { return false; }
65 
66 
67 // Assert that an 'any' object stores the specified 'Type' and 'value'.
68 template <class Type>
69 std::enable_if_t<has_value_member<Type>(0)>
70 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
assertContains(std::any const & a,int value)71 assertContains(std::any const& a, int value) {
72     assert(a.has_value());
73     assert(containsType<Type>(a));
74     assert(std::any_cast<Type const &>(a).value == value);
75 }
76 
77 template <class Type, class Value>
78 std::enable_if_t<!has_value_member<Type>(0)>
79 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
assertContains(std::any const & a,Value value)80 assertContains(std::any const& a, Value value) {
81     assert(a.has_value());
82     assert(containsType<Type>(a));
83     assert(std::any_cast<Type const &>(a) == value);
84 }
85 
86 
87 // Modify the value of a "test type" stored within an any to the specified
88 // 'value'.
89 template <class Type>
90 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
modifyValue(std::any & a,int value)91 void modifyValue(std::any& a, int value) {
92     using namespace std;
93     using namespace std::experimental;
94     assert(a.has_value());
95     assert(containsType<Type>(a));
96     any_cast<Type&>(a).value = value;
97 }
98 
99 // A test type that will trigger the small object optimization within 'any'.
100 template <int Dummy = 0>
101 struct small_type
102 {
103     static int count;
104     static int copied;
105     static int moved;
106     static int const_copied;
107     static int non_const_copied;
108 
resetsmall_type109     static void reset() {
110         small_type::copied = 0;
111         small_type::moved = 0;
112         small_type::const_copied = 0;
113         small_type::non_const_copied = 0;
114     }
115 
116     int value;
117 
valuesmall_type118     explicit small_type(int val = 0) : value(val) {
119         ++count;
120     }
small_typesmall_type121     explicit small_type(int, int val, int) : value(val) {
122         ++count;
123     }
small_typesmall_type124     small_type(std::initializer_list<int> il) : value(*il.begin()) {
125         ++count;
126     }
127 
small_typesmall_type128     small_type(small_type const & other) noexcept {
129         value = other.value;
130         ++count;
131         ++copied;
132         ++const_copied;
133     }
134 
small_typesmall_type135     small_type(small_type& other) noexcept {
136         value = other.value;
137         ++count;
138         ++copied;
139         ++non_const_copied;
140     }
141 
small_typesmall_type142     small_type(small_type && other) noexcept {
143         value = other.value;
144         other.value = 0;
145         ++count;
146         ++moved;
147     }
148 
~small_typesmall_type149     ~small_type() {
150         value = -1;
151         --count;
152     }
153 
154 private:
155     small_type& operator=(small_type const&) = delete;
156     small_type& operator=(small_type&&) = delete;
157 };
158 
159 template <int Dummy>
160 int small_type<Dummy>::count = 0;
161 
162 template <int Dummy>
163 int small_type<Dummy>::copied = 0;
164 
165 template <int Dummy>
166 int small_type<Dummy>::moved = 0;
167 
168 template <int Dummy>
169 int small_type<Dummy>::const_copied = 0;
170 
171 template <int Dummy>
172 int small_type<Dummy>::non_const_copied = 0;
173 
174 typedef small_type<> small;
175 typedef small_type<1> small1;
176 typedef small_type<2> small2;
177 
178 
179 // A test type that will NOT trigger the small object optimization in any.
180 template <int Dummy = 0>
181 struct large_type
182 {
183     static int count;
184     static int copied;
185     static int moved;
186     static int const_copied;
187     static int non_const_copied;
188 
resetlarge_type189     static void reset() {
190         large_type::copied = 0;
191         large_type::moved  = 0;
192         large_type::const_copied = 0;
193         large_type::non_const_copied = 0;
194     }
195 
196     int value;
197 
valuelarge_type198     large_type(int val = 0) : value(val) {
199         ++count;
200         data[0] = 0;
201     }
large_typelarge_type202     large_type(int, int val, int) : value(val) {
203         ++count;
204         data[0] = 0;
205     }
large_typelarge_type206     large_type(std::initializer_list<int> il) : value(*il.begin()) {
207         ++count;
208     }
large_typelarge_type209     large_type(large_type const & other) {
210         value = other.value;
211         ++count;
212         ++copied;
213         ++const_copied;
214     }
215 
large_typelarge_type216     large_type(large_type & other) {
217         value = other.value;
218         ++count;
219         ++copied;
220         ++non_const_copied;
221     }
222 
large_typelarge_type223     large_type(large_type && other) {
224         value = other.value;
225         other.value = 0;
226         ++count;
227         ++moved;
228     }
229 
~large_typelarge_type230     ~large_type()  {
231         value = 0;
232         --count;
233     }
234 
235 private:
236     large_type& operator=(large_type const&) = delete;
237     large_type& operator=(large_type &&) = delete;
238     int data[10];
239 };
240 
241 template <int Dummy>
242 int large_type<Dummy>::count = 0;
243 
244 template <int Dummy>
245 int large_type<Dummy>::copied = 0;
246 
247 template <int Dummy>
248 int large_type<Dummy>::moved = 0;
249 
250 template <int Dummy>
251 int large_type<Dummy>::const_copied = 0;
252 
253 template <int Dummy>
254 int large_type<Dummy>::non_const_copied = 0;
255 
256 typedef large_type<> large;
257 typedef large_type<1> large1;
258 typedef large_type<2> large2;
259 
260 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
261 // and 'throws_on_move'.
262 struct my_any_exception {};
263 
throwMyAnyExpression()264 void throwMyAnyExpression() {
265 #if !defined(TEST_HAS_NO_EXCEPTIONS)
266         throw my_any_exception();
267 #else
268         assert(false && "Exceptions are disabled");
269 #endif
270 }
271 
272 // A test type that will trigger the small object optimization within 'any'.
273 // this type throws if it is copied.
274 struct small_throws_on_copy
275 {
276     static int count;
277     static int copied;
278     static int moved;
resetsmall_throws_on_copy279     static void reset() { count = copied = moved = 0; }
280     int value;
281 
valuesmall_throws_on_copy282     explicit small_throws_on_copy(int val = 0) : value(val) {
283         ++count;
284     }
small_throws_on_copysmall_throws_on_copy285     explicit small_throws_on_copy(int, int val, int) : value(val) {
286         ++count;
287     }
small_throws_on_copysmall_throws_on_copy288     small_throws_on_copy(small_throws_on_copy const &) {
289         throwMyAnyExpression();
290     }
291 
throwsmall_throws_on_copy292     small_throws_on_copy(small_throws_on_copy && other) throw() {
293         value = other.value;
294         ++count; ++moved;
295     }
296 
~small_throws_on_copysmall_throws_on_copy297     ~small_throws_on_copy() {
298         --count;
299     }
300 private:
301     small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
302     small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
303 };
304 
305 int small_throws_on_copy::count = 0;
306 int small_throws_on_copy::copied = 0;
307 int small_throws_on_copy::moved = 0;
308 
309 
310 // A test type that will NOT trigger the small object optimization within 'any'.
311 // this type throws if it is copied.
312 struct large_throws_on_copy
313 {
314     static int count;
315     static int copied;
316     static int moved;
resetlarge_throws_on_copy317     static void reset() { count = copied = moved = 0; }
318     int value = 0;
319 
valuelarge_throws_on_copy320     explicit large_throws_on_copy(int val = 0) : value(val) {
321         data[0] = 0;
322         ++count;
323     }
large_throws_on_copylarge_throws_on_copy324     explicit large_throws_on_copy(int, int val, int) : value(val) {
325         data[0] = 0;
326         ++count;
327     }
large_throws_on_copylarge_throws_on_copy328     large_throws_on_copy(large_throws_on_copy const &) {
329          throwMyAnyExpression();
330     }
331 
throwlarge_throws_on_copy332     large_throws_on_copy(large_throws_on_copy && other) throw() {
333         value = other.value;
334         ++count; ++moved;
335     }
336 
~large_throws_on_copylarge_throws_on_copy337     ~large_throws_on_copy() {
338         --count;
339     }
340 
341 private:
342     large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
343     large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
344     int data[10];
345 };
346 
347 int large_throws_on_copy::count = 0;
348 int large_throws_on_copy::copied = 0;
349 int large_throws_on_copy::moved = 0;
350 
351 // A test type that throws when it is moved. This object will NOT trigger
352 // the small object optimization in 'any'.
353 struct throws_on_move
354 {
355     static int count;
356     static int copied;
357     static int moved;
resetthrows_on_move358     static void reset() { count = copied = moved = 0; }
359     int value;
360 
valuethrows_on_move361     explicit throws_on_move(int val = 0) : value(val) { ++count; }
throws_on_movethrows_on_move362     explicit throws_on_move(int, int val, int) : value(val) { ++count; }
throws_on_movethrows_on_move363     throws_on_move(throws_on_move const & other) {
364         value = other.value;
365         ++count; ++copied;
366     }
367 
throws_on_movethrows_on_move368     throws_on_move(throws_on_move &&) {
369         throwMyAnyExpression();
370     }
371 
~throws_on_movethrows_on_move372     ~throws_on_move() {
373         --count;
374     }
375 private:
376     throws_on_move& operator=(throws_on_move const&) = delete;
377     throws_on_move& operator=(throws_on_move &&) = delete;
378 };
379 
380 int throws_on_move::count = 0;
381 int throws_on_move::copied = 0;
382 int throws_on_move::moved = 0;
383 
384 struct small_tracked_t {
small_tracked_tsmall_tracked_t385   small_tracked_t()
386       : arg_types(&makeArgumentID<>()) {}
small_tracked_tsmall_tracked_t387   small_tracked_t(small_tracked_t const&) noexcept
388       : arg_types(&makeArgumentID<small_tracked_t const&>()) {}
small_tracked_tsmall_tracked_t389   small_tracked_t(small_tracked_t &&) noexcept
390       : arg_types(&makeArgumentID<small_tracked_t &&>()) {}
391   template <class ...Args>
small_tracked_tsmall_tracked_t392   explicit small_tracked_t(Args&&...)
393       : arg_types(&makeArgumentID<Args...>()) {}
394   template <class ...Args>
small_tracked_tsmall_tracked_t395   explicit small_tracked_t(std::initializer_list<int>, Args&&...)
396       : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
397 
398   TypeID const* arg_types;
399 };
400 static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
401 
402 struct large_tracked_t {
large_tracked_tlarge_tracked_t403   large_tracked_t()
404       : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
large_tracked_tlarge_tracked_t405   large_tracked_t(large_tracked_t const&) noexcept
406       : arg_types(&makeArgumentID<large_tracked_t const&>()) {}
large_tracked_tlarge_tracked_t407   large_tracked_t(large_tracked_t &&) noexcept
408       : arg_types(&makeArgumentID<large_tracked_t &&>()) {}
409   template <class ...Args>
large_tracked_tlarge_tracked_t410   explicit large_tracked_t(Args&&...)
411       : arg_types(&makeArgumentID<Args...>()) {}
412   template <class ...Args>
large_tracked_tlarge_tracked_t413   explicit large_tracked_t(std::initializer_list<int>, Args&&...)
414       : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
415 
416   TypeID const* arg_types;
417   int dummy[sizeof(std::any) / sizeof(int) + 1];
418 };
419 
420 static_assert(!IsSmallObject<large_tracked_t>::value, "must not be small");
421 
422 
423 template <class Type, class ...Args>
assertArgsMatch(std::any const & a)424 void assertArgsMatch(std::any const& a) {
425     using namespace std;
426     using namespace std::experimental;
427     assert(a.has_value());
428     assert(containsType<Type>(a));
429     assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());
430 };
431 
432 
433 #endif
434