Sen API
Sen Libraries
Loading...
Searching...
No Matches
callback.h
Go to the documentation of this file.
1// === callback.h ======================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_OBJ_CALLBACK_H
9#define SEN_CORE_OBJ_CALLBACK_H
10
11// sen
18
19// std
20#include "detail/work_queue.h"
21
22#include <exception>
23#include <functional>
24#include <memory>
25#include <mutex>
26#include <optional>
27#include <utility>
28#include <variant>
29
30namespace sen
31{
32class NativeObject;
33class Object;
34} // namespace sen
35
36namespace sen::impl
37{
38class WorkQueue;
39[[nodiscard]] inline WorkQueue* getWorkQueue(NativeObject* object) noexcept;
40} // namespace sen::impl
41
42namespace sen
43{
44
47
53
56{
57};
58
59namespace impl
60{
61
62class CallbackBase;
63
65struct CallbackData
66{
67 std::recursive_mutex mutex;
68 WorkQueue* queue = nullptr;
69 std::optional<std::weak_ptr<Object>> caller;
70};
71
73class CallbackLock
74{
75 SEN_NOCOPY_NOMOVE(CallbackLock)
76
77public:
78 ~CallbackLock() { data_->mutex.unlock(); }
79
80public:
82 [[nodiscard]] bool isValid() const noexcept { return isCallbackDataValid(*data_); }
83
85 void pushAnswer(sen::std_util::move_only_function<void()>&& answer, bool force) const;
86
88 void invalidate() const noexcept { data_->queue = nullptr; }
89
91 [[nodiscard]] bool isSameQueue(const WorkQueue* queue) const noexcept { return queue == data_->queue; }
92
94 [[nodiscard]] static bool isCallbackDataValid(const CallbackData& data) noexcept
95 {
96 return data.queue != nullptr && (!data.caller.has_value() || !data.caller.value().expired());
97 }
98
99private:
100 friend class CallbackBase;
101 explicit CallbackLock(std::shared_ptr<CallbackData> data): data_(std::move(data)) { data_->mutex.lock(); }
102
103private:
104 std::shared_ptr<CallbackData> data_;
105};
106
110class CallbackBase: public std::enable_shared_from_this<CallbackBase>
111{
112public:
113 SEN_COPY_MOVE(CallbackBase)
114
115public:
116 virtual ~CallbackBase() = default;
117
118public:
120 [[nodiscard]] CallbackLock lock() const { return CallbackLock(data_); }
121
122protected:
124 CallbackBase() = default;
125
127 explicit CallbackBase(WorkQueue* queue) { data_->queue = queue; }
128
131 explicit CallbackBase(NativeObject* obj);
132
133 [[nodiscard]] CallbackData& getData() const noexcept { return *data_; }
134
135private:
136 std::shared_ptr<CallbackData> data_ = std::make_shared<CallbackData>();
137};
138
139} // namespace impl
140
144template <typename InfoClass, typename... Args>
145class Callback final: public impl::CallbackBase
146{
147public:
148 SEN_COPY_MOVE(Callback)
149
150public:
153 using CallbackFunc = std::variant<Function, FunctionWithCallInfo>;
154
155public:
156 Callback() = default;
157 ~Callback() override = default;
158
160 Callback(impl::WorkQueue* queue, CallbackFunc&& function) noexcept: CallbackBase(queue), func_(std::move(function)) {}
161
164 Callback(NativeObject* obj, CallbackFunc&& function) noexcept: CallbackBase(obj), func_(std::move(function)) {}
165
168 template <typename C>
169 Callback(C* obj, void (C::*function)(MaybeRef<Args>...))
170 : CallbackBase(obj, [obj, function](MaybeRef<Args>... args) { ((*obj).*(function))(args...); })
171 {
172 }
173
176 template <typename C>
177 Callback(C* obj, void (C::*function)(const InfoClass&, MaybeRef<Args>...))
178 : CallbackBase(obj)
179 , func_([obj, function](const InfoClass& info, MaybeRef<Args>... args) { ((*obj).*(function))(info, args...); })
180 {
181 }
182
183public:
185 void invoke(const InfoClass& info, MaybeRef<Args>... args) const
186 {
187 // directly use the data's mutex to avoid the overhead of creating a Lock object
188 auto& data = getData();
189 std::scoped_lock<std::recursive_mutex> lock(data.mutex);
190
191 if (SEN_LIKELY(impl::CallbackLock::isCallbackDataValid(data)))
192 {
193 std::holds_alternative<Function>(func_) ? std::get<Function>(func_)(args...)
194 : std::get<FunctionWithCallInfo>(func_)(info, args...);
195 }
196 }
197
198private:
199 mutable CallbackFunc func_;
200};
201
203template <typename R>
205
207template <typename... Args>
209
211template <typename R>
213
216
218
219} // namespace sen
220
221#endif // SEN_CORE_OBJ_CALLBACK_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
Base class for event or method callbacks. It stores the queue where to push the response....
Definition callback.h:146
Callback(C *obj, void(C::*function)(const EventInfo &, MaybeRef< Args >...))
Definition callback.h:177
sen::std_util::move_only_function< void(MaybeRef< Args >...)> Function
Definition callback.h:151
Callback(C *obj, void(C::*function)(MaybeRef< Args >...))
Definition callback.h:169
sen::std_util::move_only_function< void(const InfoClass &, MaybeRef< Args >...)> FunctionWithCallInfo
Definition callback.h:152
Callback()=default
Callback(impl::WorkQueue *queue, CallbackFunc &&function) noexcept
Definition callback.h:160
std::variant< Function, FunctionWithCallInfo > CallbackFunc
Definition callback.h:153
Callback(NativeObject *obj, CallbackFunc &&function) noexcept
Definition callback.h:164
void invoke(const EventInfo &info, MaybeRef< Args >... args) const
Definition callback.h:185
An object instantiated in this process. This is the base class for all user-implemented objects.
Definition native_object.h:63
A sen object.
Definition object.h:76
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
A point in time.
Definition timestamp.h:26
TimeStamp creationTime
When was the event created, timestamped by the source.
Definition callback.h:51
Result< R, std::exception_ptr > MethodResult
The result of a method (which can be an exception in case of error).
Definition callback.h:204
Callback< MethodCallInfo, MethodResult< R > > MethodCallback
A method callback.
Definition callback.h:212
Callback< EventInfo, Args... > EventCallback
An event callback.
Definition callback.h:208
EventCallback<> PropertyCallback
A property change callback.
Definition callback.h:215
Information about an event emission.
Definition callback.h:50
Information about a method call.
Definition callback.h:56
std::conditional_t< std::is_arithmetic_v< T >||shouldBePassedByValueV< T >, T, AddConstRef< T > > MaybeRef
returns 'const T&' or 'T' depending on the type
Definition class_helpers.h:46
@ force
Definition unit.h:40
detail::MoveOnlyFunctionImpl< FwdArgs... > move_only_function
Definition move_only_function.h:256
Definition assert.h:17