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 9 #ifndef TEST_STD_UTILITIES_MEMORY_UTIL_SMARTPTR_SHARED_CREATE_TYPES_H 10 #define TEST_STD_UTILITIES_MEMORY_UTIL_SMARTPTR_SHARED_CREATE_TYPES_H 11 12 #include <cassert> 13 #include <cstddef> 14 #include <exception> 15 16 #include "test_macros.h" 17 18 struct DestroyInReverseOrder { resetDestroyInReverseOrder19 static void reset() { global_count_ = 0; } aliveDestroyInReverseOrder20 static int alive() { return global_count_; } 21 DestroyInReverseOrderDestroyInReverseOrder22 DestroyInReverseOrder() 23 : DestroyInReverseOrder(&global_count_) 24 { } 25 DestroyInReverseOrderDestroyInReverseOrder26 constexpr DestroyInReverseOrder(int* count) 27 : count_(count), value_(*count) 28 { ++*count_; } 29 DestroyInReverseOrderDestroyInReverseOrder30 constexpr DestroyInReverseOrder(DestroyInReverseOrder const& other) 31 : count_(other.count_), value_(*other.count_) 32 { ++*count_; } 33 valueDestroyInReverseOrder34 constexpr int value() const { return value_; } 35 36 // Ensure that we destroy these objects in the reverse order as they were created. ~DestroyInReverseOrderDestroyInReverseOrder37 constexpr ~DestroyInReverseOrder() { 38 --*count_; 39 assert(*count_ == value_); 40 } 41 42 private: 43 int* count_; 44 int value_; 45 static int global_count_; 46 }; 47 48 int DestroyInReverseOrder::global_count_ = 0; 49 50 struct NonMovable { 51 NonMovable() = default; 52 NonMovable(NonMovable&&) = delete; 53 }; 54 55 struct CountCopies { resetCountCopies56 static void reset() { global_count_ = 0; } copiesCountCopies57 static int copies() { return global_count_; } 58 CountCopiesCountCopies59 constexpr CountCopies() : copies_(&global_count_) { } CountCopiesCountCopies60 constexpr CountCopies(int* counter) : copies_(counter) { } CountCopiesCountCopies61 constexpr CountCopies(CountCopies const& other) : copies_(other.copies_) { ++*copies_; } 62 63 private: 64 int* copies_; 65 static int global_count_; 66 }; 67 68 int CountCopies::global_count_ = 0; 69 70 struct alignas(alignof(std::max_align_t) * 2) OverAligned { }; 71 72 struct MaxAligned { 73 std::max_align_t foo; 74 }; 75 76 #ifndef TEST_HAS_NO_EXCEPTIONS 77 struct ThrowOnConstruction { 78 struct exception : std::exception { }; 79 ThrowOnConstructionThrowOnConstruction80 ThrowOnConstruction() { on_construct(); } ThrowOnConstructionThrowOnConstruction81 ThrowOnConstruction(ThrowOnConstruction const&) { on_construct(); } 82 resetThrowOnConstruction83 static void reset() { throw_after_ = -1; } throw_afterThrowOnConstruction84 static void throw_after(int n) { throw_after_ = n; } 85 86 private: 87 static int throw_after_; on_constructThrowOnConstruction88 void on_construct() { 89 if (throw_after_ == 0) 90 throw exception{}; 91 92 if (throw_after_ != -1) 93 --throw_after_; 94 } 95 }; 96 97 int ThrowOnConstruction::throw_after_ = -1; 98 #endif // TEST_HAS_NO_EXCEPTIONS 99 100 #endif // TEST_STD_UTILITIES_MEMORY_UTIL_SMARTPTR_SHARED_CREATE_TYPES_H 101