OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CallbackManager< IDType, CallbackType, ContextType > Class Template Reference

#include <CallbackManager.h>

Public Member Functions

IDType registerCallback (CallbackType callback)
 
void unregisterCallback (IDType id)
 
void notify (const ContextType &context)
 
bool isEmpty () const
 

Private Attributes

std::map< IDType, CallbackType > callbacks_
 
IDType next_id_
 
bool is_notifying_
 
std::vector< IDType > deferred_unregister_ids_
 

Detailed Description

template<typename IDType, typename CallbackType, typename ContextType>
class CallbackManager< IDType, CallbackType, ContextType >

Template class for managing notification callbacks

IDType must be an intergral type CallbackType must be a callable type: lambda, functor, etc ContextType can be any basic type, struct, or class

Notification broadcast is in registration order IDs DO NOT wrap around, and a free-list is not used, so IDType must not be exhaustible

Unregistering a callback during notification handling will defer unregistration until notification completes

Example:

struct MyContextType { int value; ... }; using MyCallbackType = std::function<void(const MyContextType&)>; using MyCallbackID = uint32_t;

class SomeClass { CallbackManager<MyCallbackID, MyCallbackType, MyContextType> callbacks;

SomeClass() { callbacks.registerCallback([this](const MyContextType& context) { handleCallback(context); } ); }

void handleCallback(const MyContextType& context) { doStuff(context.value); } };

Definition at line 63 of file CallbackManager.h.

Member Function Documentation

template<typename IDType , typename CallbackType , typename ContextType >
bool CallbackManager< IDType, CallbackType, ContextType >::isEmpty ( ) const
inline

Definition at line 102 of file CallbackManager.h.

References CallbackManager< IDType, CallbackType, ContextType >::callbacks_.

102 { return callbacks_.empty(); }
std::map< IDType, CallbackType > callbacks_
template<typename IDType , typename CallbackType , typename ContextType >
void CallbackManager< IDType, CallbackType, ContextType >::notify ( const ContextType &  context)
inline

Definition at line 86 of file CallbackManager.h.

References CallbackManager< IDType, CallbackType, ContextType >::callbacks_, CallbackManager< IDType, CallbackType, ContextType >::deferred_unregister_ids_, CallbackManager< IDType, CallbackType, ContextType >::is_notifying_, and CallbackManager< IDType, CallbackType, ContextType >::unregisterCallback().

86  {
87  is_notifying_ = true;
88  for (auto callback_pair : callbacks_) {
89  callback_pair.second(context);
90  }
91  is_notifying_ = false;
92 
93  // Unregister any callbacks that tried to unregister during notification
94  if (!deferred_unregister_ids_.empty()) {
95  for (auto id : deferred_unregister_ids_) {
97  }
98  deferred_unregister_ids_.clear();
99  }
100  }
std::map< IDType, CallbackType > callbacks_
std::vector< IDType > deferred_unregister_ids_
void unregisterCallback(IDType id)

+ Here is the call graph for this function:

template<typename IDType , typename CallbackType , typename ContextType >
IDType CallbackManager< IDType, CallbackType, ContextType >::registerCallback ( CallbackType  callback)
inline

Definition at line 70 of file CallbackManager.h.

References CallbackManager< IDType, CallbackType, ContextType >::callbacks_, CHECK_LT, and CallbackManager< IDType, CallbackType, ContextType >::next_id_.

70  {
71  CHECK_LT(next_id_, std::numeric_limits<IDType>::max());
72  callbacks_.emplace(next_id_++, callback);
73  return next_id_ - 1;
74  }
std::map< IDType, CallbackType > callbacks_
#define CHECK_LT(x, y)
Definition: Logger.h:303
template<typename IDType , typename CallbackType , typename ContextType >
void CallbackManager< IDType, CallbackType, ContextType >::unregisterCallback ( IDType  id)
inline

Definition at line 76 of file CallbackManager.h.

References CallbackManager< IDType, CallbackType, ContextType >::callbacks_, CHECK, CallbackManager< IDType, CallbackType, ContextType >::deferred_unregister_ids_, and CallbackManager< IDType, CallbackType, ContextType >::is_notifying_.

Referenced by CallbackManager< IDType, CallbackType, ContextType >::notify().

76  {
77  CHECK(callbacks_.count(id)) << "Callback id not found";
78  if (is_notifying_) {
79  // Defer unregistration until notification loop is complete
80  deferred_unregister_ids_.push_back(id);
81  } else {
82  callbacks_.erase(id);
83  }
84  }
std::map< IDType, CallbackType > callbacks_
std::vector< IDType > deferred_unregister_ids_
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the caller graph for this function:

Member Data Documentation

template<typename IDType , typename CallbackType , typename ContextType >
std::map<IDType, CallbackType> CallbackManager< IDType, CallbackType, ContextType >::callbacks_
private
template<typename IDType , typename CallbackType , typename ContextType >
std::vector<IDType> CallbackManager< IDType, CallbackType, ContextType >::deferred_unregister_ids_
private
template<typename IDType , typename CallbackType , typename ContextType >
bool CallbackManager< IDType, CallbackType, ContextType >::is_notifying_
private
template<typename IDType , typename CallbackType , typename ContextType >
IDType CallbackManager< IDType, CallbackType, ContextType >::next_id_
private

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