Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
uninitialized< T > Class Template Reference

Template class that allows allocating storage for an object but not calling its constructor. More...

#include <Uninitialized.hxx>

Public Member Functions

const T * operator-> () const
 
T * operator-> ()
 
T & operator* ()
 
const T & operator* () const
 
T & value ()
 
const T & value () const
 
constexpr T * get_mutable () const
 Gets the embedded object pointer in a way that is friendly to linker-initialization.
 
constexpr const T * get () const
 Gets the embedded object pointer in a way that is friendly to linker-initialization.
 
template<class... Args>
T & emplace (Args &&... args)
 Constructs the embedded object.
 
void reset ()
 Destructs the embedded object.
 

Static Public Member Functions

static constexpr T * cast_data (uninitialized< T > *parent)
 Public API to convert the pointer in a linker-initialized way.
 

Private Member Functions

constexpr T * tptrm () const
 
constexpr const T * tptr () const
 

Private Attributes

std::aligned_storage< sizeof(T), alignof(T)>::type data
 

Detailed Description

template<class T>
class uninitialized< T >

Template class that allows allocating storage for an object but not calling its constructor.

The object shall be constructed before first use by calling emplace(). It is the responsibility of the caller to also call the destructor sometime by invoking reset(), otherwise the object will be leaked.

Using this class is inherently unsafe, so avoid if possible. After c++17 this class will be replaceable by std::optional.

Definition at line 49 of file Uninitialized.hxx.

Member Function Documentation

◆ cast_data()

template<class T >
static constexpr T * uninitialized< T >::cast_data ( uninitialized< T > *  parent)
inlinestaticconstexpr

Public API to convert the pointer in a linker-initialized way.

Definition at line 117 of file Uninitialized.hxx.

◆ emplace()

template<class T >
template<class... Args>
T & uninitialized< T >::emplace ( Args &&...  args)
inline

Constructs the embedded object.

Definition at line 104 of file Uninitialized.hxx.

◆ get()

template<class T >
constexpr const T * uninitialized< T >::get ( ) const
inlineconstexpr

Gets the embedded object pointer in a way that is friendly to linker-initialization.

NOTE: when switching to std::optional<>, calls to this function need to be replaced with calls to .operator->().

Returns
const pointer to the embedded object

Definition at line 98 of file Uninitialized.hxx.

◆ get_mutable()

template<class T >
constexpr T * uninitialized< T >::get_mutable ( ) const
inlineconstexpr

Gets the embedded object pointer in a way that is friendly to linker-initialization.

NOTE: when switching to std::optional<>, calls to this function need to be replaced with calls to .operator->().

Returns
mutable pointer to the embedded object

Definition at line 88 of file Uninitialized.hxx.

◆ operator*() [1/2]

template<class T >
T & uninitialized< T >::operator* ( )
inline
Returns
the embedded object

Definition at line 63 of file Uninitialized.hxx.

◆ operator*() [2/2]

template<class T >
const T & uninitialized< T >::operator* ( ) const
inline
Returns
the embedded object

Definition at line 68 of file Uninitialized.hxx.

◆ operator->() [1/2]

template<class T >
T * uninitialized< T >::operator-> ( )
inline
Returns
pointer to the embedded object.

Definition at line 58 of file Uninitialized.hxx.

◆ operator->() [2/2]

template<class T >
const T * uninitialized< T >::operator-> ( ) const
inline
Returns
pointer to the embedded object

Definition at line 53 of file Uninitialized.hxx.

◆ reset()

template<class T >
void uninitialized< T >::reset ( )
inline

Destructs the embedded object.

Definition at line 111 of file Uninitialized.hxx.

◆ tptr()

template<class T >
constexpr const T * uninitialized< T >::tptr ( ) const
inlineconstexprprivate
Returns
the embedded object

Definition at line 131 of file Uninitialized.hxx.

◆ tptrm()

template<class T >
constexpr T * uninitialized< T >::tptrm ( ) const
inlineconstexprprivate
Returns
the embedded object (mutable pointer)

Definition at line 126 of file Uninitialized.hxx.

◆ value() [1/2]

template<class T >
T & uninitialized< T >::value ( )
inline
Returns
the embedded object

Definition at line 73 of file Uninitialized.hxx.

◆ value() [2/2]

template<class T >
const T & uninitialized< T >::value ( ) const
inline
Returns
the embedded object

Definition at line 78 of file Uninitialized.hxx.

Member Data Documentation

◆ data

template<class T >
std::aligned_storage<sizeof(T),alignof(T)>::type uninitialized< T >::data
private

Definition at line 123 of file Uninitialized.hxx.


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