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>
|
| template<typename... Args> |
| static T & | instance (Args &&... args) noexcept |
| | Construct the instance to be guarded with constructor arguments.
|
| static uint64_t | count () |
| | Get the current count value.
|
|
| static uint64_t | setCount (uint64_t count) |
| | Set the instance life time count.
|
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.
- instance() creates a static guard itself and hence has static lifetime
- Any static StaticLifetimeGuard G created before that prolongs the lifetime of the instance at least until G is destroyed
- 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.
- If and once the instance is constructed, it will be destructed only after main exits (static destruction).
- 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
-
| T | the type of the instance to be guarded |
- Note
- all public functions are thread-safe
static T & instance(Args &&... args) noexcept
Construct the instance to be guarded with constructor arguments.
StaticLifetimeGuard() noexcept
Definition at line 49 of file static_lifetime_guard.hpp.
◆ StaticLifetimeGuard() [1/3]
◆ StaticLifetimeGuard() [2/3]
◆ StaticLifetimeGuard() [3/3]
◆ ~StaticLifetimeGuard()
◆ count()
Get the current count value.
- Returns
- current count value
◆ instance()
template<typename T>
template<typename... Args>
Construct the instance to be guarded with constructor arguments.
- Parameters
-
| args | constructor 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()
Set the instance life time count.
- Parameters
-
- 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: