Nameless Engine
Loading...
Searching...
No Matches
ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)> Class Template Reference

#include <NodeNotificationBroadcaster.hpp>

Inheritance diagram for ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>:
ne::NodeNotificationBroadcasterBase

Public Member Functions

 NodeNotificationBroadcaster (const NodeNotificationBroadcaster &)=delete
 
NodeNotificationBroadcasteroperator= (const NodeNotificationBroadcaster &)=delete
 
void broadcast (FunctionArgs &&... args)
 
size_t subscribe (const NodeFunction< FunctionReturnType(FunctionArgs...)> &callback)
 
void unsubscribe (size_t iBindingId)
 
size_t getSubscriberCount ()
 
- Public Member Functions inherited from ne::NodeNotificationBroadcasterBase
 NodeNotificationBroadcasterBase (const NodeNotificationBroadcasterBase &)=delete
 
NodeNotificationBroadcasterBaseoperator= (const NodeNotificationBroadcasterBase &)=delete
 

Protected Member Functions

virtual void onOwnerNodeSpawning (Node *pOwnerNode) override
 
virtual void onOwnerNodeDespawning (Node *pOwnerNode) override
 
virtual void onOwnerNodeSpawning (Node *pOwnerNode)=0
 
virtual void onOwnerNodeDespawning (Node *pOwnerNode)=0
 

Private Member Functions

void removeAllCallbacks ()
 

Private Attributes

std::pair< std::recursive_mutex, std::unordered_map< size_t, NodeFunction< FunctionReturnType(FunctionArgs...)> > > mtxCallbacks
 
std::pair< std::recursive_mutex, std::unordered_map< size_t, NodeFunction< FunctionReturnType(FunctionArgs...)> > > mtxCallbacksToAdd
 
std::pair< std::recursive_mutex, std::vector< size_t > > mtxCallbacksToRemove
 
std::pair< std::recursive_mutex, Node * > mtxSpawnedOwnerNode
 
std::atomic< size_t > iAvailableBindingId {0}
 
std::atomic_flag bIsBroadcasting {}
 

Friends

class Node
 

Detailed Description

template<typename FunctionReturnType, typename... FunctionArgs>
class ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>

Implements publisher-subscriber pattern. Allows nodes to subscribe by specifying their callback functions via NodeFunction and triggers them once the broadcast method is called.

Member Function Documentation

◆ broadcast()

template<typename FunctionReturnType , typename... FunctionArgs>
void ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::broadcast ( FunctionArgs &&...  args)
inline

Executes all registered (subscribed) callbacks.

Remarks
It's safe to call this function while your node is despawned, in this case the call will be ignored and nothing will be broadcasted.
Additionally, before running registered callbacks, removes callbacks of despawned nodes.
Parameters
argsArguments to pass to subscribed callbacks.

◆ getSubscriberCount()

template<typename FunctionReturnType , typename... FunctionArgs>
size_t ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::getSubscriberCount ( )
inline

Returns the current estimated number of subscribers.

The returned number is called "estimated" because right now we don't know if some nodes that subscribed to this broadcaster have despawned or not, callbacks of despawned nodes are only removed in broadcast calls. The only thing that we can say for sure is that the actual number of spawned subscribers is either equal to the returned value or smaller than it.

Returns
Estimated number of spawned subscribers.

◆ onOwnerNodeDespawning()

template<typename FunctionReturnType , typename... FunctionArgs>
virtual void ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::onOwnerNodeDespawning ( Node pOwnerNode)
inlineoverrideprotectedvirtual

Called by the node, that owns this broadcaster, when it's despawning.

Parameters
pOwnerNodeNode that owns this broadcaster.

Implements ne::NodeNotificationBroadcasterBase.

◆ onOwnerNodeSpawning()

template<typename FunctionReturnType , typename... FunctionArgs>
virtual void ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::onOwnerNodeSpawning ( Node pOwnerNode)
inlineoverrideprotectedvirtual

Called by the node, that owns this broadcaster, when it's spawning.

Parameters
pOwnerNodeSpawned node that owns this broadcaster.

Implements ne::NodeNotificationBroadcasterBase.

◆ removeAllCallbacks()

template<typename FunctionReturnType , typename... FunctionArgs>
void ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::removeAllCallbacks ( )
inlineprivate

Removes all registered callbacks (including callbacks that are pending to be added/removed).

◆ subscribe()

template<typename FunctionReturnType , typename... FunctionArgs>
size_t ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::subscribe ( const NodeFunction< FunctionReturnType(FunctionArgs...)> &  callback)
inline

Adds the specified callback to be registered in the broadcaster so that the callback will be triggered on the next broadcast call.

Parameters
callbackCallback to register.
Returns
Unique ID (only unique relative to this broadcaster) of the registered callback. You can save this ID if you would need to unsubscribe later (see unsubscribe), otherwise just ignore this returned value.

◆ unsubscribe()

template<typename FunctionReturnType , typename... FunctionArgs>
void ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::unsubscribe ( size_t  iBindingId)
inline

Removes a previously added callback (see subscribe) by its binding ID.

Remarks
You don't need to unsubscribe when your subscribed node is being despawned/destroyed as this is done automatically. Each broadcast call removes callbacks of despawned nodes.
Parameters
iBindingIdID of the binding to remove.

Member Data Documentation

◆ bIsBroadcasting

template<typename FunctionReturnType , typename... FunctionArgs>
std::atomic_flag ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::bIsBroadcasting {}
private

Determines whether we are currently broadcasting or not.

◆ iAvailableBindingId

template<typename FunctionReturnType , typename... FunctionArgs>
std::atomic<size_t> ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::iAvailableBindingId {0}
private

Stores the next free (available for use) binding ID.

◆ mtxCallbacks

template<typename FunctionReturnType , typename... FunctionArgs>
std::pair< std::recursive_mutex, std::unordered_map<size_t, NodeFunction<FunctionReturnType(FunctionArgs...)> > > ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::mtxCallbacks
private

Stores map that should be used with the mutex. Map contains pairs of "binding ID" - "callback".

◆ mtxCallbacksToAdd

template<typename FunctionReturnType , typename... FunctionArgs>
std::pair< std::recursive_mutex, std::unordered_map<size_t, NodeFunction<FunctionReturnType(FunctionArgs...)> > > ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::mtxCallbacksToAdd
private

Stores map that should be used with the mutex. Map contains pairs of "binding ID" - "callback" to add to mtxCallbacks.

◆ mtxCallbacksToRemove

template<typename FunctionReturnType , typename... FunctionArgs>
std::pair<std::recursive_mutex, std::vector<size_t> > ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::mtxCallbacksToRemove
private

Stores array that should be used with the mutex. Array contains binding IDs to remove from mtxCallbacks.

◆ mtxSpawnedOwnerNode

template<typename FunctionReturnType , typename... FunctionArgs>
std::pair<std::recursive_mutex, Node*> ne::NodeNotificationBroadcaster< FunctionReturnType(FunctionArgs...)>::mtxSpawnedOwnerNode
private

Information about the owner node.


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