blob: eeffd8c98d78c81e16f637992fab22b37e239dba [file] [log] [blame]
Eric Fiselier324506b2016-08-11 03:13:11 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Eric Fiselier324506b2016-08-11 03:13:11 +00006//
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
Stephan T. Lavavejaae63562017-08-11 20:53:53 +000024template <class T>
Eric Fiselier324506b2016-08-11 03:13:11 +000025 struct IsSmallObject
26 : public std::integral_constant<bool
Stephan T. Lavavejaae63562017-08-11 20:53:53 +000027 , sizeof(T) <= (sizeof(void*)*3)
Eric Fiselier324506b2016-08-11 03:13:11 +000028 && std::alignment_of<void*>::value
Stephan T. Lavavejaae63562017-08-11 20:53:53 +000029 % std::alignment_of<T>::value == 0
30 && std::is_nothrow_move_constructible<T>::value
Eric Fiselier324506b2016-08-11 03:13:11 +000031 >
32 {};
33
34
35// Return 'true' if 'Type' will be considered a small type by 'any'
36template <class Type>
37bool 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.
48template <class LastType = int>
49void 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'.
56template <class Type>
Duncan P. N. Exon Smithb1cb2792017-06-18 14:52:26 +000057_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
Eric Fiselier324506b2016-08-11 03:13:11 +000058void 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'.
66template <class Type>
Duncan P. N. Exon Smithb1cb2792017-06-18 14:52:26 +000067_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
Eric Fiselier324506b2016-08-11 03:13:11 +000068void 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'.
75template <int Dummy = 0>
76struct 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
84 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
93 explicit small_type(int val) : value(val) {
94 ++count;
95 }
96
97 small_type(small_type const & other) throw() {
98 value = other.value;
99 ++count;
100 ++copied;
101 ++const_copied;
102 }
103
104 small_type(small_type& other) throw() {
105 value = other.value;
106 ++count;
107 ++copied;
108 ++non_const_copied;
109 }
110
111 small_type(small_type && other) throw() {
112 value = other.value;
113 other.value = 0;
114 ++count;
115 ++moved;
116 }
117
118 ~small_type() {
119 value = -1;
120 --count;
121 }
122
123private:
124 small_type& operator=(small_type const&) = delete;
125 small_type& operator=(small_type&&) = delete;
126};
127
128template <int Dummy>
129int small_type<Dummy>::count = 0;
130
131template <int Dummy>
132int small_type<Dummy>::copied = 0;
133
134template <int Dummy>
135int small_type<Dummy>::moved = 0;
136
137template <int Dummy>
138int small_type<Dummy>::const_copied = 0;
139
140template <int Dummy>
141int small_type<Dummy>::non_const_copied = 0;
142
143typedef small_type<> small;
144typedef small_type<1> small1;
145typedef small_type<2> small2;
146
147
148// A test type that will NOT trigger the small object optimization in any.
149template <int Dummy = 0>
150struct 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
158 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
167 large_type(int val) : value(val) {
168 ++count;
169 data[0] = 0;
170 }
171
172 large_type(large_type const & other) {
173 value = other.value;
174 ++count;
175 ++copied;
176 ++const_copied;
177 }
178
179 large_type(large_type & other) {
180 value = other.value;
181 ++count;
182 ++copied;
183 ++non_const_copied;
184 }
185
186 large_type(large_type && other) {
187 value = other.value;
188 other.value = 0;
189 ++count;
190 ++moved;
191 }
192
193 ~large_type() {
194 value = 0;
195 --count;
196 }
197
198private:
199 large_type& operator=(large_type const&) = delete;
200 large_type& operator=(large_type &&) = delete;
201 int data[10];
202};
203
204template <int Dummy>
205int large_type<Dummy>::count = 0;
206
207template <int Dummy>
208int large_type<Dummy>::copied = 0;
209
210template <int Dummy>
211int large_type<Dummy>::moved = 0;
212
213template <int Dummy>
214int large_type<Dummy>::const_copied = 0;
215
216template <int Dummy>
217int large_type<Dummy>::non_const_copied = 0;
218
219typedef large_type<> large;
220typedef large_type<1> large1;
221typedef large_type<2> large2;
222
223// The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
224// and 'throws_on_move'.
225struct my_any_exception {};
226
227void 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.
237struct small_throws_on_copy
238{
239 static int count;
240 int value;
241
242 explicit small_throws_on_copy(int val = 0) : value(val) {
243 ++count;
244 }
245
246 small_throws_on_copy(small_throws_on_copy const &) {
247 throwMyAnyExpression();
248 }
249
250 small_throws_on_copy(small_throws_on_copy && other) throw() {
251 value = other.value;
252 ++count;
253 }
254
255 ~small_throws_on_copy() {
256 --count;
257 }
258private:
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
263int 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.
267struct large_throws_on_copy
268{
269 static int count;
270 int value = 0;
271
272 explicit large_throws_on_copy(int val = 0) : value(val) {
273 data[0] = 0;
274 ++count;
275 }
276
277 large_throws_on_copy(large_throws_on_copy const &) {
278 throwMyAnyExpression();
279 }
280
281 large_throws_on_copy(large_throws_on_copy && other) throw() {
282 value = other.value;
283 ++count;
284 }
285
286 ~large_throws_on_copy() {
287 --count;
288 }
289
290private:
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
296int 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'.
300struct throws_on_move
301{
302 static int count;
303 int value;
304
305 explicit throws_on_move(int val = 0) : value(val) { ++count; }
306
307 throws_on_move(throws_on_move const & other) {
308 value = other.value;
309 ++count;
310 }
311
312 throws_on_move(throws_on_move &&) {
313 throwMyAnyExpression();
314 }
315
316 ~throws_on_move() {
317 --count;
318 }
319private:
320 throws_on_move& operator=(throws_on_move const&) = delete;
321 throws_on_move& operator=(throws_on_move &&) = delete;
322};
323
324int throws_on_move::count = 0;
325
326
327#endif