1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 #ifndef EXPERIMENTAL_ANY_HELPERS_H
9 #define EXPERIMENTAL_ANY_HELPERS_H
11 #include <experimental/any>
13 #include <type_traits>
16 #include "test_macros.h"
20 : public std::integral_constant
<bool
21 , sizeof(T
) <= (sizeof(void*)*3)
22 && std::alignment_of
<void*>::value
23 % std::alignment_of
<T
>::value
== 0
24 && std::is_nothrow_move_constructible
<T
>::value
29 // Return 'true' if 'Type' will be considered a small type by 'any'
32 #if defined(_LIBCPP_VERSION)
33 return std::experimental::__any_imp::_IsSmallObject
<Type
>::value
;
35 return IsSmallObject
<Type
>::value
;
40 // Assert that an object is empty. If the object used to contain an object
41 // of type 'LastType' check that it can no longer be accessed.
42 template <class LastType
= int>
43 void assertEmpty(std::experimental::any
const& a
) {
45 RTTI_ASSERT(a
.type() == typeid(void));
46 assert(std::experimental::any_cast
<LastType
const>(&a
) == nullptr);
49 // Assert that an 'any' object stores the specified 'Type' and 'value'.
51 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
52 void assertContains(std::experimental::any
const& a
, int value
= 1) {
54 RTTI_ASSERT(a
.type() == typeid(Type
));
55 assert(std::experimental::any_cast
<Type
const &>(a
).value
== value
);
58 // Modify the value of a "test type" stored within an any to the specified
61 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
62 void modifyValue(std::experimental::any
& a
, int value
) {
64 RTTI_ASSERT(a
.type() == typeid(Type
));
65 std::experimental::any_cast
<Type
&>(a
).value
= value
;
68 // A test type that will trigger the small object optimization within 'any'.
69 template <int Dummy
= 0>
75 static int const_copied
;
76 static int non_const_copied
;
79 small_type::copied
= 0;
80 small_type::moved
= 0;
81 small_type::const_copied
= 0;
82 small_type::non_const_copied
= 0;
87 explicit small_type(int val
) : value(val
) {
91 small_type(small_type
const & other
) throw() {
98 small_type(small_type
& other
) throw() {
105 small_type(small_type
&& other
) throw() {
118 small_type
& operator=(small_type
const&) = delete;
119 small_type
& operator=(small_type
&&) = delete;
123 int small_type
<Dummy
>::count
= 0;
126 int small_type
<Dummy
>::copied
= 0;
129 int small_type
<Dummy
>::moved
= 0;
132 int small_type
<Dummy
>::const_copied
= 0;
135 int small_type
<Dummy
>::non_const_copied
= 0;
137 typedef small_type
<> small
;
138 typedef small_type
<1> small1
;
139 typedef small_type
<2> small2
;
142 // A test type that will NOT trigger the small object optimization in any.
143 template <int Dummy
= 0>
149 static int const_copied
;
150 static int non_const_copied
;
152 static void reset() {
153 large_type::copied
= 0;
154 large_type::moved
= 0;
155 large_type::const_copied
= 0;
156 large_type::non_const_copied
= 0;
161 large_type(int val
) : value(val
) {
166 large_type(large_type
const & other
) {
173 large_type(large_type
& other
) {
180 large_type(large_type
&& other
) {
193 large_type
& operator=(large_type
const&) = delete;
194 large_type
& operator=(large_type
&&) = delete;
199 int large_type
<Dummy
>::count
= 0;
202 int large_type
<Dummy
>::copied
= 0;
205 int large_type
<Dummy
>::moved
= 0;
208 int large_type
<Dummy
>::const_copied
= 0;
211 int large_type
<Dummy
>::non_const_copied
= 0;
213 typedef large_type
<> large
;
214 typedef large_type
<1> large1
;
215 typedef large_type
<2> large2
;
217 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
218 // and 'throws_on_move'.
219 struct my_any_exception
{};
221 void throwMyAnyExpression() {
222 #if !defined(TEST_HAS_NO_EXCEPTIONS)
223 throw my_any_exception();
225 assert(false && "Exceptions are disabled");
229 // A test type that will trigger the small object optimization within 'any'.
230 // this type throws if it is copied.
231 struct small_throws_on_copy
236 explicit small_throws_on_copy(int val
= 0) : value(val
) {
240 small_throws_on_copy(small_throws_on_copy
const &) {
241 throwMyAnyExpression();
244 small_throws_on_copy(small_throws_on_copy
&& other
) throw() {
249 ~small_throws_on_copy() {
253 small_throws_on_copy
& operator=(small_throws_on_copy
const&) = delete;
254 small_throws_on_copy
& operator=(small_throws_on_copy
&&) = delete;
257 int small_throws_on_copy::count
= 0;
259 // A test type that will NOT trigger the small object optimization within 'any'.
260 // this type throws if it is copied.
261 struct large_throws_on_copy
266 explicit large_throws_on_copy(int val
= 0) : value(val
) {
271 large_throws_on_copy(large_throws_on_copy
const &) {
272 throwMyAnyExpression();
275 large_throws_on_copy(large_throws_on_copy
&& other
) throw() {
280 ~large_throws_on_copy() {
285 large_throws_on_copy
& operator=(large_throws_on_copy
const&) = delete;
286 large_throws_on_copy
& operator=(large_throws_on_copy
&&) = delete;
290 int large_throws_on_copy::count
= 0;
292 // A test type that throws when it is moved. This object will NOT trigger
293 // the small object optimization in 'any'.
294 struct throws_on_move
299 explicit throws_on_move(int val
= 0) : value(val
) { ++count
; }
301 throws_on_move(throws_on_move
const & other
) {
306 throws_on_move(throws_on_move
&&) {
307 throwMyAnyExpression();
314 throws_on_move
& operator=(throws_on_move
const&) = delete;
315 throws_on_move
& operator=(throws_on_move
&&) = delete;
318 int throws_on_move::count
= 0;