This templated class supports the implementation of types whose fields are allocated within a MonoAllocator . In addition the monotonic allocator itself is also self-contained. This results in the following:
ALib uses this type in various areas. A prominent sample is class Exception which is the unique C++ throwable
used by the library.
The general use-case for this type is similar to the use-case of class MonoAllocator, with the addition that the life-cycle of the allocated objects are bound to the life-cycle of an instance of the derived type. A well understandable sample for this is found with type Variable implemented in module ALib Configuration .
The proposed schematic for using this class is as follows:
std::move
.this
, methods of MyType use method Self to access field members (which are defined in helper struct MyTypesFields).A quick sample code of this schematic is given with chapter 5.1 Self-Contained Allocators of the Programmer's Manual of this ALib Module .
It is superfluous to say that instances of derived types should not be created using C++ keyword new
but instead should be created on stack memory or embedded as value members in other types.
Finally this type supports memory resets. For this, a MonoAllocator::Snapshot is created right after construction. Method Reset resets the internal monotonic allocator using this snapshot. Before that, the TContained object is destructed and afterwards is "re-constructed" using C++ placement new. For the latter, variadic construction parameters are accepted by method Reset.
The memory reserved by Reset can be increased with method TakeSnapshot. Furthermore, the monotonic allocator's pair of methods Reset and TakeSnapshot are also allowed to be used to implement more complex (nested) snapshots.
TContained | Default-constructible type of the stored object. |
Definition at line 83 of file selfcontained.hpp.
#include <selfcontained.hpp>
Inner Type Index: | |
struct | Fields |
Public Method Index: | |
SelfContained ()=delete | |
SelfContained (SelfContained &&move) noexcept | |
SelfContained (SelfContained &)=delete | |
template<typename... TArgs> | |
SelfContained (size_t initialChunkSize, unsigned int chunkGrowthInPercent, TArgs &&... args) | |
~SelfContained () | |
MonoAllocator & | Allocator () |
const MonoAllocator & | Allocator () const |
SelfContained & | operator= (SelfContained &&src) |
SelfContained & | operator= (SelfContained &)=delete |
template<typename... TArgs> | |
void | Reset (TArgs &&... args) |
TContained & | Self () |
const TContained & | Self () const |
void | TakeSnapshot () |
|
protected |
The only member of this class. It points to the start of the 'effective' members, residing in the first chunk of the mono allocator.
Definition at line 125 of file selfcontained.hpp.
|
inline |
Creates object fields allocated in the first chunk of the monotonic allocator which is found in that struct.
The instance of custom type TContained which is found in fields will be constructed using the given variadic arguments args .
TArgs | The argument types used for constructing TContained . |
initialChunkSize | The initial size of memory chunks used with the monotonic allocator. |
chunkGrowthInPercent | Optional growth factor in percent (*100), applied to each allocation of a next chunk size. Values provided should be greater than 100. |
args | The arguments to construct the instance of TContained . |
Definition at line 145 of file selfcontained.hpp.
|
delete |
Deleted default constructor.
|
delete |
Deleted copy constructor.
|
inlinenoexcept |
Move constructor. Sets field fields of src to nullptr
.
move | The object to move. |
Definition at line 207 of file selfcontained.hpp.
|
inline |
Destructor. If fields is not nullptr
, the destructors of TContained and the monotonic allocator are explicitly invoked.
Definition at line 237 of file selfcontained.hpp.
|
inline |
Returns a non-constant reference to the self-contained allocator object.
Definition at line 269 of file selfcontained.hpp.
|
inline |
Returns a constant reference to the self-contained allocator object.
Definition at line 278 of file selfcontained.hpp.
|
inline |
Move assignment operator. Sets field fields of src to nullptr
.
src | The object to move. |
Definition at line 225 of file selfcontained.hpp.
|
delete |
Deleted copy constructor.
|
inline |
Resets the monotonic allocator that this object is contained in to the snapshot created right after construction. The allocated memory chunks will remain allocated and reused. Prior to resetting, the destructor of the custom object of type TContained is invoked and after the reset, in-place construction is performed.
To preserve further custom objects that might have been initially allocated, method TakeSnapshot might be used after the initial allocations (aka after construction of the derived type). In this case, a derived type should expose an own version of this method which prior and after the invocation this base implementation, either destroys and reconstructs own objects or just leaves them intact.
It is up to the implementation of the derived class if this method should be exposed or not.
It is furthermore allowed to use methods MonoAllocator::TakeSnapshot and MonoAllocator::Reset directly, to allow more flexible, nested snapshots.
TArgs | The argument types used for re-constructing TContained . |
args | The arguments to re-construct the instance of TContained . |
Definition at line 182 of file selfcontained.hpp.
|
inline |
Returns a non-constant reference to the stored object of type TContained .
Definition at line 251 of file selfcontained.hpp.
|
inline |
Returns a constant reference to the stored object of type TContained .
Definition at line 260 of file selfcontained.hpp.
|
inline |
With the construction of a self-contained object, a MonoAllocator::Snapshot is taken, used with method Reset to clear the allocator without disposing the memory of the self-contained object itself.
With this method, this snapshot can be modified to use the current fill of the allocator. Note that it is still allowed to reset the monotonic allocator "manually", using custom snapshot objects received with MonoAllocator::TakeSnapshot .
Definition at line 292 of file selfcontained.hpp.