ECSTASY
All in the name
Loading...
Searching...
No Matches
util::StackAllocator< size, Base > Class Template Reference

#include <StackAllocator.hpp>

Collaboration diagram for util::StackAllocator< size, Base >:

Public Member Functions

 StackAllocator ()
 Default constructor.
 
 StackAllocator (StackAllocator &&other)
 Move constructor.
 
 ~StackAllocator ()
 Destructor.
 
StackAllocatoroperator= (StackAllocator &&other)
 Move assignment operator.
 
template<std::derived_from< Base > T, typename... Args>
T & instanciate (Args &&...args)
 Instanciate a new instance of type T with lifetime attached to this lifetime.
 
constexpr const std::vector< Base * > & getInstances () const noexcept
 Access the internal instances vector.
 

Static Public Attributes

static constexpr size_t mem_size = size
 Size in byte of the allocator memory.
 

Private Attributes

char _memory [size]
 
size_t _cursor
 
std::vector< Base * > _instances
 

Detailed Description

template<size_t size, typename Base>
class util::StackAllocator< size, Base >

Definition at line 31 of file StackAllocator.hpp.

Constructor & Destructor Documentation

◆ StackAllocator() [1/2]

template<size_t size, typename Base >
util::StackAllocator< size, Base >::StackAllocator ( )
inline

Default constructor.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2023-11-08)

Definition at line 44 of file StackAllocator.hpp.

45 {
46 _cursor = 0;
47 }

◆ StackAllocator() [2/2]

template<size_t size, typename Base >
util::StackAllocator< size, Base >::StackAllocator ( StackAllocator< size, Base > &&  other)
inline

Move constructor.

Warning
Instances must not have pointers/reference to object in other memory block.
Parameters
[in]otherExisting allocator to copy.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2023-11-08)

Definition at line 59 of file StackAllocator.hpp.

60 {
61 *this = std::move(other);
62 }

◆ ~StackAllocator()

template<size_t size, typename Base >
util::StackAllocator< size, Base >::~StackAllocator ( )
inline

Destructor.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2023-11-08)

Definition at line 70 of file StackAllocator.hpp.

71 {
72 for (auto &&object : _instances)
73 (*object).~Base();
74 }
std::vector< Base * > _instances

Member Function Documentation

◆ getInstances()

template<size_t size, typename Base >
constexpr const std::vector< Base * > & util::StackAllocator< size, Base >::getInstances ( ) const
inlineconstexprnoexcept

Access the internal instances vector.

Returns
const std::vector<Base *>& Read only instances vector.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2023-11-08)

Definition at line 133 of file StackAllocator.hpp.

134 {
135 return _instances;
136 }

◆ instanciate()

template<size_t size, typename Base >
template<std::derived_from< Base > T, typename... Args>
T & util::StackAllocator< size, Base >::instanciate ( Args &&...  args)
inline

Instanciate a new instance of type T with lifetime attached to this lifetime.

Template Parameters
TType of the new object.
ArgsArguments Types of the object constructor.
Parameters
[in]argsArguments to forward to the object constructor.
Returns
T& newly created object.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-24)

Definition at line 117 of file StackAllocator.hpp.

118 {
119 T *newObject = new (reinterpret_cast<void *>(&_memory[_cursor])) T(std::forward<Args>(args)...);
120
121 _instances.push_back(newObject);
122 _cursor += sizeof(T);
123 return *newObject;
124 }
T push_back(T... args)

◆ operator=()

template<size_t size, typename Base >
StackAllocator & util::StackAllocator< size, Base >::operator= ( StackAllocator< size, Base > &&  other)
inline

Move assignment operator.

Warning
Instances must not have pointers/reference to object in other memory block.
Parameters
[in]otherExisting allocator to move into this.
Returns
StackAllocator& this
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2023-11-08)

Definition at line 88 of file StackAllocator.hpp.

89 {
90 // Doesn't support overlap but we shouldn't have any
91 std::memcpy(_memory, other._memory, size);
92 _cursor = other._cursor;
93 for (Base *instance : other._instances)
94 _instances.push_back(reinterpret_cast<Base *>(reinterpret_cast<char *>(_memory)
95 + (reinterpret_cast<char *>(instance) - reinterpret_cast<char *>(other._memory))));
96
97 // Clear other
98 other._cursor = 0;
99 other._instances.clear();
100 return *this;
101 }
T clear(T... args)
T memcpy(T... args)

Member Data Documentation

◆ _cursor

template<size_t size, typename Base >
size_t util::StackAllocator< size, Base >::_cursor
private

Definition at line 141 of file StackAllocator.hpp.

◆ _instances

template<size_t size, typename Base >
std::vector<Base *> util::StackAllocator< size, Base >::_instances
private

Definition at line 142 of file StackAllocator.hpp.

◆ _memory

template<size_t size, typename Base >
char util::StackAllocator< size, Base >::_memory[size]
private

Definition at line 139 of file StackAllocator.hpp.

◆ mem_size

template<size_t size, typename Base >
constexpr size_t util::StackAllocator< size, Base >::mem_size = size
staticconstexpr

Size in byte of the allocator memory.

Note
This is an alias to the template parameter
Template Parameters
size

Definition at line 36 of file StackAllocator.hpp.


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