Monado OpenXR Runtime
|
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... | |
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.
CallbackType | the function pointer type to store for each callback. |
EventBitflagType | the event enum type. |
|
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.
Referenced by android_lifecycle_callbacks::android_lifecycle_callbacks_register_callback().
|
inline |
See if the collection contains at least one matching callback.
callback | The callback function pointer. Tested for equality with each callback entry. |
event_mask | The callback event mask. Tested for equality with each callback entry. |
userdata | The opaque user data pointer. Tested for equality with each callback entry. |
|
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.
event | The event type to invoke callbacks for. |
invoker | A 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.
|
inline |
Remove some number of callback entries matching the given callback function pointer, event mask, and user data.
callback | The callback function pointer. Tested for equality with each callback entry. |
event_mask | The callback event mask. Tested for equality with each callback entry. |
userdata | The opaque user data pointer. Tested for equality with each callback entry. |
num_skip | The number of matches to skip before starting to remove callbacks. Defaults to 0. |
max_remove | The number of matches to remove, or negative if no limit. Defaults to -1. |
Referenced by android_lifecycle_callbacks::android_lifecycle_callbacks_remove_callback().