1 #ifndef ZSERIO_PMR_POLYMORPHIC_ALLOCATOR_H_INC
2 #define ZSERIO_PMR_POLYMORPHIC_ALLOCATOR_H_INC
22 template <
class T = u
int8_t>
23 class PolymorphicAllocatorBase
30 using pointer = value_type*;
31 using const_pointer =
const value_type*;
32 using size_type = std::size_t;
33 using difference_type = std::ptrdiff_t;
34 using reference = value_type&;
35 using const_reference =
const value_type&;
55 ~PolymorphicAllocatorBase() =
default;
57 PolymorphicAllocatorBase(
const PolymorphicAllocatorBase& other) noexcept =
default;
58 PolymorphicAllocatorBase& operator=(
const PolymorphicAllocatorBase& other) noexcept =
default;
60 PolymorphicAllocatorBase(PolymorphicAllocatorBase&& other) noexcept =
default;
61 PolymorphicAllocatorBase& operator=(PolymorphicAllocatorBase&& other) noexcept =
default;
72 PolymorphicAllocatorBase(
const PolymorphicAllocatorBase<U>& other) noexcept :
73 m_resource(other.resource())
82 PolymorphicAllocatorBase& operator=(
const PolymorphicAllocatorBase<U>& other) noexcept
84 m_resource = other.resource();
93 value_type* allocate(std::size_t size)
95 return static_cast<value_type*
>(m_resource->allocate(size *
sizeof(value_type),
alignof(value_type)));
105 void deallocate(value_type* memory, std::size_t size) noexcept
107 m_resource->deallocate(memory, size *
sizeof(value_type),
alignof(value_type));
113 MemoryResource* resource() const noexcept
126 template <
typename U,
typename... Args>
127 void construct(U* ptr, Args&&... args) noexcept(
128 noexcept(
new (
static_cast<void*
>(ptr)) U(std::forward<Args>(args)...)))
130 new (
static_cast<void*
>(ptr)) U(std::forward<Args>(args)...);
140 template <
typename U>
141 void destroy(U* ptr) noexcept(noexcept(ptr->~U()))
153 size_type max_size() const noexcept
155 return std::numeric_limits<size_type>::max() /
sizeof(value_type);
159 MemoryResource* m_resource;
162 template <
class T,
class U>
163 bool operator==(
const PolymorphicAllocatorBase<T>& lhs,
const PolymorphicAllocatorBase<U>& rhs) noexcept
165 return *lhs.resource() == *rhs.resource();
168 template <
class T,
class U>
169 bool operator!=(
const PolymorphicAllocatorBase<T>& lhs,
const PolymorphicAllocatorBase<U>& rhs) noexcept
171 return !(lhs == rhs);
180 template <
class T = u
int8_t>
184 using detail::PolymorphicAllocatorBase<T>::PolymorphicAllocatorBase;
203 template <
typename U>
213 template <
class T = u
int8_t>
217 using detail::PolymorphicAllocatorBase<T>::PolymorphicAllocatorBase;
236 template <
typename U>
std::false_type propagate_on_container_copy_assignment
PolymorphicAllocator select_on_container_copy_construction() const
std::false_type propagate_on_container_move_assignment
std::false_type propagate_on_container_swap
PropagatingPolymorphicAllocator select_on_container_copy_construction() const
std::true_type propagate_on_container_move_assignment
std::true_type propagate_on_container_copy_assignment
std::true_type propagate_on_container_swap
bool operator==(const MemoryResource &lhs, const MemoryResource &rhs)
MemoryResource * getDefaultResource() noexcept
bool operator!=(const MemoryResource &lhs, const MemoryResource &rhs)