throwing_allocator.hpp 1.7 KB

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