ADTF  3.18.2
StackPtr< T, StackSize, Alignment >

A smart pointer allocating its memory only on the stack. More...

Public Member Functions

 StackPtr ()
 Initializes the storage and constructs object ot type T by calling its default ctor.
 
 StackPtr (std::nullptr_t)
 Constructor overload t zero-initialize the storage - no object of type T is constructed!
 
 StackPtr (const T &data)
 Initializes the storage by calling the copy constructor of type T. More...
 
 StackPtr (T &&data)
 Initializes the storage by moving data to internal memory representation. More...
 
 ~StackPtr ()
 Destructor.
 
 StackPtr (const StackPtr &other)
 Copy constructor. More...
 
StackPtroperator= (StackPtr other)
 Assignment operator. More...
 
 StackPtr (StackPtr &&other)
 Move constructor. More...
 
T * operator-> ()
 Operator overload to use this class like a raw pointer. More...
 
const T * operator-> () const
 Operator overload to use this class like a raw pointer - for const correctness. More...
 
T & operator* ()
 Operator overload to use this class like a raw pointer. More...
 
const T & operator* () const
 Operator overload to use this class like a raw pointer - for const correctness. More...
 
 operator bool () const noexcept
 Check whether *this owns an object. More...
 
void reset ()
 Unitializes the storage and, if constructed, destructs the storaged object.
 
template<typename... Args>
void reset (Args &&... args)
 Resets the managed object of type T by forwarding args to its ctor. More...
 
void swap (StackPtr &other)
 Swap storage of *this with storage of other stack pointer. More...
 

Private Types

enum  StackPtrIntrospection { NumFlagBytes = 1 , OverheadSize = (Alignment - (StackSize % Alignment)) , StorageSize = StackSize + OverheadSize , FirstFlagPosition = StackSize }
 Calculate entire size of the _storage - adds padding bytes to comply to the alignment. More...
 
enum  StackPtrFlagPositions { InitializeStatus = FirstFlagPosition }
 Absolute bit positions of the flags inside the overhead area of the storage. More...
 
enum class  StackPtrFlags : char { IsDestroyed = 0x00 , IsConstructed = 0x01 }
 Flags for StackPtrFlagPositions. More...
 

Private Member Functions

void * address ()
 Get pointer to beginning of the storage. More...
 
const void * address () const
 Get pointer to beginning of the storage provided for const correctness. More...
 
void setFlag (StackPtrFlagPositions position, StackPtrFlags flag)
 Sets a flag to the appropriate position in the overhead section of the storage. More...
 
bool isConstructed () const noexcept
 Check whether the object hidden by the storage got constructed. More...
 

Private Attributes

std::array< char, StorageSize_storage
 Aligned storage large enough to contain one object of type T and some overhead for flags.
 

Detailed Description

template<typename T, std::size_t StackSize = sizeof(T), std::size_t Alignment = alignof(std::size_t)>
class a_util::memory::StackPtr< T, StackSize, Alignment >

A smart pointer allocating its memory only on the stack.

Template Parameters
TThe storaged type - must be default and move constructible
StackSizeSize of the stack to allocate - must at least be the sizeof the object of type T.
AlignmentAlignment of the storage

Definition at line 32 of file stack_ptr_decl.h.

Member Enumeration Documentation

◆ StackPtrFlagPositions

enum StackPtrFlagPositions
private

Absolute bit positions of the flags inside the overhead area of the storage.

Enumerator
InitializeStatus 

Flag position for the initialize status.

Definition at line 143 of file stack_ptr_decl.h.

◆ StackPtrFlags

enum StackPtrFlags : char
strongprivate

Flags for StackPtrFlagPositions.

Enumerator
IsDestroyed 

for InitializeStatus --> contained object not constructed

IsConstructed 

for InitializeStatus --> contained object constructed

Definition at line 148 of file stack_ptr_decl.h.

◆ StackPtrIntrospection

enum StackPtrIntrospection
private

Calculate entire size of the _storage - adds padding bytes to comply to the alignment.

Enumerator
NumFlagBytes 

Number of bytes required to store control flags.

OverheadSize 

Size of the overhead containing the flags (complying to alignment)

StorageSize 

Entire size of the _storage, including aligned overhead for necessary flags.

FirstFlagPosition 

First byte of the StackPtr flag area.

Definition at line 131 of file stack_ptr_decl.h.

Constructor & Destructor Documentation

◆ StackPtr() [1/4]

StackPtr ( const T &  data)
inline

Initializes the storage by calling the copy constructor of type T.

Parameters
[in]dataObject of type T whose content gets copy constructed into the storage

Definition at line 37 of file stack_ptr_impl.h.

◆ StackPtr() [2/4]

StackPtr ( T &&  data)
inline

Initializes the storage by moving data to internal memory representation.

Parameters
[in]dataObject of type T that gets moved into the storage

Definition at line 43 of file stack_ptr_impl.h.

◆ StackPtr() [3/4]

StackPtr ( const StackPtr< T, StackSize, Alignment > &  other)
inline

Copy constructor.

Parameters
[in]otherObject of type StackPtr whose content gets copy constructed into the storage only if the object inside its storage was constructed

Definition at line 57 of file stack_ptr_impl.h.

References StackPtr< T, StackSize, Alignment >::isConstructed().

◆ StackPtr() [4/4]

StackPtr ( StackPtr< T, StackSize, Alignment > &&  other)
inline

Move constructor.

Parameters
[in,out]otherMove-from object, left in a valid but unspecified state

Definition at line 73 of file stack_ptr_impl.h.

Member Function Documentation

◆ address() [1/2]

void * address
inlineprivate

Get pointer to beginning of the storage.

Returns
Pointer to the beginning of the storage

Definition at line 164 of file stack_ptr_impl.h.

Referenced by StackPtr< T, StackSize, Alignment >::StackPtr().

◆ address() [2/2]

const void * address
inlineprivate

Get pointer to beginning of the storage provided for const correctness.

Returns
Pointer to the beginning of the storage

Definition at line 170 of file stack_ptr_impl.h.

◆ isConstructed()

bool isConstructed
inlineprivatenoexcept

Check whether the object hidden by the storage got constructed.

Returns
true if the object was constructed, false otherwise.

Definition at line 183 of file stack_ptr_impl.h.

Referenced by StackPtr< T, StackSize, Alignment >::StackPtr(), StackPtr< Implementation, 64 >::StackPtr(), and StackPtr< T, StackSize, Alignment >::swap().

◆ operator bool()

operator bool
inlineexplicitnoexcept

Check whether *this owns an object.

Returns
true if *this owns an object, false otherwise

Definition at line 105 of file stack_ptr_impl.h.

◆ operator*() [1/2]

T & operator*
inline

Operator overload to use this class like a raw pointer.

Returns
Reference to the storaged object of type T
Warning
No check is performed whether the object was really constructed

Definition at line 93 of file stack_ptr_impl.h.

◆ operator*() [2/2]

const T & operator*
inline

Operator overload to use this class like a raw pointer - for const correctness.

Returns
Reference to the storaged object of type T
Warning
No check is performed whether the object was really constructed

Definition at line 99 of file stack_ptr_impl.h.

◆ operator->() [1/2]

T * operator->
inline

Operator overload to use this class like a raw pointer.

Returns
Pointer to the storaged object of type T
Warning
No check is performed whether the object was really constructed

Definition at line 81 of file stack_ptr_impl.h.

◆ operator->() [2/2]

const T * operator->
inline

Operator overload to use this class like a raw pointer - for const correctness.

Returns
Pointer to the storaged object of type T
Warning
No check is performed whether the object was really constructed

Definition at line 87 of file stack_ptr_impl.h.

◆ operator=()

StackPtr< T, StackSize, Alignment > & operator= ( StackPtr< T, StackSize, Alignment >  other)
inline

Assignment operator.

Parameters
[in]otherObject of type StackPtr whose content gets copy constructed into the storage only if the object inside its storage was constructed
Returns
*this

Definition at line 65 of file stack_ptr_impl.h.

◆ reset()

void reset ( Args &&...  args)
inline

Resets the managed object of type T by forwarding args to its ctor.

Template Parameters
ArgsArgument types of type T ctor
Parameters
[in]argsArguments passed to the ctor of type T for its construction

Definition at line 121 of file stack_ptr_impl.h.

References adtf_ddl::access_element::reset().

◆ setFlag()

void setFlag ( StackPtrFlagPositions  position,
StackPtrFlags  flag 
)
inlineprivate

Sets a flag to the appropriate position in the overhead section of the storage.

Parameters
[in]positionPosition of the flag in the overhead area
[in]flagThe flag to set

Definition at line 176 of file stack_ptr_impl.h.

Referenced by StackPtr< T, StackSize, Alignment >::StackPtr().

◆ swap()

void swap ( StackPtr< T, StackSize, Alignment > &  other)
inline

Swap storage of *this with storage of other stack pointer.

Parameters
[in]otherObject of type StackPtr whose content gets swapped with *this
Note
The storage is swapped byte-by-byte, so depending on the size this operation might be very time consuming.

Definition at line 129 of file stack_ptr_impl.h.

References StackPtr< T, StackSize, Alignment >::_storage, StackPtr< T, StackSize, Alignment >::isConstructed(), StackPtr< T, StackSize, Alignment >::reset(), adtf_ddl::access_element::reset(), and a_util::memory::swap().

Referenced by a_util::memory::swap().