A Hopf, Skip and a Jump
uniqueallocator.h
1 #ifndef _UNIQUEALLOCATOR_H_
2 #define _UNIQUEALLOCATOR_H_
3 
4 #include <memory>
5 
6 // The following template comes from
7 // https://howardhinnant.github.io/allocator_boilerplate.html
8 
9 namespace slidingbuffer {
10 
33 template <typename T> class UniqueAllocator
34 {
35 public:
36  using value_type = T;
37 
38  // not required, unless used
40  UniqueAllocator() noexcept {}
41 
42  template <class U> UniqueAllocator(UniqueAllocator<U> const&) noexcept {}
43 
44  // Use pointer if pointer is not a value_type*
45  value_type* allocate(std::size_t n)
46  {
47  return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
48  }
49 
50  // Use pointer if pointer is not a value_type*
51  void deallocate(value_type* p, std::size_t n) noexcept
52  {
53  ::operator delete(p);
54  }
55 
65  template <class P> inline void delete_if_ptr (P* const& p) { delete(p); }
66 
68  template <class P> inline void delete_if_ptr (P const&) { }
69 
84  template <class U> void destroy(U* p) noexcept
85  {
86  delete_if_ptr(*p);
87  p->~U();
88  }
89 };
90 
92 template <class T, class U> bool
93 operator==(UniqueAllocator<T> const&, UniqueAllocator<U> const&) noexcept
94 {
95  return true;
96 };
97 
99 template <class T, class U> bool
100 operator!=(UniqueAllocator<T> const& x, UniqueAllocator<U> const& y) noexcept
101 {
102  return !(x == y);
103 };
104 
105 } // end namespace slidingbuffer
106 
107 #endif
UniqueAllocator replaces the std::allocator for the internal stuctures of SlidingBuffer.
UniqueAllocator() noexcept
Constuctor.
T value_type
Type of values to allocate.
void delete_if_ptr(P *const &p)
If the argument matches a pointer type, delete the object at which it points.
void destroy(U *p) noexcept
Destroy (but don&#39;t deallocate) the collection member.
void delete_if_ptr(P const &)
If P isn&#39;t a pointer type, ignore this call.