iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
polymorphic_handler.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_DESIGN_POLYMORPHIC_HANDLER_HPP
15#define IOX2_BB_DESIGN_POLYMORPHIC_HANDLER_HPP
16
19
20#include <type_traits>
21
22namespace iox2 {
23namespace legacy {
24namespace detail {
25
29template <typename Interface>
35 [[noreturn]] static void onSetAfterFinalize(Interface& currentInstance, Interface& newInstance) noexcept;
36};
37
38} // namespace detail
39
56template <typename Interface, typename Default, typename Hooks = detail::DefaultHooks<Interface>>
57class PolymorphicHandler {
58 static_assert(std::is_base_of<Interface, Default>::value, "Interface must be a base class of Default");
59
60 using Self = PolymorphicHandler<Interface, Default, Hooks>;
61 friend class StaticLifetimeGuard<Self>;
62
63 public:
69 static Interface& get() noexcept;
70
80 template <typename Handler>
81 static bool set(StaticLifetimeGuard<Handler> handlerGuard) noexcept;
82
86 static bool reset() noexcept;
87
91 static void finalize() noexcept;
92
98 static StaticLifetimeGuard<Self> guard() noexcept;
99
100 private:
101 PolymorphicHandler() noexcept;
102
103 static PolymorphicHandler& self() noexcept;
104
105 static Default& getDefault() noexcept;
106
107 static Interface* getCurrentRelaxed() noexcept;
108
109 static Interface* getCurrentSync() noexcept;
110
111 static bool setHandler(Interface& handler) noexcept;
112
113 // should a defaultHandler be created, the guard prevents its destruction
114 StaticLifetimeGuard<Default> m_defaultGuard;
115 concurrent::Atomic<bool> m_isFinal { false };
116 concurrent::Atomic<Interface*> m_current { nullptr };
117};
118
119} // namespace legacy
120} // namespace iox2
121
122#include "iox2/legacy/detail/polymorphic_handler.inl"
123
124#endif // IOX2_BB_DESIGN_POLYMORPHIC_HANDLER_HPP
static Interface & get() noexcept
obtain the current singleton instance
static bool set(StaticLifetimeGuard< Handler > handlerGuard) noexcept
Manages a static instance of type T in a way so that each existing StaticLifetimeGuard prevents the d...
A thin wrapper for a 'std::atomic' which ensures that all atomic operations are always lock-free in o...
Definition atomic.hpp:32
default hooks for the PolymorphicHandler
static void onSetAfterFinalize(Interface &currentInstance, Interface &newInstance) noexcept
called if the polymorphic handler is set or reset after finalize