iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
iox2::legacy::PolymorphicHandler< Interface, Default, Hooks > Class Template Reference

Implements a singleton handler that has a default instance and can be changed to another instance at runtime. All instances have to derive from the same interface. The singleton handler owns the default instance but all other instances are created externally. More...

#include <polymorphic_handler.hpp>

Static Public Member Functions

static Interfaceget () noexcept
 obtain the current singleton instance
 
template<typename Handler >
static bool set (StaticLifetimeGuard< Handler > handlerGuard) noexcept
 set the current singleton instance
 
static bool reset () noexcept
 reset the current singleton instance to the default instance
 
static void finalize () noexcept
 finalizes the instance, afterwards Hooks::onSetAfterFinalize will be called during the remaining program lifetime when attempting to set or reset the handler
 
static StaticLifetimeGuard< Selfguard () noexcept
 returns a lifetime guard whose existence guarantees the created PolymorphicHandler singleton instance will exist at least as long as the guard.
 

Friends

class StaticLifetimeGuard< Self >
 

Detailed Description

template<typename Interface, typename Default, typename Hooks = detail::DefaultHooks<Interface>>
class iox2::legacy::PolymorphicHandler< Interface, Default, Hooks >

Implements a singleton handler that has a default instance and can be changed to another instance at runtime. All instances have to derive from the same interface. The singleton handler owns the default instance but all other instances are created externally.

Template Parameters
InterfaceThe interface of the handler instances.
DefaultThe type of the default instance. Must be equal to or derive from Interface.
HooksA struct that implements onSetAfterFinalize which is called when attempting to set or reset the handler after finalize was called.
Note
In the special case where Default equals Interface, no polymorphism is required.
The lifetime of external non-default instances must exceed the lifetime of the PolymorphicHandler.
The PolymorphicHandler is guaranteed to provide a valid handler during the whole program lifetime (static). It is hence not advisable to have other static variables depend on the PolymorphicHandler. It must be ensured that they are destroyed before the PolymorphicHandler.
Hooks must implement static void onSetAfterFinalize(Interface& /*currentInstance*‍/, Interface& /*newInstance*‍/).
DefaultHooks call abort if the handler is set or reset after finalize

Definition at line 57 of file polymorphic_handler.hpp.

Member Function Documentation

◆ finalize()

template<typename Interface , typename Default , typename Hooks = detail::DefaultHooks<Interface>>
static void iox2::legacy::PolymorphicHandler< Interface, Default, Hooks >::finalize ( )
staticnoexcept

finalizes the instance, afterwards Hooks::onSetAfterFinalize will be called during the remaining program lifetime when attempting to set or reset the handler

◆ get()

template<typename Interface , typename Default , typename Hooks = detail::DefaultHooks<Interface>>
static Interface & iox2::legacy::PolymorphicHandler< Interface, Default, Hooks >::get ( )
staticnoexcept

obtain the current singleton instance

Returns
the current instance
Note
we cannot be sure to use the current handler unless we call get, i.e. a reference obtained from get may reference a previous handler (that is still functional)

Referenced by iox2::legacy::er::panic(), iox2::legacy::er::report(), iox2::legacy::er::detail::report(), iox2::legacy::er::detail::report(), and iox2::legacy::er::report().

◆ guard()

template<typename Interface , typename Default , typename Hooks = detail::DefaultHooks<Interface>>
static StaticLifetimeGuard< Self > iox2::legacy::PolymorphicHandler< Interface, Default, Hooks >::guard ( )
staticnoexcept

returns a lifetime guard whose existence guarantees the created PolymorphicHandler singleton instance will exist at least as long as the guard.

Returns
opaque lifetime guard object for the (implicit) PolymorphicHandler instance
Note
the PolymorphicHandler will exist once any of the static methods (get, set etc.) are called

◆ reset()

template<typename Interface , typename Default , typename Hooks = detail::DefaultHooks<Interface>>
static bool iox2::legacy::PolymorphicHandler< Interface, Default, Hooks >::reset ( )
staticnoexcept

reset the current singleton instance to the default instance

Returns
true if handler was reset to default, false otherwise
Note
the handler cannot be reset if it was finalized

◆ set()

template<typename Interface , typename Default , typename Hooks = detail::DefaultHooks<Interface>>
template<typename Handler >
static bool iox2::legacy::PolymorphicHandler< Interface, Default, Hooks >::set ( StaticLifetimeGuard< Handler handlerGuard)
staticnoexcept

set the current singleton instance

Parameters
handlerGuarda guard to the handler instance to be set
Returns
true if handler was set, false otherwise
Note
the handler cannot be replaced if it was finalized
using a guard in the interface prevents the handler to be destroyed while it is used, passing the guard by value is necessary (it has no state anyway)
If finalization takes effect here, setHandler will still change the handler This is still correct concurrent behavior in the sense that it maps to a sequential execution where the handler is set before finalization.

Friends And Related Symbol Documentation

◆ StaticLifetimeGuard< Self >

template<typename Interface , typename Default , typename Hooks = detail::DefaultHooks<Interface>>
friend class StaticLifetimeGuard< Self >
friend

Definition at line 58 of file polymorphic_handler.hpp.


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