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