oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ove::StaticCell< T > Class Template Reference

Thread-safe, lazily initialised storage cell for a single object of type T. More...

#include <sync.hpp>

Public Member Functions

template<typename... Args>
T & init (Args &&... args)
 Constructs the contained object in-place.
 
T & get ()
 Returns a reference to the contained object.
 
const T & get () const
 Returns a const reference to the contained object.
 
bool is_initialized () const
 Returns true if init() has been called successfully.
 
 StaticCell (const StaticCell &)=delete
 
StaticCelloperator= (const StaticCell &)=delete
 
 StaticCell (StaticCell &&)=delete
 
StaticCelloperator= (StaticCell &&)=delete
 

Detailed Description

template<typename T>
class ove::StaticCell< T >

Thread-safe, lazily initialised storage cell for a single object of type T.

Holds the object in a properly aligned raw byte buffer so that no dynamic allocation is required. The object is placement-constructed by calling init() and its lifetime ends when the StaticCell is destroyed. A second call to init() aborts via assertion.

This is useful for zero-heap embedded targets where global objects with complex constructors must be initialised in a controlled order.

Template Parameters
TThe type of the object to store. Must be constructible with the arguments passed to init().
Note
Non-copyable and non-movable.
init() and get() are safe to call from a single initialisation thread. Concurrent get() calls after init() completes are safe due to the atomic flag, but the cell does not protect the contained object itself.

Member Function Documentation

◆ init()

template<typename T >
template<typename... Args>
T & ove::StaticCell< T >::init ( Args &&...  args)
inline

Constructs the contained object in-place.

Forwards all arguments to T's constructor. The initialisation flag is set atomically, ensuring the cell is initialised at most once.

Template Parameters
ArgsConstructor argument types.
Parameters
[in]argsArguments forwarded to T's constructor.
Returns
Reference to the newly constructed object.

◆ get() [1/2]

template<typename T >
T & ove::StaticCell< T >::get ( )
inline

Returns a reference to the contained object.

Asserts if init() has not been called yet.

Returns
Reference to the contained object.

◆ get() [2/2]

template<typename T >
const T & ove::StaticCell< T >::get ( ) const
inline

Returns a const reference to the contained object.

Asserts if init() has not been called yet.

Returns
Const reference to the contained object.

◆ is_initialized()

template<typename T >
bool ove::StaticCell< T >::is_initialized ( ) const
inline

Returns true if init() has been called successfully.

Returns
true when the cell contains a live object.

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