Zserio C++ runtime library  1.3.0
Built for Zserio 2.18.0
PolymorphicAllocator.h
Go to the documentation of this file.
1 #ifndef ZSERIO_PMR_POLYMORPHIC_ALLOCATOR_H_INC
2 #define ZSERIO_PMR_POLYMORPHIC_ALLOCATOR_H_INC
3 
4 #include <limits>
5 #include <type_traits>
6 #include <utility>
7 
8 #include "zserio/Types.h"
10 
11 namespace zserio
12 {
13 namespace pmr
14 {
15 namespace detail
16 {
17 
21 template <class T = uint8_t>
22 class PolymorphicAllocatorBase
23 {
24 public:
25  using value_type = T;
26 
27  // Following typedefs are only present for compatibility with older std. libraries, that does not fully
28  // conform to C++11. [old-compiler-support]
29  using pointer = value_type*;
30  using const_pointer = const value_type*;
31  using size_type = size_t;
32  using difference_type = ptrdiff_t;
33  using reference = value_type&;
34  using const_reference = const value_type&;
35 
46  PolymorphicAllocatorBase(MemoryResource* resource = getDefaultResource()) noexcept :
47  m_resource(resource != nullptr ? resource : getDefaultResource()) // non-standard extension
48  {}
49 
54  ~PolymorphicAllocatorBase() = default;
55 
56  PolymorphicAllocatorBase(const PolymorphicAllocatorBase& other) noexcept = default;
57  PolymorphicAllocatorBase& operator=(const PolymorphicAllocatorBase& other) noexcept = default;
58 
59  PolymorphicAllocatorBase(PolymorphicAllocatorBase&& other) noexcept = default;
60  PolymorphicAllocatorBase& operator=(PolymorphicAllocatorBase&& other) noexcept = default;
70  template <class U>
71  PolymorphicAllocatorBase(const PolymorphicAllocatorBase<U>& other) noexcept :
72  m_resource(other.resource())
73  {}
74 
80  template <class U>
81  PolymorphicAllocatorBase& operator=(const PolymorphicAllocatorBase<U>& other) noexcept
82  {
83  m_resource = other.resource();
84  return *this;
85  }
86 
92  value_type* allocate(size_t size)
93  {
94  return static_cast<value_type*>(m_resource->allocate(size * sizeof(value_type), alignof(value_type)));
95  }
96 
104  void deallocate(value_type* memory, size_t size) noexcept
105  {
106  m_resource->deallocate(memory, size * sizeof(value_type), alignof(value_type));
107  }
108 
112  MemoryResource* resource() const noexcept
113  {
114  return m_resource;
115  }
116 
125  template <typename U, typename... Args>
126  void construct(U* ptr, Args&&... args) noexcept(
127  noexcept(new (static_cast<void*>(ptr)) U(std::forward<Args>(args)...)))
128  {
129  new (static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
130  }
131 
139  template <typename U>
140  void destroy(U* ptr) noexcept(noexcept(ptr->~U()))
141  {
142  ptr->~U();
143  }
144 
152  size_type max_size() const noexcept
153  {
154  return std::numeric_limits<size_type>::max() / sizeof(value_type);
155  }
156 
157 private:
158  MemoryResource* m_resource;
159 };
160 
161 template <class T, class U>
162 bool operator==(const PolymorphicAllocatorBase<T>& lhs, const PolymorphicAllocatorBase<U>& rhs) noexcept
163 {
164  return *lhs.resource() == *rhs.resource();
165 }
166 
167 template <class T, class U>
168 bool operator!=(const PolymorphicAllocatorBase<T>& lhs, const PolymorphicAllocatorBase<U>& rhs) noexcept
169 {
170  return !(lhs == rhs);
171 }
172 
173 } // namespace detail
174 
179 template <class T = uint8_t>
180 class PolymorphicAllocator : public detail::PolymorphicAllocatorBase<T>
181 {
182 public:
183  using detail::PolymorphicAllocatorBase<T>::PolymorphicAllocatorBase;
184 
185  using propagate_on_container_copy_assignment = std::false_type;
186  using propagate_on_container_move_assignment = std::false_type;
187  using propagate_on_container_swap = std::false_type;
188 
193  {
194  return PolymorphicAllocator();
195  }
196 
202  template <typename U>
203  struct rebind
204  {
206  };
207 };
208 
212 template <class T = uint8_t>
213 class PropagatingPolymorphicAllocator : public detail::PolymorphicAllocatorBase<T>
214 {
215 public:
216  using detail::PolymorphicAllocatorBase<T>::PolymorphicAllocatorBase;
217 
220  using propagate_on_container_swap = std::true_type;
221 
226  {
227  return *this;
228  }
229 
235  template <typename U>
236  struct rebind
237  {
239  };
240 };
241 
242 } // namespace pmr
243 } // namespace zserio
244 
245 #endif // ZSERIO_PMR_POLYMORPHIC_ALLOCATOR_H_INC
std::false_type propagate_on_container_copy_assignment
PolymorphicAllocator select_on_container_copy_construction() const
std::false_type propagate_on_container_move_assignment
PropagatingPolymorphicAllocator select_on_container_copy_construction() const
bool operator==(const MemoryResource &lhs, const MemoryResource &rhs)
MemoryResource * getDefaultResource() noexcept
bool operator!=(const MemoryResource &lhs, const MemoryResource &rhs)