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     assert(!a.has_value());
56     RTTI_ASSERT(a.type() == typeid(void));
57     assert(std::any_cast<LastType const>(&a) == nullptr);
58 }
59 
60 template <class Type>
61 constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)
62 { return true; }
has_value_member(long)63 template <class> constexpr bool has_value_member(long) { return false; }
64 
65 
66 // Assert that an 'any' object stores the specified 'Type' and 'value'.
67 template <class Type>
68 std::enable_if_t<has_value_member<Type>(0)>
69 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
assertContains(std::any const & a,int value)70 assertContains(std::any const& a, int value) {
71     assert(a.has_value());
72     assert(containsType<Type>(a));
73     assert(std::any_cast<Type const &>(a).value == value);
74 }
75 
76 template <class Type, class Value>
77 std::enable_if_t<!has_value_member<Type>(0)>
78 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
assertContains(std::any const & a,Value value)79 assertContains(std::any const& a, Value value) {
80     assert(a.has_value());
81     assert(containsType<Type>(a));
82     assert(std::any_cast<Type const &>(a) == value);
83 }
84 
85 
86 // Modify the value of a "test type" stored within an any to the specified
87 // 'value'.
88 template <class Type>
89 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
modifyValue(std::any & a,int value)90 void modifyValue(std::any& a, int value) {
91     assert(a.has_value());
92     assert(containsType<Type>(a));
93     std::any_cast<Type&>(a).value = value;
94 }
95 
96 // A test type that will trigger the small object optimization within 'any'.
97 template <int Dummy = 0>
98 struct small_type
99 {
100     static int count;
101     static int copied;
102     static int moved;
103     static int const_copied;
104     static int non_const_copied;
105 
resetsmall_type106     static void reset() {
107         small_type::copied = 0;
108         small_type::moved = 0;
109         small_type::const_copied = 0;
110         small_type::non_const_copied = 0;
111     }
112 
113     int value;
114 
valuesmall_type115     explicit small_type(int val = 0) : value(val) {
116         ++count;
117     }
small_typesmall_type118     explicit small_type(int, int val, int) : value(val) {
119         ++count;
120     }
small_typesmall_type121     small_type(std::initializer_list<int> il) : value(*il.begin()) {
122         ++count;
123     }
124 
small_typesmall_type125     small_type(small_type const & other) noexcept {
126         value = other.value;
127         ++count;
128         ++copied;
129         ++const_copied;
130     }
131 
small_typesmall_type132     small_type(small_type& other) noexcept {
133         value = other.value;
134         ++count;
135         ++copied;
136         ++non_const_copied;
137     }
138 
small_typesmall_type139     small_type(small_type && other) noexcept {
140         value = other.value;
141         other.value = 0;
142         ++count;
143         ++moved;
144     }
145 
~small_typesmall_type146     ~small_type() {
147         value = -1;
148         --count;
149     }
150 
151 private:
152     small_type& operator=(small_type const&) = delete;
153     small_type& operator=(small_type&&) = delete;
154 };
155 
156 template <int Dummy>
157 int small_type<Dummy>::count = 0;
158 
159 template <int Dummy>
160 int small_type<Dummy>::copied = 0;
161 
162 template <int Dummy>
163 int small_type<Dummy>::moved = 0;
164 
165 template <int Dummy>
166 int small_type<Dummy>::const_copied = 0;
167 
168 template <int Dummy>
169 int small_type<Dummy>::non_const_copied = 0;
170 
171 typedef small_type<> small;
172 typedef small_type<1> small1;
173 typedef small_type<2> small2;
174 
175 
176 // A test type that will NOT trigger the small object optimization in any.
177 template <int Dummy = 0>
178 struct large_type
179 {
180     static int count;
181     static int copied;
182     static int moved;
183     static int const_copied;
184     static int non_const_copied;
185 
resetlarge_type186     static void reset() {
187         large_type::copied = 0;
188         large_type::moved  = 0;
189         large_type::const_copied = 0;
190         large_type::non_const_copied = 0;
191     }
192 
193     int value;
194 
valuelarge_type195     large_type(int val = 0) : value(val) {
196         ++count;
197         data[0] = 0;
198     }
large_typelarge_type199     large_type(int, int val, int) : value(val) {
200         ++count;
201         data[0] = 0;
202     }
large_typelarge_type203     large_type(std::initializer_list<int> il) : value(*il.begin()) {
204         ++count;
205     }
large_typelarge_type206     large_type(large_type const & other) {
207         value = other.value;
208         ++count;
209         ++copied;
210         ++const_copied;
211     }
212 
large_typelarge_type213     large_type(large_type & other) {
214         value = other.value;
215         ++count;
216         ++copied;
217         ++non_const_copied;
218     }
219 
large_typelarge_type220     large_type(large_type && other) {
221         value = other.value;
222         other.value = 0;
223         ++count;
224         ++moved;
225     }
226 
~large_typelarge_type227     ~large_type()  {
228         value = 0;
229         --count;
230     }
231 
232 private:
233     large_type& operator=(large_type const&) = delete;
234     large_type& operator=(large_type &&) = delete;
235     int data[10];
236 };
237 
238 template <int Dummy>
239 int large_type<Dummy>::count = 0;
240 
241 template <int Dummy>
242 int large_type<Dummy>::copied = 0;
243 
244 template <int Dummy>
245 int large_type<Dummy>::moved = 0;
246 
247 template <int Dummy>
248 int large_type<Dummy>::const_copied = 0;
249 
250 template <int Dummy>
251 int large_type<Dummy>::non_const_copied = 0;
252 
253 typedef large_type<> large;
254 typedef large_type<1> large1;
255 typedef large_type<2> large2;
256 
257 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
258 // and 'throws_on_move'.
259 struct my_any_exception {};
260 
throwMyAnyExpression()261 void throwMyAnyExpression() {
262 #if !defined(TEST_HAS_NO_EXCEPTIONS)
263         throw my_any_exception();
264 #else
265         assert(false && "Exceptions are disabled");
266 #endif
267 }
268 
269 // A test type that will trigger the small object optimization within 'any'.
270 // this type throws if it is copied.
271 struct small_throws_on_copy
272 {
273     static int count;
274     static int copied;
275     static int moved;
resetsmall_throws_on_copy276     static void reset() { count = copied = moved = 0; }
277     int value;
278 
valuesmall_throws_on_copy279     explicit small_throws_on_copy(int val = 0) : value(val) {
280         ++count;
281     }
small_throws_on_copysmall_throws_on_copy282     explicit small_throws_on_copy(int, int val, int) : value(val) {
283         ++count;
284     }
small_throws_on_copysmall_throws_on_copy285     small_throws_on_copy(small_throws_on_copy const &) {
286         throwMyAnyExpression();
287     }
288 
throwsmall_throws_on_copy289     small_throws_on_copy(small_throws_on_copy && other) throw() {
290         value = other.value;
291         ++count; ++moved;
292     }
293 
~small_throws_on_copysmall_throws_on_copy294     ~small_throws_on_copy() {
295         --count;
296     }
297 private:
298     small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
299     small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
300 };
301 
302 int small_throws_on_copy::count = 0;
303 int small_throws_on_copy::copied = 0;
304 int small_throws_on_copy::moved = 0;
305 
306 
307 // A test type that will NOT trigger the small object optimization within 'any'.
308 // this type throws if it is copied.
309 struct large_throws_on_copy
310 {
311     static int count;
312     static int copied;
313     static int moved;
resetlarge_throws_on_copy314     static void reset() { count = copied = moved = 0; }
315     int value = 0;
316 
valuelarge_throws_on_copy317     explicit large_throws_on_copy(int val = 0) : value(val) {
318         data[0] = 0;
319         ++count;
320     }
large_throws_on_copylarge_throws_on_copy321     explicit large_throws_on_copy(int, int val, int) : value(val) {
322         data[0] = 0;
323         ++count;
324     }
large_throws_on_copylarge_throws_on_copy325     large_throws_on_copy(large_throws_on_copy const &) {
326          throwMyAnyExpression();
327     }
328 
throwlarge_throws_on_copy329     large_throws_on_copy(large_throws_on_copy && other) throw() {
330         value = other.value;
331         ++count; ++moved;
332     }
333 
~large_throws_on_copylarge_throws_on_copy334     ~large_throws_on_copy() {
335         --count;
336     }
337 
338 private:
339     large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
340     large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
341     int data[10];
342 };
343 
344 int large_throws_on_copy::count = 0;
345 int large_throws_on_copy::copied = 0;
346 int large_throws_on_copy::moved = 0;
347 
348 // A test type that throws when it is moved. This object will NOT trigger
349 // the small object optimization in 'any'.
350 struct throws_on_move
351 {
352     static int count;
353     static int copied;
354     static int moved;
resetthrows_on_move355     static void reset() { count = copied = moved = 0; }
356     int value;
357 
valuethrows_on_move358     explicit throws_on_move(int val = 0) : value(val) { ++count; }
throws_on_movethrows_on_move359     explicit throws_on_move(int, int val, int) : value(val) { ++count; }
throws_on_movethrows_on_move360     throws_on_move(throws_on_move const & other) {
361         value = other.value;
362         ++count; ++copied;
363     }
364 
throws_on_movethrows_on_move365     throws_on_move(throws_on_move &&) {
366         throwMyAnyExpression();
367     }
368 
~throws_on_movethrows_on_move369     ~throws_on_move() {
370         --count;
371     }
372 private:
373     throws_on_move& operator=(throws_on_move const&) = delete;
374     throws_on_move& operator=(throws_on_move &&) = delete;
375 };
376 
377 int throws_on_move::count = 0;
378 int throws_on_move::copied = 0;
379 int throws_on_move::moved = 0;
380 
381 struct small_tracked_t {
small_tracked_tsmall_tracked_t382   small_tracked_t()
383       : arg_types(&makeArgumentID<>()) {}
small_tracked_tsmall_tracked_t384   small_tracked_t(small_tracked_t const&) noexcept
385       : arg_types(&makeArgumentID<small_tracked_t const&>()) {}
small_tracked_tsmall_tracked_t386   small_tracked_t(small_tracked_t &&) noexcept
387       : arg_types(&makeArgumentID<small_tracked_t &&>()) {}
388   template <class ...Args>
small_tracked_tsmall_tracked_t389   explicit small_tracked_t(Args&&...)
390       : arg_types(&makeArgumentID<Args...>()) {}
391   template <class ...Args>
small_tracked_tsmall_tracked_t392   explicit small_tracked_t(std::initializer_list<int>, Args&&...)
393       : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
394 
395   TypeID const* arg_types;
396 };
397 static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
398 
399 struct large_tracked_t {
large_tracked_tlarge_tracked_t400   large_tracked_t()
401       : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
large_tracked_tlarge_tracked_t402   large_tracked_t(large_tracked_t const&) noexcept
403       : arg_types(&makeArgumentID<large_tracked_t const&>()) {}
large_tracked_tlarge_tracked_t404   large_tracked_t(large_tracked_t &&) noexcept
405       : arg_types(&makeArgumentID<large_tracked_t &&>()) {}
406   template <class ...Args>
large_tracked_tlarge_tracked_t407   explicit large_tracked_t(Args&&...)
408       : arg_types(&makeArgumentID<Args...>()) {}
409   template <class ...Args>
large_tracked_tlarge_tracked_t410   explicit large_tracked_t(std::initializer_list<int>, Args&&...)
411       : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
412 
413   TypeID const* arg_types;
414   int dummy[sizeof(std::any) / sizeof(int) + 1];
415 };
416 
417 static_assert(!IsSmallObject<large_tracked_t>::value, "must not be small");
418 
419 
420 template <class Type, class ...Args>
assertArgsMatch(std::any const & a)421 void assertArgsMatch(std::any const& a) {
422     assert(a.has_value());
423     assert(containsType<Type>(a));
424     assert(std::any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());
425 };
426 
427 
428 #endif
429