Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
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 
7 namespace zserio
8 {
9 namespace ppmr
10 {
11 
15 template <class T>
17 {
18 public:
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>
63  m_resource(other.resource())
64  {}
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 
141  {
142  return *this;
143  }
144 
148  std::pmr::memory_resource* resource() const noexcept
149  {
150  return m_resource;
151  }
152 
153 private:
154  std::pmr::memory_resource* m_resource;
155 };
156 
157 template <class T, class U>
160 {
161  return *lhs.resource() == *rhs.resource();
162 }
163 
164 template <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 & operator=(PropagatingPolymorphicAllocator &&other) noexcept=default
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 &other) noexcept=default
std::pmr::memory_resource * resource() const noexcept
PropagatingPolymorphicAllocator(PropagatingPolymorphicAllocator &&other) noexcept=default
PropagatingPolymorphicAllocator & operator=(const PropagatingPolymorphicAllocator< U > &other) noexcept
void deallocate(value_type *memory, std::size_t size) noexcept
Definition: BitBuffer.h:602
bool operator==(const PropagatingPolymorphicAllocator< T > &lhs, const PropagatingPolymorphicAllocator< U > &rhs) noexcept
bool operator!=(const PropagatingPolymorphicAllocator< T > &lhs, const PropagatingPolymorphicAllocator< U > &rhs) noexcept