Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
PropagatingPolymorphicAllocator.h
Go to the documentation of this file.
1#ifndef ZSERIO_PPMR_PROPAGATING_POLYMORPHIC_ALLOCATOR_H_INC
2#define ZSERIO_PPMR_PROPAGATING_POLYMORPHIC_ALLOCATOR_H_INC
3
4#include <cstddef>
5#include <memory_resource>
6
7namespace zserio
8{
9namespace ppmr
10{
11
15template <class T>
17{
18public:
19 using value_type = T;
20
23 using propagate_on_container_swap = std::true_type;
24
36 std::pmr::memory_resource* resource = std::pmr::get_default_resource()) noexcept :
37 m_resource(
38 resource != nullptr ? resource : std::pmr::get_default_resource()) // non-standard extension
39 {}
40
46
49
61 template <class U>
65
71 template <class U>
73 {
74 m_resource = other.resource();
75 return *this;
76 }
82 value_type* allocate(std::size_t size)
83 {
84 return static_cast<value_type*>(m_resource->allocate(size * sizeof(value_type), alignof(value_type)));
85 }
86
94 void deallocate(value_type* memory, std::size_t size) noexcept
95 {
96 m_resource->deallocate(memory, size * sizeof(value_type), alignof(value_type));
97 }
98
105 template <typename U, typename... Args>
106 void construct(U* ptr, Args&&... args)
107 {
108 using Self = decltype(*this);
109 if constexpr (std::uses_allocator_v<std::remove_cv_t<U>, Self>)
110 {
111 if constexpr (std::is_constructible_v<U, std::allocator_arg_t, Self, Args...>)
112 {
113 new (static_cast<void*>(ptr)) U(std::allocator_arg, *this, std::forward<Args>(args)...);
114 }
115 else
116 {
117 new (static_cast<void*>(ptr)) U(std::forward<Args>(args)..., *this);
118 }
119 }
120 else
121 {
122 new (static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
123 }
124 }
125
131 template <typename U>
132 void destroy(U* ptr) noexcept(noexcept(ptr->~U()))
133 {
134 ptr->~U();
135 }
136
144
148 std::pmr::memory_resource* resource() const noexcept
149 {
150 return m_resource;
151 }
152
153private:
154 std::pmr::memory_resource* m_resource;
155};
156
157template <class T, class U>
160{
161 return *lhs.resource() == *rhs.resource();
162}
163
164template <class T, class U>
167{
168 return !(lhs == rhs);
169}
170
171} // namespace ppmr
172} // namespace zserio
173
174#endif // ZSERIO_PPMR_PROPAGATING_POLYMORPHIC_ALLOCATOR_H_INC
PropagatingPolymorphicAllocator(const PropagatingPolymorphicAllocator &other) noexcept=default
PropagatingPolymorphicAllocator(const PropagatingPolymorphicAllocator< U > &other) noexcept
PropagatingPolymorphicAllocator select_on_container_copy_construction() const
void destroy(U *ptr) noexcept(noexcept(ptr->~U()))
PropagatingPolymorphicAllocator(std::pmr::memory_resource *resource=std::pmr::get_default_resource()) noexcept
PropagatingPolymorphicAllocator & operator=(const PropagatingPolymorphicAllocator< U > &other) noexcept
PropagatingPolymorphicAllocator(PropagatingPolymorphicAllocator &&other) noexcept=default
PropagatingPolymorphicAllocator & operator=(PropagatingPolymorphicAllocator &&other) noexcept=default
PropagatingPolymorphicAllocator & operator=(const PropagatingPolymorphicAllocator &other) noexcept=default
std::pmr::memory_resource * resource() const noexcept
void deallocate(value_type *memory, std::size_t size) noexcept
bool operator==(const PropagatingPolymorphicAllocator< T > &lhs, const PropagatingPolymorphicAllocator< U > &rhs) noexcept
bool operator!=(const PropagatingPolymorphicAllocator< T > &lhs, const PropagatingPolymorphicAllocator< U > &rhs) noexcept