pianod2
multisource multiuser scriptable networked music player
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CallbackManager< TOwner, TCallbacks, TCallbackClientId > Class Template Reference

A template class to manage callback, delegate and notification functions. More...

#include <callback.h>

Public Member Functions

void subscribe (const TCallbackClientId client_id, const TCallbacks &handlers)
 Register callback, delegate, or notification functions. More...
 
void unsubscribe (const TCallbackClientId client_id)
 Remove registered callbacks, delegates, or notifications. More...
 

Private Member Functions

template<typename TMemberFunction , typename... Arguments>
void notify (TMemberFunction function, const Arguments &... arguments)
 Invoke a notification function. More...
 
template<typename TMemberFunction , typename... Arguments>
void callback (TMemberFunction function, Arguments &&... arguments)
 Invoke a callback function. More...
 
template<typename TReturnType , typename TMemberFunction , typename... Arguments>
TReturnType aggregate (TReturnType result, const std::function< TReturnType(TReturnType, TReturnType)> &combine, TMemberFunction function, const Arguments &... arguments)
 Invoke a delegate function and aggregate the results. More...
 
template<typename TMemberFunction , typename... Arguments>
bool queryUnanimousApproval (bool default_result, TMemberFunction function, const Arguments &... arguments)
 Check for approval from all delegates. More...
 
template<typename TMemberFunction , typename... Arguments>
bool queryAnyApproval (bool default_result, TMemberFunction function, const Arguments &... arguments)
 Check for approval by any delegate. More...
 

Private Attributes

friend TOwner
 
std::unordered_map< TCallbackClientId, TCallbacks > registered_handlers
 The list of subscribers to the callbacks. More...
 

Detailed Description

template<typename TOwner, typename TCallbacks, typename TCallbackClientId = void *>
class CallbackManager< TOwner, TCallbacks, TCallbackClientId >

A template class to manage callback, delegate and notification functions.

Template Parameters
TOwnerCallbackManager is expected to be a nested class. TOwner is the enclosing class.
TCallbacksA structure with callback hooks as members.
TCallbackClientIdUnique ID to allow update or removal of hooks. Typically the caller's 'this'.

Member Function Documentation

◆ aggregate()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
template<typename TReturnType , typename TMemberFunction , typename... Arguments>
TReturnType CallbackManager< TOwner, TCallbacks, TCallbackClientId >::aggregate ( TReturnType  default_result,
const std::function< TReturnType(TReturnType, TReturnType)> &  combine,
TMemberFunction  function,
const Arguments &...  arguments 
)
private

Invoke a delegate function and aggregate the results.

Template Parameters
TReturnTypeThe type returned by the delegates, and aggregated and returned by this method.
Parameters
default_resultThe result returned if there are no registered delegates.
combineA function that combines results when there are multiple delegates.
functionA TCallbacks member selector for the callback to be invoked.
argumentsAny arguments to be passed to the callback functions.

◆ callback()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
template<typename TMemberFunction , typename... Arguments>
void CallbackManager< TOwner, TCallbacks, TCallbackClientId >::callback ( TMemberFunction  function,
Arguments &&...  arguments 
)
private

Invoke a callback function.

Parameters
functionA TCallbacks member selector for the callback to be invoked.
argumentsAny arguments to be passed to the callback functions.

◆ notify()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
template<typename TMemberFunction , typename... Arguments>
void CallbackManager< TOwner, TCallbacks, TCallbackClientId >::notify ( TMemberFunction  function,
const Arguments &...  arguments 
)
private

Invoke a notification function.

Parameters
functionA TCallbacks member selector for the callback to be invoked.
argumentsAny arguments to be passed to the callback functions.

◆ queryAnyApproval()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
template<typename TMemberFunction , typename... Arguments>
bool CallbackManager< TOwner, TCallbacks, TCallbackClientId >::queryAnyApproval ( bool  default_result,
TMemberFunction  function,
const Arguments &...  arguments 
)
inlineprivate

Check for approval by any delegate.

Parameters
default_resultThe result in the event there are no delegates.
functionA TCallbacks member selector for the delegate to be queried.
argumentsAny arguments to be passed to delegates.
Returns
True if any delegate returns true, false otherwise.

◆ queryUnanimousApproval()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
template<typename TMemberFunction , typename... Arguments>
bool CallbackManager< TOwner, TCallbacks, TCallbackClientId >::queryUnanimousApproval ( bool  default_result,
TMemberFunction  function,
const Arguments &...  arguments 
)
inlineprivate

Check for approval from all delegates.

All delegates are queried.

Parameters
default_resultThe result in the event there are no delegates.
functionA TCallbacks member selector for the delegate to be queried.
argumentsAny arguments to be passed to delegates.
Returns
True if all delegates return true, false if any return false.

◆ subscribe()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
void CallbackManager< TOwner, TCallbacks, TCallbackClientId >::subscribe ( const TCallbackClientId  client_id,
const TCallbacks &  handlers 
)

Register callback, delegate, or notification functions.

Parameters
client_idA unique ID for the client. Suggest using its 'this' object value.
handlersThe handler functions to invoke for callbacks.

◆ unsubscribe()

template<typename TOwner , typename TCallbacks , typename TCallbackClientId >
void CallbackManager< TOwner, TCallbacks, TCallbackClientId >::unsubscribe ( const TCallbackClientId  client_id)

Remove registered callbacks, delegates, or notifications.

Parameters
client_idThe ID the client registered its callback with.

Member Data Documentation

◆ registered_handlers

template<typename TOwner , typename TCallbacks , typename TCallbackClientId = void *>
std::unordered_map<TCallbackClientId, TCallbacks> CallbackManager< TOwner, TCallbacks, TCallbackClientId >::registered_handlers
private

The list of subscribers to the callbacks.

◆ TOwner

template<typename TOwner , typename TCallbacks , typename TCallbackClientId = void *>
friend CallbackManager< TOwner, TCallbacks, TCallbackClientId >::TOwner
private

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