22template<
class... Types>
27template<
class... Types>
30 this->g_function(args...);
32template<
class... Types>
35 return this->g_function ==
reinterpret_cast<fge::CallbackFunctor<Types...
>::CallbackFunction>(ptr);
40template<
class TObject,
class... Types>
42 fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject func,
48template<
class TObject,
class... Types>
51 ((this->g_object)->*(this->g_functionObj))(args...);
54template<
class TObject,
class... Types>
57 return this->g_object ==
reinterpret_cast<TObject*
>(ptr);
62template<
class... Types>
63template<
typename TLambda>
65 g_lambda(new TLambda(lambda))
67 this->g_executeLambda = [](
void* lambdaPtr, [[maybe_unused]] Types... arguments) {
68 if constexpr (std::is_invocable_v<TLambda, Types...>)
70 return (*
reinterpret_cast<TLambda*
>(lambdaPtr))(arguments...);
74 return (*
reinterpret_cast<TLambda*
>(lambdaPtr))();
77 this->g_deleteLambda = [](
void* lambdaPtr) {
delete reinterpret_cast<TLambda*
>(lambdaPtr); };
79template<
class... Types>
80CallbackLambda<Types...>::~CallbackLambda()
82 (*this->g_deleteLambda)(this->g_lambda);
85template<
class... Types>
88 (*this->g_executeLambda)(this->g_lambda, args...);
90template<
class... Types>
98template<
class... Types>
101 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
103 for (
auto& callee: this->g_callees)
105 callee._markedForDeletion =
true;
109template<
class... Types>
112 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
117 for (
auto& callee: this->g_callees)
119 if (callee._markedForDeletion)
124 callee._f = std::move(callback);
125 callee._subscriber = subscriber;
126 callee._markedForDeletion =
false;
127 return callee._f.get();
132 this->g_callees.emplace_back(std::move(callback), subscriber);
133 return this->g_callees.back()._f.get();
135template<
class... Types>
143template<
class... Types>
144template<
typename TLambda>
151template<
class... Types>
152template<
class TObject>
154 typename fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject func,
156 Subscriber* subscriber)
162template<
class... Types>
165 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
166 for (
auto& callee: this->g_callees)
168 if (callee._markedForDeletion)
173 if (callee._f->check(ptr))
176 callee._markedForDeletion =
true;
180template<
class... Types>
183 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
184 for (
auto& callee: this->g_callees)
186 if (callee._markedForDeletion)
191 if (callee._subscriber == subscriber)
193 if (this->
detachOnce(callee._subscriber) == 0)
195 callee._markedForDeletion =
true;
198 callee._markedForDeletion =
true;
202template<
class... Types>
205 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
206 for (
auto& callee: this->g_callees)
208 if (callee._markedForDeletion)
213 if (callee._f.get() == callback)
216 this->g_callees._markedForDeletion =
true;
221template<
class... Types>
224 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
226 std::size_t eraseCount = 0;
227 for (std::size_t i = 0; i < this->g_callees.size(); ++i)
229 if (this->g_callees[i]._markedForDeletion)
237 this->g_callees.erase(this->g_callees.begin() + i - eraseCount, this->g_callees.begin() + i);
242 this->g_callees[i]._f->call(std::forward<Types>(args)...);
250template<
class... Types>
253 if (
this == &handler)
258 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
260 [&handler](Types... args) { handler.call(std::forward<Types>(args)...); }),
264template<
class... Types>
267 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
268 for (
auto& callee: this->g_callees)
270 if (callee._markedForDeletion)
275 if (callee._subscriber == subscriber)
277 callee._markedForDeletion =
true;
Base class for callbacks.
Definition C_callback.hpp:37
Callback functor.
Definition C_callback.hpp:54
bool check(void *ptr) override
Check if the given pointer is the same as the one used to construct the functor.
Definition C_callback.inl:33
void call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:28
CallbackFunctor(fge::CallbackFunctor< Types... >::CallbackFunction func)
Constructor.
Definition C_callback.inl:23
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
void delPtr(void *ptr)
Remove a callback from the list.
Definition C_callback.inl:163
fge::CallbackFunctor< Types... > * addFunctor(typename fge::CallbackFunctor< Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback functor.
Definition C_callback.inl:137
void del(fge::CallbackBase< Types... > *callback)
Remove a callback from the list.
Definition C_callback.inl:203
fge::CallbackBase< Types... > * add(CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
Add a new callback to the list.
Definition C_callback.inl:110
void onDetach(fge::Subscriber *subscriber) override
This method is called when a subscriber is destroyed (destructor called)
Definition C_callback.inl:265
void call(Types... args)
Call all the callbacks with the given arguments.
Definition C_callback.inl:222
void hook(fge::CallbackHandler< Types... > &handler, fge::Subscriber *subscriber=nullptr)
Hook a callback handler to this handler.
Definition C_callback.inl:251
void delSub(fge::Subscriber *subscriber)
Remove a callback from the list.
Definition C_callback.inl:181
void clear()
Clear the list of callbacks.
Definition C_callback.inl:99
fge::CallbackObjectFunctor< TObject, Types... > * addObjectFunctor(typename fge::CallbackObjectFunctor< TObject, Types... >::CallbackFunctionObject func, TObject *object, Subscriber *subscriber=nullptr)
Helper method to add a callback object functor.
Definition C_callback.inl:153
fge::CallbackLambda< Types... > * addLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback lambda.
Definition C_callback.inl:145
Callback lambda (with/without capture)
Definition C_callback.hpp:93
bool check(void *ptr) override
Always return false.
Definition C_callback.inl:91
void call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:86
CallbackLambda(TLambda const &lambda)
Constructor.
Definition C_callback.inl:64
Callback functor of a method with an object.
Definition C_callback.hpp:135
bool check(void *ptr) override
Check if the given object pointer is the same as the one used to construct the functor.
Definition C_callback.inl:55
void call(Types... args) override
Call the callback method with the given arguments.
Definition C_callback.inl:49
CallbackObjectFunctor(fge::CallbackObjectFunctor< TObject, Types... >::CallbackFunctionObject func, TObject *object)
Constructor.
Definition C_callback.inl:41
This class is a useful utility to "link" multiple objects around.
Definition C_subscription.hpp:150
fge::Subscription::SubscriberCount detachOnce(fge::Subscriber *subscriber)
Detach only once a specific subscriber.
fge::Subscription::SubscriberCount attach(fge::Subscriber *subscriber)
Attach a specific subscriber.
void detachAll()
Detach all subscribers.