A Hopf, Skip and a Jump
Public Types | Public Member Functions | List of all members
slidingbuffer::UniqueAllocator< T > Class Template Reference

UniqueAllocator replaces the std::allocator for the internal stuctures of SlidingBuffer. More...

#include <uniqueallocator.h>

Public Types

using value_type = T
 Type of values to allocate.
 

Public Member Functions

 UniqueAllocator () noexcept
 Constuctor.
 
template<class U >
 UniqueAllocator (UniqueAllocator< U > const &) noexcept
 
value_typeallocate (std::size_t n)
 
void deallocate (value_type *p, std::size_t n) noexcept
 
template<class P >
void delete_if_ptr (P *const &p)
 If the argument matches a pointer type, delete the object at which it points. More...
 
template<class P >
void delete_if_ptr (P const &)
 If P isn't a pointer type, ignore this call.
 
template<class U >
void destroy (U *p) noexcept
 Destroy (but don't deallocate) the collection member. More...
 

Detailed Description

template<typename T>
class slidingbuffer::UniqueAllocator< T >

UniqueAllocator replaces the std::allocator for the internal stuctures of SlidingBuffer.

Containers using this allocator can conveniently be used to hold pointers to dynamically allocated objects, so long as the only access to the objecs is uniquely through the container.

Since the pointers are stored uniquely in their particular collection, UniqueAllocator provides a method which deletes the referenced object when its pointer is deleted. This combines efficiency of access with guaranteed deallocation.

e.g., SlidingBuffer maintains a container of Segments internally. These segments are allocated dynamically, and it is their addresses which are stored. In order to maximise efficiency, the addresses are stored as raw pointers, but this might lead to memory leaks when the pointers are deallocated.

Definition at line 33 of file uniqueallocator.h.

Member Function Documentation

◆ delete_if_ptr()

template<typename T>
template<class P >
void slidingbuffer::UniqueAllocator< T >::delete_if_ptr ( P *const &  p)
inline

If the argument matches a pointer type, delete the object at which it points.

The assumption is that pointers held in the container are obtained by calling the relevant new to allocate an object dynamically. If this isn't the case, this isn't an appropriate allocator

Parameters
pPointer to object to be deleted

Definition at line 65 of file uniqueallocator.h.

◆ destroy()

template<typename T>
template<class U >
void slidingbuffer::UniqueAllocator< T >::destroy ( U *  p)
inlinenoexcept

Destroy (but don't deallocate) the collection member.

Since the collection is expected to contain pointers to objects allocated with new(), delete_if_ptr() is called to delete the object at which the collection element points. This checks the type of the collection element and will not attempt to call delete on a non-pointer type.

However, one would normally expect the referenced object to be deleted, then the pointer within the collection destroyed.

Parameters
pPointer to object to be destroyed

Definition at line 84 of file uniqueallocator.h.


The documentation for this class was generated from the following file: