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>
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)
109template<
class... Types>
112 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
114 this->attach(subscriber);
117 for (
auto& callee: this->g_callees)
119 if (callee._f ==
nullptr)
121 callee._f = std::move(callback);
122 callee._subscriber = subscriber;
123 return callee._f.get();
128 this->g_callees.emplace_back(std::move(callback), subscriber);
129 return this->g_callees.back()._f.get();
131template<
class... Types>
139template<
class... Types>
140template<
typename TLambda>
147template<
class... Types>
148template<
class TObject>
150 typename fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject func,
158template<
class... Types>
161 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
162 for (
auto& callee: this->g_callees)
164 if (callee._f ==
nullptr)
169 if (callee._f->check(ptr))
171 this->detachOnce(callee._subscriber);
176template<
class... Types>
179 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
180 for (
auto& callee: this->g_callees)
182 if (callee._f ==
nullptr)
187 if (callee._subscriber == subscriber)
189 if (this->detachOnce(callee._subscriber) == 0)
198template<
class... Types>
201 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
202 for (
auto& callee: this->g_callees)
204 if (callee._f ==
nullptr)
209 if (callee._f.get() == callback)
211 this->detachOnce(callee._subscriber);
212 this->g_callees._f =
nullptr;
217template<
class... Types>
220 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
222 std::size_t eraseCount = 0;
223 for (std::size_t i = 0; i < this->g_callees.size(); ++i)
225 if (this->g_callees[i]._f ==
nullptr)
233 this->g_callees.erase(this->g_callees.begin() + i - eraseCount, this->g_callees.begin() + i);
238 this->g_callees[i]._f->call(std::forward<Types>(args)...);
246template<
class... Types>
249 if (
this == &handler)
254 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
256 [&handler](Types... args) { handler.call(std::forward<Types>(args)...); }),
260template<
class... Types>
263 std::scoped_lock<std::recursive_mutex>
const lck(this->g_mutex);
264 for (
auto& callee: this->g_callees)
266 if (callee._f ==
nullptr)
271 if (callee._subscriber == subscriber)
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:159
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:133
void del(fge::CallbackBase< Types... > *callback)
Remove a callback from the list.
Definition C_callback.inl:199
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:261
void call(Types... args)
Call all the callbacks with the given arguments.
Definition C_callback.inl:218
void hook(fge::CallbackHandler< Types... > &handler, fge::Subscriber *subscriber=nullptr)
Hook a callback handler to this handler.
Definition C_callback.inl:247
void delSub(fge::Subscriber *subscriber)
Remove a callback from the list.
Definition C_callback.inl:177
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:149
fge::CallbackLambda< Types... > * addLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback lambda.
Definition C_callback.inl:141
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