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 Interface & | get () 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< Self > | guard () noexcept |
| | returns a lifetime guard whose existence guarantees the created PolymorphicHandler singleton instance will exist at least as long as the guard.
|
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
-
| Interface | The interface of the handler instances. |
| Default | The type of the default instance. Must be equal to or derive from Interface. |
| Hooks | A 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.
◆ finalize()
template<typename Interface, typename Default, typename Hooks = detail::DefaultHooks<Interface>>
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>>
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)
◆ guard()
template<typename Interface, typename Default, typename Hooks = detail::DefaultHooks<Interface>>
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>>
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>
set the current singleton instance
- Parameters
-
| handlerGuard | a 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.
◆ StaticLifetimeGuard< Self >
template<typename Interface, typename Default, typename Hooks = detail::DefaultHooks<Interface>>
The documentation for this class was generated from the following file: