Monado OpenXR Runtime
xrt::auxiliary::util::GenericCallbacks< CallbackType, EventBitflagType > Struct Template Reference

A generic collection of callbacks for event types represented as a bitmask, intended to be wrapped for each usage. More...

#include <util/u_generic_callbacks.hpp>

Public Types

using callback_t = CallbackType
 
using event_t = EventBitflagType
 
using mask_t = detail::mask_from_enum_t< EventBitflagType >
 

Public Member Functions

void addCallback (CallbackType callback, mask_t event_mask, void *userdata)
 Add a new callback entry with the given callback function pointer, event mask, and user data. More...
 
int removeCallback (CallbackType callback, mask_t event_mask, void *userdata, unsigned int num_skip=0, int max_remove=-1)
 Remove some number of callback entries matching the given callback function pointer, event mask, and user data. More...
 
bool contains (CallbackType callback, mask_t event_mask, void *userdata)
 See if the collection contains at least one matching callback. More...
 
template<typename F >
int invokeCallbacks (EventBitflagType event, F &&invoker)
 Invokes the callbacks, by passing the ones we should run to your "invoker" to add any desired context/event data and forward the call. More...
 

Detailed Description

template<typename CallbackType, typename EventBitflagType>
struct xrt::auxiliary::util::GenericCallbacks< CallbackType, EventBitflagType >

A generic collection of callbacks for event types represented as a bitmask, intended to be wrapped for each usage.

A registered callback may identify one or more event types (bits in the bitmask) that it wants to be invoked for. A userdata void pointer is also stored for each callback. Bitmasks are tested at invocation time, and the general callback format allows for callbacks to indicate they should be removed from the collection. Actually calling each callback is left to a consumer-provided "invoker" to allow adding context and event data to the call. The "invoker" also allows the option of whether or how to expose the self-removal capability: yours might simply always return "false".

This generic structure supports callbacks that are included multiple times in the collection, if the consuming code needs it. GenericCallbacks::contains may be used by consuming code before conditionally calling addCallback, to limit to a single instance in a collection.

Template Parameters
CallbackTypethe function pointer type to store for each callback.
EventBitflagTypethe event enum type.

Member Function Documentation

◆ addCallback()

template<typename CallbackType , typename EventBitflagType >
void xrt::auxiliary::util::GenericCallbacks< CallbackType, EventBitflagType >::addCallback ( CallbackType  callback,
mask_t  event_mask,
void *  userdata 
)
inline

Add a new callback entry with the given callback function pointer, event mask, and user data.

New callback entries are always added at the end of the collection.

◆ contains()

template<typename CallbackType , typename EventBitflagType >
bool xrt::auxiliary::util::GenericCallbacks< CallbackType, EventBitflagType >::contains ( CallbackType  callback,
mask_t  event_mask,
void *  userdata 
)
inline

See if the collection contains at least one matching callback.

Parameters
callbackThe callback function pointer. Tested for equality with each callback entry.
event_maskThe callback event mask. Tested for equality with each callback entry.
userdataThe opaque user data pointer. Tested for equality with each callback entry.
Returns
true if a matching callback is found.

◆ invokeCallbacks()

template<typename CallbackType , typename EventBitflagType >
template<typename F >
int xrt::auxiliary::util::GenericCallbacks< CallbackType, EventBitflagType >::invokeCallbacks ( EventBitflagType  event,
F &&  invoker 
)
inline

Invokes the callbacks, by passing the ones we should run to your "invoker" to add any desired context/event data and forward the call.

Callbacks are called in order, filtering out those whose event mask does not include the given event.

Parameters
eventThe event type to invoke callbacks for.
invokerA function/functor accepting the event, a callback function pointer, and the callback entry's userdata as parameters, and returning true if the callback should be removed from the collection. It is assumed that the invoker will add any additional context or event data and call the provided callback.

Typically, a lambda with some captures and a single return statement will be sufficient for an invoker.

Returns
the number of callbacks run

◆ removeCallback()

template<typename CallbackType , typename EventBitflagType >
int xrt::auxiliary::util::GenericCallbacks< CallbackType, EventBitflagType >::removeCallback ( CallbackType  callback,
mask_t  event_mask,
void *  userdata,
unsigned int  num_skip = 0,
int  max_remove = -1 
)
inline

Remove some number of callback entries matching the given callback function pointer, event mask, and user data.

Parameters
callbackThe callback function pointer. Tested for equality with each callback entry.
event_maskThe callback event mask. Tested for equality with each callback entry.
userdataThe opaque user data pointer. Tested for equality with each callback entry.
num_skipThe number of matches to skip before starting to remove callbacks. Defaults to 0.
max_removeThe number of matches to remove, or negative if no limit. Defaults to -1.
Returns
the number of callbacks removed.

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