Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::PolymorphicAllocator< T > Class Template Reference

Polymorphic allocator wrapping a non-owning memory resource. Allocation strategy depends on the memory resource implementation. More...

#include <flow_polymorphic_allocator.h>

Public Types

using value_type = T

Public Member Functions

 PolymorphicAllocator () noexcept
 Default constructor using the default memory resource.
 PolymorphicAllocator (MemoryResource &resource) noexcept
 Construct allocator with a specific memory resource.
template<typename U>
 PolymorphicAllocator (const PolymorphicAllocator< U > &allocator) noexcept
 Copy constructor from another polymorphic allocator.
T * allocate (std::size_t count)
 Allocate memory for count objects.
void deallocate (T *address, std::size_t count) noexcept
 Deallocate memory.
template<typename U, typename... Args>
void construct (U *address, Args &&... args)
 Construct an object in place at the given address.
template<typename U>
void destroy (U *address) noexcept
 Destroy the object at the given address.

Private Attributes

MemoryResourceresource_

Friends

template<typename U>
class PolymorphicAllocator
 Non-owning pointer to memory resource.
template<typename U>
bool operator== (const PolymorphicAllocator< T > &lhs, const PolymorphicAllocator< U > &rhs) noexcept
template<typename U>
bool operator!= (const PolymorphicAllocator< T > &lhs, const PolymorphicAllocator< U > &rhs) noexcept

Detailed Description

template<typename T = std::byte>
class flow::PolymorphicAllocator< T >

Polymorphic allocator wrapping a non-owning memory resource. Allocation strategy depends on the memory resource implementation.

Definition at line 10 of file flow_polymorphic_allocator.h.

Member Typedef Documentation

◆ value_type

template<typename T = std::byte>
using flow::PolymorphicAllocator< T >::value_type = T

Definition at line 12 of file flow_polymorphic_allocator.h.

Constructor & Destructor Documentation

◆ PolymorphicAllocator() [1/3]

template<typename T = std::byte>
flow::PolymorphicAllocator< T >::PolymorphicAllocator ( )
inlinenoexcept

Default constructor using the default memory resource.

Definition at line 15 of file flow_polymorphic_allocator.h.

17 }
Polymorphic allocator wrapping a non-owning memory resource. Allocation strategy depends on the memor...

References resource_.

◆ PolymorphicAllocator() [2/3]

template<typename T = std::byte>
flow::PolymorphicAllocator< T >::PolymorphicAllocator ( MemoryResource & resource)
inlineexplicitnoexcept

Construct allocator with a specific memory resource.

Parameters
resourceMemory resource to use.

Definition at line 21 of file flow_polymorphic_allocator.h.

23 }

◆ PolymorphicAllocator() [3/3]

template<typename T = std::byte>
template<typename U>
flow::PolymorphicAllocator< T >::PolymorphicAllocator ( const PolymorphicAllocator< U > & allocator)
inlinenoexcept

Copy constructor from another polymorphic allocator.

Parameters
allocatorAnother allocator to copy from.

Definition at line 28 of file flow_polymorphic_allocator.h.

References PolymorphicAllocator.

Member Function Documentation

◆ allocate()

template<typename T = std::byte>
T * flow::PolymorphicAllocator< T >::allocate ( std::size_t count)
inline

Allocate memory for count objects.

Parameters
countNumber of objects to allocate.
Returns
Pointer to allocated memory.

Definition at line 35 of file flow_polymorphic_allocator.h.

35 {
36 return static_cast<T*>(resource_->allocate(sizeof(T) * count, alignof(T)));
37 }
T * allocate(std::size_t count)
Allocate memory for count objects.

References resource_.

◆ construct()

template<typename T = std::byte>
template<typename U, typename... Args>
void flow::PolymorphicAllocator< T >::construct ( U * address,
Args &&... args )
inline

Construct an object in place at the given address.

Template Parameters
UType of object to construct.
Parameters
addressPointer to memory.
argsConstructor arguments.

Definition at line 51 of file flow_polymorphic_allocator.h.

51 {
53 }

◆ deallocate()

template<typename T = std::byte>
void flow::PolymorphicAllocator< T >::deallocate ( T * address,
std::size_t count )
inlinenoexcept

Deallocate memory.

Parameters
addressPointer to memory to deallocate.
countNumber of objects previously allocated.

Definition at line 42 of file flow_polymorphic_allocator.h.

42 {
43 resource_->deallocate(address, sizeof(T) * count, alignof(T));
44 }
void deallocate(T *address, std::size_t count) noexcept
Deallocate memory.

References resource_.

◆ destroy()

template<typename T = std::byte>
template<typename U>
void flow::PolymorphicAllocator< T >::destroy ( U * address)
inlinenoexcept

Destroy the object at the given address.

Parameters
addressPointer to object to destroy.

Definition at line 58 of file flow_polymorphic_allocator.h.

58 {
59 address->~U();
60 }

◆ operator!=

template<typename T = std::byte>
template<typename U>
bool operator!= ( const PolymorphicAllocator< T > & lhs,
const PolymorphicAllocator< U > & rhs )
friend

Definition at line 74 of file flow_polymorphic_allocator.h.

74 {
75 return !(lhs == rhs);
76 }

References PolymorphicAllocator.

◆ operator==

template<typename T = std::byte>
template<typename U>
bool operator== ( const PolymorphicAllocator< T > & lhs,
const PolymorphicAllocator< U > & rhs )
friend

Definition at line 69 of file flow_polymorphic_allocator.h.

69 {
70 return lhs.resource_ == rhs.resource_;
71 }

References PolymorphicAllocator.

◆ PolymorphicAllocator

template<typename T = std::byte>
template<typename U>
friend class PolymorphicAllocator
friend

Non-owning pointer to memory resource.

Definition at line 66 of file flow_polymorphic_allocator.h.

References PolymorphicAllocator.

Referenced by operator!=, operator==, PolymorphicAllocator(), and PolymorphicAllocator.

Member Data Documentation

◆ resource_

template<typename T = std::byte>
MemoryResource* flow::PolymorphicAllocator< T >::resource_
private

Definition at line 63 of file flow_polymorphic_allocator.h.

Referenced by allocate(), deallocate(), and PolymorphicAllocator().


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