tomato/test/entt/common/throwing_allocator.hpp
Green Sky 5c7231b7a3 Squashed 'external/entt/entt/' content from commit fef92113
git-subtree-dir: external/entt/entt
git-subtree-split: fef921132cae7588213d0f9bcd2fb9c8ffd8b7fc
2023-07-25 11:29:51 +02:00

70 lines
1.7 KiB
C++

#ifndef ENTT_COMMON_THROWING_ALLOCATOR_HPP
#define ENTT_COMMON_THROWING_ALLOCATOR_HPP
#include <cstddef>
#include <memory>
#include <type_traits>
namespace test {
template<typename Type>
class throwing_allocator: std::allocator<Type> {
template<typename Other>
friend class throwing_allocator;
using base = std::allocator<Type>;
struct test_exception {};
public:
using value_type = Type;
using pointer = value_type *;
using const_pointer = const value_type *;
using void_pointer = void *;
using const_void_pointer = const void *;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
using exception_type = test_exception;
template<class Other>
struct rebind {
using other = throwing_allocator<Other>;
};
throwing_allocator() = default;
template<class Other>
throwing_allocator(const throwing_allocator<Other> &other)
: base{other} {}
pointer allocate(std::size_t length) {
if(trigger_on_allocate) {
trigger_on_allocate = false;
throw test_exception{};
}
trigger_on_allocate = trigger_after_allocate;
trigger_after_allocate = false;
return base::allocate(length);
}
void deallocate(pointer mem, std::size_t length) {
base::deallocate(mem, length);
}
bool operator==(const throwing_allocator<Type> &) const {
return true;
}
bool operator!=(const throwing_allocator<Type> &other) const {
return !(*this == other);
}
static inline bool trigger_on_allocate{};
static inline bool trigger_after_allocate{};
};
} // namespace test
#endif