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 EXPERIMENTAL_ANY_HELPERS_H
9 #define EXPERIMENTAL_ANY_HELPERS_H
10 
11 #include <experimental/any>
12 #include <typeinfo>
13 #include <type_traits>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 
18 #if !defined(TEST_HAS_NO_RTTI)
19 #define RTTI_ASSERT(X) assert(X)
20 #else
21 #define RTTI_ASSERT(X)
22 #endif
23 
24 template <class T>
25   struct IsSmallObject
26     : public std::integral_constant<bool
27         , sizeof(T) <= (sizeof(void*)*3)
28           && std::alignment_of<void*>::value
29              % std::alignment_of<T>::value == 0
30           && std::is_nothrow_move_constructible<T>::value
31         >
32   {};
33 
34 
35 // Return 'true' if 'Type' will be considered a small type by 'any'
36 template <class Type>
isSmallType()37 bool isSmallType() {
38 #if defined(_LIBCPP_VERSION)
39     return std::experimental::__any_imp::_IsSmallObject<Type>::value;
40 #else
41     return IsSmallObject<Type>::value;
42 #endif
43 
44 }
45 
46 // Assert that an object is empty. If the object used to contain an object
47 // of type 'LastType' check that it can no longer be accessed.
48 template <class LastType = int>
assertEmpty(std::experimental::any const & a)49 void assertEmpty(std::experimental::any const& a) {
50     assert(a.empty());
51     RTTI_ASSERT(a.type() == typeid(void));
52     assert(std::experimental::any_cast<LastType const>(&a) == nullptr);
53 }
54 
55 // Assert that an 'any' object stores the specified 'Type' and 'value'.
56 template <class Type>
57 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
58 void assertContains(std::experimental::any const& a, int value = 1) {
59     assert(!a.empty());
60     RTTI_ASSERT(a.type() == typeid(Type));
61     assert(std::experimental::any_cast<Type const &>(a).value == value);
62 }
63 
64 // Modify the value of a "test type" stored within an any to the specified
65 // 'value'.
66 template <class Type>
67 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
modifyValue(std::experimental::any & a,int value)68 void modifyValue(std::experimental::any& a, int value) {
69     assert(!a.empty());
70     RTTI_ASSERT(a.type() == typeid(Type));
71     std::experimental::any_cast<Type&>(a).value = value;
72 }
73 
74 // A test type that will trigger the small object optimization within 'any'.
75 template <int Dummy = 0>
76 struct small_type
77 {
78     static int count;
79     static int copied;
80     static int moved;
81     static int const_copied;
82     static int non_const_copied;
83 
resetsmall_type84     static void reset() {
85         small_type::copied = 0;
86         small_type::moved = 0;
87         small_type::const_copied = 0;
88         small_type::non_const_copied = 0;
89     }
90 
91     int value;
92 
small_typesmall_type93     explicit small_type(int val) : value(val) {
94         ++count;
95     }
96 
throwsmall_type97     small_type(small_type const & other) throw() {
98         value = other.value;
99         ++count;
100         ++copied;
101         ++const_copied;
102     }
103 
throwsmall_type104     small_type(small_type& other) throw() {
105         value = other.value;
106         ++count;
107         ++copied;
108         ++non_const_copied;
109     }
110 
throwsmall_type111     small_type(small_type && other) throw() {
112         value = other.value;
113         other.value = 0;
114         ++count;
115         ++moved;
116     }
117 
~small_typesmall_type118     ~small_type() {
119         value = -1;
120         --count;
121     }
122 
123 private:
124     small_type& operator=(small_type const&) = delete;
125     small_type& operator=(small_type&&) = delete;
126 };
127 
128 template <int Dummy>
129 int small_type<Dummy>::count = 0;
130 
131 template <int Dummy>
132 int small_type<Dummy>::copied = 0;
133 
134 template <int Dummy>
135 int small_type<Dummy>::moved = 0;
136 
137 template <int Dummy>
138 int small_type<Dummy>::const_copied = 0;
139 
140 template <int Dummy>
141 int small_type<Dummy>::non_const_copied = 0;
142 
143 typedef small_type<> small;
144 typedef small_type<1> small1;
145 typedef small_type<2> small2;
146 
147 
148 // A test type that will NOT trigger the small object optimization in any.
149 template <int Dummy = 0>
150 struct large_type
151 {
152     static int count;
153     static int copied;
154     static int moved;
155     static int const_copied;
156     static int non_const_copied;
157 
resetlarge_type158     static void reset() {
159         large_type::copied = 0;
160         large_type::moved  = 0;
161         large_type::const_copied = 0;
162         large_type::non_const_copied = 0;
163     }
164 
165     int value;
166 
large_typelarge_type167     large_type(int val) : value(val) {
168         ++count;
169         data[0] = 0;
170     }
171 
large_typelarge_type172     large_type(large_type const & other) {
173         value = other.value;
174         ++count;
175         ++copied;
176         ++const_copied;
177     }
178 
large_typelarge_type179     large_type(large_type & other) {
180         value = other.value;
181         ++count;
182         ++copied;
183         ++non_const_copied;
184     }
185 
large_typelarge_type186     large_type(large_type && other) {
187         value = other.value;
188         other.value = 0;
189         ++count;
190         ++moved;
191     }
192 
~large_typelarge_type193     ~large_type()  {
194         value = 0;
195         --count;
196     }
197 
198 private:
199     large_type& operator=(large_type const&) = delete;
200     large_type& operator=(large_type &&) = delete;
201     int data[10];
202 };
203 
204 template <int Dummy>
205 int large_type<Dummy>::count = 0;
206 
207 template <int Dummy>
208 int large_type<Dummy>::copied = 0;
209 
210 template <int Dummy>
211 int large_type<Dummy>::moved = 0;
212 
213 template <int Dummy>
214 int large_type<Dummy>::const_copied = 0;
215 
216 template <int Dummy>
217 int large_type<Dummy>::non_const_copied = 0;
218 
219 typedef large_type<> large;
220 typedef large_type<1> large1;
221 typedef large_type<2> large2;
222 
223 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
224 // and 'throws_on_move'.
225 struct my_any_exception {};
226 
throwMyAnyExpression()227 void throwMyAnyExpression() {
228 #if !defined(TEST_HAS_NO_EXCEPTIONS)
229         throw my_any_exception();
230 #else
231         assert(false && "Exceptions are disabled");
232 #endif
233 }
234 
235 // A test type that will trigger the small object optimization within 'any'.
236 // this type throws if it is copied.
237 struct small_throws_on_copy
238 {
239     static int count;
240     int value;
241 
valuesmall_throws_on_copy242     explicit small_throws_on_copy(int val = 0) : value(val) {
243         ++count;
244     }
245 
small_throws_on_copysmall_throws_on_copy246     small_throws_on_copy(small_throws_on_copy const &) {
247         throwMyAnyExpression();
248     }
249 
throwsmall_throws_on_copy250     small_throws_on_copy(small_throws_on_copy && other) throw() {
251         value = other.value;
252         ++count;
253     }
254 
~small_throws_on_copysmall_throws_on_copy255     ~small_throws_on_copy() {
256         --count;
257     }
258 private:
259     small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
260     small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
261 };
262 
263 int small_throws_on_copy::count = 0;
264 
265 // A test type that will NOT trigger the small object optimization within 'any'.
266 // this type throws if it is copied.
267 struct large_throws_on_copy
268 {
269     static int count;
270     int value = 0;
271 
valuelarge_throws_on_copy272     explicit large_throws_on_copy(int val = 0) : value(val) {
273         data[0] = 0;
274         ++count;
275     }
276 
large_throws_on_copylarge_throws_on_copy277     large_throws_on_copy(large_throws_on_copy const &) {
278          throwMyAnyExpression();
279     }
280 
throwlarge_throws_on_copy281     large_throws_on_copy(large_throws_on_copy && other) throw() {
282         value = other.value;
283         ++count;
284     }
285 
~large_throws_on_copylarge_throws_on_copy286     ~large_throws_on_copy() {
287         --count;
288     }
289 
290 private:
291     large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
292     large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
293     int data[10];
294 };
295 
296 int large_throws_on_copy::count = 0;
297 
298 // A test type that throws when it is moved. This object will NOT trigger
299 // the small object optimization in 'any'.
300 struct throws_on_move
301 {
302     static int count;
303     int value;
304 
valuethrows_on_move305     explicit throws_on_move(int val = 0) : value(val) { ++count; }
306 
throws_on_movethrows_on_move307     throws_on_move(throws_on_move const & other) {
308         value = other.value;
309         ++count;
310     }
311 
throws_on_movethrows_on_move312     throws_on_move(throws_on_move &&) {
313         throwMyAnyExpression();
314     }
315 
~throws_on_movethrows_on_move316     ~throws_on_move() {
317         --count;
318     }
319 private:
320     throws_on_move& operator=(throws_on_move const&) = delete;
321     throws_on_move& operator=(throws_on_move &&) = delete;
322 };
323 
324 int throws_on_move::count = 0;
325 
326 
327 #endif
328