1 #ifndef ENTT_ENTITY_THROWING_ALLOCATOR_HPP 2 #define ENTT_ENTITY_THROWING_ALLOCATOR_HPP 3 4 5 #include <cstddef> 6 #include <memory> 7 #include <type_traits> 8 9 10 namespace test { 11 12 13 template<typename Type> 14 class throwing_allocator { 15 template<typename Other> 16 friend class throwing_allocator; 17 18 struct test_exception {}; 19 20 public: 21 using value_type = Type; 22 using pointer = value_type *; 23 using const_pointer = const value_type *; 24 using void_pointer = void *; 25 using const_void_pointer = const void *; 26 using propagate_on_container_move_assignment = std::true_type; 27 using exception_type = test_exception; 28 29 throwing_allocator() = default; 30 31 template<class Other> throwing_allocator(const throwing_allocator<Other> & other)32 throwing_allocator(const throwing_allocator<Other> &other) 33 : allocator{other.allocator} 34 {} 35 allocate(std::size_t length)36 pointer allocate(std::size_t length) { 37 if(trigger_on_allocate) { 38 trigger_on_allocate = false; 39 throw test_exception{}; 40 } 41 42 trigger_on_allocate = trigger_after_allocate; 43 trigger_after_allocate = false; 44 45 return allocator.allocate(length); 46 } 47 deallocate(pointer mem,std::size_t length)48 void deallocate(pointer mem, std::size_t length) { 49 allocator.deallocate(mem, length); 50 } 51 52 static inline bool trigger_on_allocate{}; 53 static inline bool trigger_after_allocate{}; 54 55 private: 56 std::allocator<Type> allocator; 57 }; 58 59 60 } 61 62 63 #endif 64