FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
fge::CallbackHandler< Types > Class Template Reference

This class is used to handle callbacks in a safe way. More...

#include <C_callback.hpp>

Inheritance diagram for fge::CallbackHandler< Types >:
fge::Subscription

Public Types

using CalleePtr = CalleeUniquePtr<Types...>
 
using SubscriberCount = unsigned int
 

Public Member Functions

 CallbackHandler (fge::CallbackHandler< Types... > const &n)
 Copy constructor that does nothing.
 
 CallbackHandler (fge::CallbackHandler< Types... > &&n)=delete
 Move constructor prohibited.
 
fge::CallbackHandler< Types... > & operator= (fge::CallbackHandler< Types... > const &n)
 Copy operator that does nothing.
 
fge::CallbackHandler< Types... > & operator= (fge::CallbackHandler< Types... > &&n)=delete
 Move operator prohibited.
 
void clear ()
 Clear the list of callbacks.
 
fge::CallbackBase< Types... > * add (CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
 Add a new callback to the list.
 
fge::CallbackFunctor< Types... > * addFunctor (typename fge::CallbackFunctor< Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
 Helper method to add a callback functor.
 
template<typename TLambda >
fge::CallbackLambda< Types... > * addLambda (TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
 Helper method to add a callback lambda.
 
template<class TObject >
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.
 
void delPtr (void *ptr)
 Remove a callback from the list.
 
void delSub (fge::Subscriber *subscriber)
 Remove a callback from the list.
 
void del (fge::CallbackBase< Types... > *callback)
 Remove a callback from the list.
 
void call (Types... args)
 Call all the callbacks with the given arguments.
 
void hook (fge::CallbackHandler< Types... > &handler, fge::Subscriber *subscriber=nullptr)
 Hook a callback handler to this handler.
 

Protected Member Functions

void onDetach (fge::Subscriber *subscriber) override
 This method is called when a subscriber is destroyed (destructor called)
 
void detachAll ()
 Detach all subscribers.
 
bool detach (fge::Subscriber *subscriber)
 Detach a specific subscriber.
 
fge::Subscription::SubscriberCount detachOnce (fge::Subscriber *subscriber)
 Detach only once a specific subscriber.
 
fge::Subscription::SubscriberCount attach (fge::Subscriber *subscriber)
 Attach a specific subscriber.
 
fge::Subscription::SubscriberCount getCount (fge::Subscriber *subscriber) const
 Get the SubscriberCount of a specific subscriber.
 

Detailed Description

template<class... Types>
class fge::CallbackHandler< Types >

This class is used to handle callbacks in a safe way.

Every callback muse use the same template parameters Types than a handler. This class is thread-safe.

This class inherits from Subscription to be able to subscribe to it. When a subscriber is added to a handler and is destroyed, all the callbacks related to this subscriber are automatically removed.

See also
Subscription
Template Parameters
TypesThe list of arguments types passed to the callbacks

Constructor & Destructor Documentation

◆ CallbackHandler() [1/2]

template<class... Types>
fge::CallbackHandler< Types >::CallbackHandler ( fge::CallbackHandler< Types... > const & n)
inline

Copy constructor that does nothing.

◆ CallbackHandler() [2/2]

template<class... Types>
fge::CallbackHandler< Types >::CallbackHandler ( fge::CallbackHandler< Types... > && n)
delete

Move constructor prohibited.

Member Function Documentation

◆ add()

template<class... Types>
fge::CallbackBase< Types... > * fge::CallbackHandler< Types >::add ( CalleePtr && callback,
fge::Subscriber * subscriber = nullptr )
inline

Add a new callback to the list.

A subscriber can be passed to this function to "categorize" the callback. The subscriber will be used to remove the group of callbacks related to him later.

You can pass a nullptr to the subscriber parameter if you don't want to use it, if so the callback will be added to the default group.

To facilitate your code, you can use the helper methods addFunctor(), addLambda() and addObjectFunctor().

Parameters
callbackThe new callback to add
subscriberThe subscriber to use to categorize the callback
Returns
The callback pointer

◆ addFunctor()

template<class... Types>
fge::CallbackFunctor< Types... > * fge::CallbackHandler< Types >::addFunctor ( typename fge::CallbackFunctor< Types... >::CallbackFunction func,
fge::Subscriber * subscriber = nullptr )
inline

Helper method to add a callback functor.

See also
add()
Parameters
funcThe callback function
subscriberThe subscriber to use to categorize the callback
Returns
The callback pointer

◆ addLambda()

template<class... Types>
template<typename TLambda >
fge::CallbackLambda< Types... > * fge::CallbackHandler< Types >::addLambda ( TLambda const & lambda,
fge::Subscriber * subscriber = nullptr )
inline

Helper method to add a callback lambda.

See also
add()
Template Parameters
TLambdaThe lambda type
Parameters
lambdaThe callback lambda
subscriberThe subscriber to use to categorize the callback
Returns
The callback pointer

◆ addObjectFunctor()

template<class... Types>
template<class TObject >
fge::CallbackObjectFunctor< TObject, Types... > * fge::CallbackHandler< Types >::addObjectFunctor ( typename fge::CallbackObjectFunctor< TObject, Types... >::CallbackFunctionObject func,
TObject * object,
Subscriber * subscriber = nullptr )
inline

Helper method to add a callback object functor.

See also
add()
Template Parameters
TObjectThe object type
Parameters
funcThe callback method of the object
objectThe object pointer
subscriberThe subscriber to use to categorize the callback
Returns
The callback pointer

◆ attach()

fge::Subscription::SubscriberCount fge::Subscription::attach ( fge::Subscriber * subscriber)
protectedinherited

Attach a specific subscriber.

You can't directly attach a nullptr global scope subscriber. But if you do, this function will do nothing and return 1 as the SubscriberCount.

Parameters
subscriberThe subscriber to attach
Returns
The SubscriberCount

◆ call()

template<class... Types>
void fge::CallbackHandler< Types >::call ( Types... args)

Call all the callbacks with the given arguments.

Parameters
argsThe list of arguments

◆ clear()

template<class... Types>
void fge::CallbackHandler< Types >::clear ( )
inline

Clear the list of callbacks.

◆ del()

template<class... Types>
void fge::CallbackHandler< Types >::del ( fge::CallbackBase< Types... > * callback)

Remove a callback from the list.

Remove a callback with the specified pointer.

Parameters
callbackThe callback to remove

◆ delPtr()

template<class... Types>
void fge::CallbackHandler< Types >::delPtr ( void * ptr)

Remove a callback from the list.

You can remove a callback with the specified function/object pointer.

Parameters
ptrThe function/object pointer to remove

◆ delSub()

template<class... Types>
void fge::CallbackHandler< Types >::delSub ( fge::Subscriber * subscriber)

Remove a callback from the list.

Remove a group of callbacks with the specified subscriber or nullptr for the default group.

Parameters
subscriberThe subscriber associated to the group of callbacks

◆ detach()

bool fge::Subscription::detach ( fge::Subscriber * subscriber)
protectedinherited

Detach a specific subscriber.

Detaching a nullptr global scope subscriber will do nothing.

Parameters
subscriberThe subscriber to detach
Returns
true if the subscriber was detached, false otherwise

◆ detachAll()

void fge::Subscription::detachAll ( )
protectedinherited

Detach all subscribers.

◆ detachOnce()

fge::Subscription::SubscriberCount fge::Subscription::detachOnce ( fge::Subscriber * subscriber)
protectedinherited

Detach only once a specific subscriber.

You can attach a subscriber multiple times and it will augment the SubscriberCount. This function will only detach one time the subscriber and decrement the SubscriberCount. If the SubscriberCount is 0, the subscriber will be detached.

Parameters
subscriberThe subscriber to detach
Returns
The remaining SubscriberCount

◆ getCount()

fge::Subscription::SubscriberCount fge::Subscription::getCount ( fge::Subscriber * subscriber) const
protectedinherited

Get the SubscriberCount of a specific subscriber.

Parameters
subscriberThe subscriber to get the SubscriberCount
Returns
The SubscriberCount

◆ hook()

template<class... Types>
void fge::CallbackHandler< Types >::hook ( fge::CallbackHandler< Types... > & handler,
fge::Subscriber * subscriber = nullptr )

Hook a callback handler to this handler.

This will propagate the calls from this handler to the given handler of the same type.

Warning
The other handler must remain valid during the lifetime of this handler as it is not cleared automatically.
Parameters
handlerAnother handler of the same type
subscriberThe subscriber associated with this handler

◆ onDetach()

template<class... Types>
void fge::CallbackHandler< Types >::onDetach ( fge::Subscriber * subscriber)
overrideprotectedvirtual

This method is called when a subscriber is destroyed (destructor called)

This avoid calling the callbacks when the subscriber is destroyed.

Parameters
subscriberThe subscriber that is destroyed (or going to be destroyed)

Implements fge::Subscription.

◆ operator=() [1/2]

template<class... Types>
fge::CallbackHandler< Types... > & fge::CallbackHandler< Types >::operator= ( fge::CallbackHandler< Types... > && n)
delete

Move operator prohibited.

◆ operator=() [2/2]

template<class... Types>
fge::CallbackHandler< Types... > & fge::CallbackHandler< Types >::operator= ( fge::CallbackHandler< Types... > const & n)
inline

Copy operator that does nothing.


The documentation for this class was generated from the following files: