425 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef ANY_HELPERS_H10#define ANY_HELPERS_H11 12#include <cassert>13#include <typeinfo>14#include <type_traits>15#include <utility>16 17namespace std { namespace experimental {} }18 19#include "test_macros.h"20#include "type_id.h"21 22#if !defined(TEST_HAS_NO_RTTI)23#define RTTI_ASSERT(X) assert(X)24#else25#define RTTI_ASSERT(X)26#endif27 28template <class T>29 struct IsSmallObject30 : public std::integral_constant<bool31 , sizeof(T) <= sizeof(std::any) - sizeof(void*)32 && std::alignment_of<void*>::value33 % std::alignment_of<T>::value == 034 && std::is_nothrow_move_constructible<T>::value35 >36 {};37 38template <class T>39bool containsType(std::any const& a) {40#if !defined(TEST_HAS_NO_RTTI)41 return a.type() == typeid(T);42#else43 return a.has_value() && std::any_cast<T>(&a) != nullptr;44#endif45}46 47// Return 'true' if 'Type' will be considered a small type by 'any'48template <class Type>49bool isSmallType() {50 return IsSmallObject<Type>::value;51}52 53// Assert that an object is empty. If the object used to contain an object54// of type 'LastType' check that it can no longer be accessed.55template <class LastType = int>56void assertEmpty(std::any const& a) {57 assert(!a.has_value());58 RTTI_ASSERT(a.type() == typeid(void));59 assert(std::any_cast<LastType const>(&a) == nullptr);60}61 62template <class Type>63constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)64{ return true; }65template <class> constexpr bool has_value_member(long) { return false; }66 67 68// Assert that an 'any' object stores the specified 'Type' and 'value'.69template <class Type>70std::enable_if_t<has_value_member<Type>(0)> 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 76template <class Type, class Value>77std::enable_if_t<!has_value_member<Type>(0)> assertContains(std::any const& a, Value value) {78 assert(a.has_value());79 assert(containsType<Type>(a));80 assert(std::any_cast<Type const&>(a) == value);81}82 83// Modify the value of a "test type" stored within an any to the specified84// 'value'.85template <class Type>86void modifyValue(std::any& a, int value) {87 assert(a.has_value());88 assert(containsType<Type>(a));89 std::any_cast<Type&>(a).value = value;90}91 92// A test type that will trigger the small object optimization within 'any'.93template <int Dummy = 0>94struct small_type95{96 static int count;97 static int copied;98 static int moved;99 static int const_copied;100 static int non_const_copied;101 102 static void reset() {103 small_type::copied = 0;104 small_type::moved = 0;105 small_type::const_copied = 0;106 small_type::non_const_copied = 0;107 }108 109 int value;110 111 explicit small_type(int val = 0) : value(val) {112 ++count;113 }114 explicit small_type(int, int val, int) : value(val) {115 ++count;116 }117 small_type(std::initializer_list<int> il) : value(*il.begin()) {118 ++count;119 }120 121 small_type(small_type const & other) noexcept {122 value = other.value;123 ++count;124 ++copied;125 ++const_copied;126 }127 128 small_type(small_type& other) noexcept {129 value = other.value;130 ++count;131 ++copied;132 ++non_const_copied;133 }134 135 small_type(small_type && other) noexcept {136 value = other.value;137 other.value = 0;138 ++count;139 ++moved;140 }141 142 ~small_type() {143 value = -1;144 --count;145 }146 147private:148 small_type& operator=(small_type const&) = delete;149 small_type& operator=(small_type&&) = delete;150};151 152template <int Dummy>153int small_type<Dummy>::count = 0;154 155template <int Dummy>156int small_type<Dummy>::copied = 0;157 158template <int Dummy>159int small_type<Dummy>::moved = 0;160 161template <int Dummy>162int small_type<Dummy>::const_copied = 0;163 164template <int Dummy>165int small_type<Dummy>::non_const_copied = 0;166 167typedef small_type<> small;168typedef small_type<1> small1;169typedef small_type<2> small2;170 171 172// A test type that will NOT trigger the small object optimization in any.173template <int Dummy = 0>174struct large_type175{176 static int count;177 static int copied;178 static int moved;179 static int const_copied;180 static int non_const_copied;181 182 static void reset() {183 large_type::copied = 0;184 large_type::moved = 0;185 large_type::const_copied = 0;186 large_type::non_const_copied = 0;187 }188 189 int value;190 191 large_type(int val = 0) : value(val) {192 ++count;193 data[0] = 0;194 }195 large_type(int, int val, int) : value(val) {196 ++count;197 data[0] = 0;198 }199 large_type(std::initializer_list<int> il) : value(*il.begin()) {200 ++count;201 }202 large_type(large_type const & other) {203 value = other.value;204 ++count;205 ++copied;206 ++const_copied;207 }208 209 large_type(large_type & other) {210 value = other.value;211 ++count;212 ++copied;213 ++non_const_copied;214 }215 216 large_type(large_type && other) {217 value = other.value;218 other.value = 0;219 ++count;220 ++moved;221 }222 223 ~large_type() {224 value = 0;225 --count;226 }227 228private:229 large_type& operator=(large_type const&) = delete;230 large_type& operator=(large_type &&) = delete;231 int data[10];232};233 234template <int Dummy>235int large_type<Dummy>::count = 0;236 237template <int Dummy>238int large_type<Dummy>::copied = 0;239 240template <int Dummy>241int large_type<Dummy>::moved = 0;242 243template <int Dummy>244int large_type<Dummy>::const_copied = 0;245 246template <int Dummy>247int large_type<Dummy>::non_const_copied = 0;248 249typedef large_type<> large;250typedef large_type<1> large1;251typedef large_type<2> large2;252 253// The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'254// and 'throws_on_move'.255struct my_any_exception {};256 257void throwMyAnyExpression() {258#if !defined(TEST_HAS_NO_EXCEPTIONS)259 throw my_any_exception();260#else261 assert(false && "Exceptions are disabled");262#endif263}264 265// A test type that will trigger the small object optimization within 'any'.266// this type throws if it is copied.267struct small_throws_on_copy268{269 static int count;270 static int copied;271 static int moved;272 static void reset() { count = copied = moved = 0; }273 int value;274 275 explicit small_throws_on_copy(int val = 0) : value(val) {276 ++count;277 }278 explicit small_throws_on_copy(int, int val, int) : value(val) {279 ++count;280 }281 small_throws_on_copy(small_throws_on_copy const &) {282 throwMyAnyExpression();283 }284 285 small_throws_on_copy(small_throws_on_copy && other) throw() {286 value = other.value;287 ++count; ++moved;288 }289 290 ~small_throws_on_copy() {291 --count;292 }293private:294 small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;295 small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;296};297 298int small_throws_on_copy::count = 0;299int small_throws_on_copy::copied = 0;300int small_throws_on_copy::moved = 0;301 302 303// A test type that will NOT trigger the small object optimization within 'any'.304// this type throws if it is copied.305struct large_throws_on_copy306{307 static int count;308 static int copied;309 static int moved;310 static void reset() { count = copied = moved = 0; }311 int value = 0;312 313 explicit large_throws_on_copy(int val = 0) : value(val) {314 data[0] = 0;315 ++count;316 }317 explicit large_throws_on_copy(int, int val, int) : value(val) {318 data[0] = 0;319 ++count;320 }321 large_throws_on_copy(large_throws_on_copy const &) {322 throwMyAnyExpression();323 }324 325 large_throws_on_copy(large_throws_on_copy && other) throw() {326 value = other.value;327 ++count; ++moved;328 }329 330 ~large_throws_on_copy() {331 --count;332 }333 334private:335 large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;336 large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;337 int data[10];338};339 340int large_throws_on_copy::count = 0;341int large_throws_on_copy::copied = 0;342int large_throws_on_copy::moved = 0;343 344// A test type that throws when it is moved. This object will NOT trigger345// the small object optimization in 'any'.346struct throws_on_move347{348 static int count;349 static int copied;350 static int moved;351 static void reset() { count = copied = moved = 0; }352 int value;353 354 explicit throws_on_move(int val = 0) : value(val) { ++count; }355 explicit throws_on_move(int, int val, int) : value(val) { ++count; }356 throws_on_move(throws_on_move const & other) {357 value = other.value;358 ++count; ++copied;359 }360 361 throws_on_move(throws_on_move &&) {362 throwMyAnyExpression();363 }364 365 ~throws_on_move() {366 --count;367 }368private:369 throws_on_move& operator=(throws_on_move const&) = delete;370 throws_on_move& operator=(throws_on_move &&) = delete;371};372 373int throws_on_move::count = 0;374int throws_on_move::copied = 0;375int throws_on_move::moved = 0;376 377struct small_tracked_t {378 small_tracked_t()379 : arg_types(&makeArgumentID<>()) {}380 small_tracked_t(small_tracked_t const&) noexcept381 : arg_types(&makeArgumentID<small_tracked_t const&>()) {}382 small_tracked_t(small_tracked_t &&) noexcept383 : arg_types(&makeArgumentID<small_tracked_t &&>()) {}384 template <class ...Args>385 explicit small_tracked_t(Args&&...)386 : arg_types(&makeArgumentID<Args...>()) {}387 template <class ...Args>388 explicit small_tracked_t(std::initializer_list<int>, Args&&...)389 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}390 391 TypeID const* arg_types;392};393static_assert(IsSmallObject<small_tracked_t>::value, "must be small");394 395struct large_tracked_t {396 large_tracked_t()397 : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }398 large_tracked_t(large_tracked_t const&) noexcept399 : arg_types(&makeArgumentID<large_tracked_t const&>()) {}400 large_tracked_t(large_tracked_t &&) noexcept401 : arg_types(&makeArgumentID<large_tracked_t &&>()) {}402 template <class ...Args>403 explicit large_tracked_t(Args&&...)404 : arg_types(&makeArgumentID<Args...>()) {}405 template <class ...Args>406 explicit large_tracked_t(std::initializer_list<int>, Args&&...)407 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}408 409 TypeID const* arg_types;410 int dummy[sizeof(std::any) / sizeof(int) + 1];411};412 413static_assert(!IsSmallObject<large_tracked_t>::value, "must not be small");414 415 416template <class Type, class ...Args>417void assertArgsMatch(std::any const& a) {418 assert(a.has_value());419 assert(containsType<Type>(a));420 assert(std::any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());421};422 423 424#endif425