17#ifndef _FGE_C_CALLBACKHANDLER_HPP_INCLUDED
18#define _FGE_C_CALLBACKHANDLER_HPP_INCLUDED
20#include "FastEngine/C_subscription.hpp"
35template<
class TReturn,
class... Types>
41 virtual TReturn call(Types... args) = 0;
42 virtual bool check(
void* ptr) = 0;
52template<
class TReturn,
class... Types>
56 using CallbackFunction = TReturn (*)(Types... args);
71 TReturn
call(Types... args)
override;
78 inline bool check(
void* ptr)
override;
81 CallbackFunction g_function;
91template<
class TReturn,
class... Types>
101 template<
typename TLambda>
110 TReturn
call(Types... args)
override;
117 inline bool check(
void* ptr)
override;
121 TReturn (*g_executeLambda)(
void*, Types...);
122 void (*g_deleteLambda)(
void*);
133template<
class TReturn,
class TObject,
class... Types>
137 using CallbackFunctionObject = TReturn (TObject::*)(Types... args);
153 TReturn
call(Types... args)
override;
160 inline bool check(
void* ptr)
override;
163 CallbackFunctionObject g_functionObj;
167template<
class TReturn,
class... Types>
169template<
class TReturn,
class... Types>
180template<
class TReturn,
class TCalleePtr,
class... Types>
183 using CalleePtr = TCalleePtr;
191 [[nodiscard]]
inline static CalleePtr
192 newFunctor(
typename fge::CallbackFunctor<TReturn, Types...>::CallbackFunction func)
204 template<
typename TLambda>
205 [[nodiscard]]
inline static CalleePtr
newLambda(TLambda
const& lambda)
218 template<
class TObject>
219 [[nodiscard]]
inline static CalleePtr
220 newObjectFunctor(
typename fge::CallbackObjectFunctor<TReturn, TObject, Types...>::CallbackFunctionObject func,
242template<
class... Types>
246 using CalleePtr = CalleeUniquePtr<void, Types...>;
249 CallbackHandler() =
default;
250 ~CallbackHandler()
override =
default;
256 fge::Subscription() {};
306 addFunctor(
typename fge::CallbackFunctor<void, Types...>::CallbackFunction func,
318 template<
typename TLambda>
331 template<
class TObject>
333 addObjectFunctor(
typename fge::CallbackObjectFunctor<void, TObject, Types...>::CallbackFunctionObject func,
367 void call(Types... args);
397 _subscriber(subscriber)
402 bool _markedForDeletion =
false;
404 using CalleeList = std::vector<CalleeData>;
406 CalleeList g_callees;
408 mutable std::recursive_mutex g_mutex;
420template<
class... Types>
424 using CalleePtr = CalleeUniquePtr<void, Types...>;
427 UniqueCallbackHandler() =
default;
428 ~UniqueCallbackHandler()
override =
default;
434 fge::UniqueSubscription()
476 setFunctor(
typename fge::CallbackFunctor<void, Types...>::CallbackFunction func,
488 template<
typename TLambda>
501 template<
class TObject>
503 setObjectFunctor(
typename fge::CallbackObjectFunctor<void, TObject, Types...>::CallbackFunctionObject func,
531 void call(Types... args);
548 _subscriber(subscriber)
555 CalleeData g_callee{
nullptr,
nullptr};
557 mutable std::recursive_mutex g_mutex;
562#include "FastEngine/C_callback.inl"
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
TReturn call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:28
CallbackFunctor(CallbackFunction func)
Constructor.
Definition C_callback.inl:23
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:244
void delPtr(void *ptr)
Remove a callback from the list.
Definition C_callback.inl:161
void del(fge::CallbackBase< void, Types... > *callback)
Remove a callback from the list.
Definition C_callback.inl:201
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > &&n)=delete
Move operator prohibited.
CallbackHandler(fge::CallbackHandler< Types... > const &n)
Copy constructor that does nothing.
Definition C_callback.hpp:255
fge::CallbackBase< void, Types... > * add(CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
Add a new callback to the list.
Definition C_callback.inl:108
fge::CallbackObjectFunctor< void, TObject, Types... > * addObjectFunctor(typename fge::CallbackObjectFunctor< void, TObject, Types... >::CallbackFunctionObject func, TObject *object, Subscriber *subscriber=nullptr)
Helper method to add a callback object functor.
Definition C_callback.inl:151
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > const &n)
Copy operator that does nothing.
Definition C_callback.hpp:265
void onDetach(fge::Subscriber *subscriber) override
This method is called when a subscriber is destroyed (destructor called).
Definition C_callback.inl:263
fge::CallbackFunctor< void, Types... > * addFunctor(typename fge::CallbackFunctor< void, Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback functor.
Definition C_callback.inl:135
fge::CallbackLambda< void, Types... > * addLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback lambda.
Definition C_callback.inl:143
void call(Types... args)
Call all the callbacks with the given arguments.
Definition C_callback.inl:220
void hook(fge::CallbackHandler< Types... > &handler, fge::Subscriber *subscriber=nullptr)
Hook a callback handler to this handler.
Definition C_callback.inl:249
void delSub(fge::Subscriber *subscriber)
Remove a callback from the list.
Definition C_callback.inl:179
CallbackHandler(fge::CallbackHandler< Types... > &&n)=delete
Move constructor prohibited.
void clear()
Clear the list of callbacks.
Definition C_callback.inl:97
Callback lambda (with/without capture).
Definition C_callback.hpp:93
CallbackLambda(TLambda const &lambda)
Constructor.
Definition C_callback.inl:62
bool check(void *ptr) override
Always return false.
Definition C_callback.inl:89
TReturn call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:84
Callback functor of a method with an object.
Definition C_callback.hpp:135
TReturn call(Types... args) override
Call the callback method with the given arguments.
Definition C_callback.inl:47
CallbackObjectFunctor(CallbackFunctionObject func, TObject *object)
Constructor.
Definition C_callback.inl:41
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:53
This class is a useful utility to "link" multiple objects around.
Definition C_subscription.hpp:210
This class is a useful utility to "link" multiple objects around a specific use with automatic lifeti...
Definition C_subscription.hpp:125
This class is used for cases where only one callback is needed.
Definition C_callback.hpp:422
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > &&n)=delete
Move operator prohibited.
fge::CallbackFunctor< void, Types... > * setFunctor(typename fge::CallbackFunctor< void, Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
Helper method to set a callback functor.
Definition C_callback.inl:308
UniqueCallbackHandler(fge::UniqueCallbackHandler< Types... > const &n)
Copy constructor that does nothing.
Definition C_callback.hpp:433
void delSub(fge::Subscriber *subscriber)
Remove the callback if the subscriber is the same as the one used to construct the callback.
Definition C_callback.inl:346
void delPtr(void *ptr)
Remove the callback if the function/object pointer is the same as the one used to construct the callb...
Definition C_callback.inl:334
fge::CallbackLambda< void, Types... > * setLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to set a callback lambda.
Definition C_callback.inl:316
fge::CallbackObjectFunctor< void, TObject, Types... > * setObjectFunctor(typename fge::CallbackObjectFunctor< void, TObject, Types... >::CallbackFunctionObject func, TObject *object, Subscriber *subscriber=nullptr)
Helper method to set a callback object functor.
Definition C_callback.inl:324
void call(Types... args)
Call the callback with the given arguments.
Definition C_callback.inl:371
void onDetach(fge::Subscriber *subscriber) override
This method is called when a subscriber is destroyed (destructor called).
Definition C_callback.inl:391
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > const &n)
Copy operator that does nothing.
Definition C_callback.hpp:444
void del(fge::CallbackBase< Types... > *callback)
Remove the callback if the callback pointer is the same as the one used to construct the callback.
Definition C_callback.inl:358
UniqueCallbackHandler(fge::CallbackHandler< Types... > &&n)=delete
Move constructor prohibited.
fge::CallbackBase< void, Types... > * set(CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
Set a new callback to the handler, replacing the previous one if it exists.
Definition C_callback.inl:291
This class allow same functionality as Subscription but only allow one subscriber at a time.
Definition C_subscription.hpp:167
This struct helper is used to create callbacks.
Definition C_callback.hpp:182
static CalleePtr newFunctor(typename fge::CallbackFunctor< TReturn, Types... >::CallbackFunction func)
Helper function to create a new callback functor.
Definition C_callback.hpp:192
static CalleePtr newObjectFunctor(typename fge::CallbackObjectFunctor< TReturn, TObject, Types... >::CallbackFunctionObject func, TObject *object)
Helper function to create a new callback object functor.
Definition C_callback.hpp:220
static CalleePtr newLambda(TLambda const &lambda)
Helper function to create a new callback lambda.
Definition C_callback.hpp:205