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.
Thread-safety. The cell publishes the init()get() happens-before edge via an atomic flag, so once init() has returned on one thread, all subsequent get() calls on other threads observe a fully constructed object. Beyond that, the cell does not synchronise access to the contained T: concurrent reads/mutations via the returned reference are the caller's responsibility — wrap the contents in your own Mutex (or use a type that is internally thread-safe) if multiple threads will mutate it after publication.

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.

The returned reference is unsynchronised — see the class-level thread-safety note: concurrent mutations through this reference are the caller's responsibility.

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.

The returned reference is unsynchronised — see the class-level thread-safety note. Concurrent const reads are safe; if any thread mutates the contained object (via the non-const overload or via const_cast / mutable members), the caller must provide external synchronisation.

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: