throwing_allocator.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef ENTT_COMMON_THROWING_ALLOCATOR_HPP
  2. #define ENTT_COMMON_THROWING_ALLOCATOR_HPP
  3. #include <cstddef>
  4. #include <limits>
  5. #include <memory>
  6. #include <type_traits>
  7. #include <entt/container/dense_map.hpp>
  8. #include <entt/core/fwd.hpp>
  9. #include <entt/core/type_info.hpp>
  10. namespace test {
  11. struct throwing_allocator_exception {};
  12. template<typename Type>
  13. class throwing_allocator {
  14. template<typename Other>
  15. friend class throwing_allocator;
  16. public:
  17. using value_type = Type;
  18. using pointer = value_type *;
  19. using const_pointer = const value_type *;
  20. using void_pointer = void *;
  21. using const_void_pointer = const void *;
  22. using propagate_on_container_move_assignment = std::true_type;
  23. using propagate_on_container_swap = std::true_type;
  24. using container_type = entt::dense_map<entt::id_type, std::size_t>;
  25. template<typename Other>
  26. struct rebind {
  27. using other = throwing_allocator<Other>;
  28. };
  29. throwing_allocator()
  30. : allocator{},
  31. config{std::allocate_shared<container_type>(allocator)} {}
  32. template<typename Other>
  33. throwing_allocator(const throwing_allocator<Other> &other)
  34. : allocator{other.allocator},
  35. config{other.config} {}
  36. pointer allocate(std::size_t length) {
  37. if(const auto hash = entt::type_id<Type>().hash(); config->contains(hash)) {
  38. if(auto &elem = (*config)[hash]; elem == 0u) {
  39. config->erase(hash);
  40. throw throwing_allocator_exception{};
  41. } else {
  42. --elem;
  43. }
  44. }
  45. return allocator.allocate(length);
  46. }
  47. void deallocate(pointer mem, std::size_t length) {
  48. allocator.deallocate(mem, length);
  49. }
  50. template<typename Other>
  51. void throw_counter(const std::size_t len) {
  52. (*config)[entt::type_id<Other>().hash()] = len;
  53. }
  54. bool operator==(const throwing_allocator<Type> &) const {
  55. return true;
  56. }
  57. bool operator!=(const throwing_allocator<Type> &other) const {
  58. return !(*this == other);
  59. }
  60. private:
  61. std::allocator<Type> allocator;
  62. std::shared_ptr<container_type> config;
  63. };
  64. } // namespace test
  65. #endif