iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
iox2::legacy::StaticLifetimeGuard< T > Class Template Reference

Manages a static instance of type T in a way so that each existing StaticLifetimeGuard prevents the destruction of the instance. More...

#include <static_lifetime_guard.hpp>

Public Member Functions

 StaticLifetimeGuard () noexcept
 
 StaticLifetimeGuard (const StaticLifetimeGuard &) noexcept
 
 StaticLifetimeGuard (StaticLifetimeGuard &&) noexcept
 
 ~StaticLifetimeGuard () noexcept
 
StaticLifetimeGuardoperator= (const StaticLifetimeGuard &) noexcept=default
 
StaticLifetimeGuardoperator= (StaticLifetimeGuard &&) noexcept=default
 

Static Public Member Functions

template<typename... Args>
static Tinstance (Args &&... args) noexcept
 Construct the instance to be guarded with constructor arguments.
 
static uint64_t count ()
 Get the current count value.
 

Static Protected Member Functions

static uint64_t setCount (uint64_t count)
 Set the instance life time count.
 

Detailed Description

template<typename T>
class iox2::legacy::StaticLifetimeGuard< T >

Manages a static instance of type T in a way so that each existing StaticLifetimeGuard prevents the destruction of the instance.

  1. instance() creates a static guard itself and hence has static lifetime
  2. Any static StaticLifetimeGuard G created before that prolongs the lifetime of the instance at least until G is destroyed
  3. The instance is lazily constructed, i.e. only when first used. Hence there can be guards without any instance existing. These guards still protect the instance from destruction if it is ever constructed.
  4. If and once the instance is constructed, it will be destructed only after main exits (static destruction).
  5. Guards can be used in static variables to control destruction order of static (singleton) instances if a specific order of destruction is required.
    Template Parameters
    Tthe type of the instance to be guarded
    Note
    all public functions are thread-safe
    // instance will be destroyed after guard
    // (or later if there are guards preceding guard in construction order)
    Manages a static instance of type T in a way so that each existing StaticLifetimeGuard prevents the d...
    static T & instance(Args &&... args) noexcept
    Construct the instance to be guarded with constructor arguments.
    constexpr bool always_false_v
    Helper value to bind a static_assert to a type.

Definition at line 49 of file static_lifetime_guard.hpp.

Constructor & Destructor Documentation

◆ StaticLifetimeGuard() [1/3]

template<typename T >
iox2::legacy::StaticLifetimeGuard< T >::StaticLifetimeGuard ( )
noexcept

◆ StaticLifetimeGuard() [2/3]

template<typename T >
iox2::legacy::StaticLifetimeGuard< T >::StaticLifetimeGuard ( const StaticLifetimeGuard< T > &  )
noexcept

◆ StaticLifetimeGuard() [3/3]

template<typename T >
iox2::legacy::StaticLifetimeGuard< T >::StaticLifetimeGuard ( StaticLifetimeGuard< T > &&  )
noexcept

◆ ~StaticLifetimeGuard()

Member Function Documentation

◆ count()

template<typename T >
static uint64_t iox2::legacy::StaticLifetimeGuard< T >::count ( )
static

Get the current count value.

Returns
current count value

References iox2::legacy::always_false_v.

◆ instance()

template<typename T >
template<typename... Args>
static T & iox2::legacy::StaticLifetimeGuard< T >::instance ( Args &&...  args)
staticnoexcept

Construct the instance to be guarded with constructor arguments.

Parameters
argsconstructor arguments
Returns
reference to the constructed instance or the existing instance if it already exists
Note
creates an implicit static StaticLifetimeGuard to ensure the instance is destroyed in the static destruction phase

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ setCount()

template<typename T >
static uint64_t iox2::legacy::StaticLifetimeGuard< T >::setCount ( uint64_t  count)
staticprotected

Set the instance life time count.

Parameters
countvalue to be set
Returns
previous count value
Note
This can be used to additionally extend or shorten the instance lifetime, This has to be done carefully, to ensure destruction or prevent early destruction. It is useful for testing purposes.
Only to be used for testing.
Attention
setCount is only supposed to be used while no guards are created concurrently as it will influence the guard mechanism. The instance is destroyed once the counter reaches zero upon destruction of a guard, i.e. changing the counter may lead to the instance never being destroyed or being destroyed before the last guard.

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