Go to the documentation of this file.
9 #ifndef CscCoreEvents_h
10 #define CscCoreEvents_h
15 #include <unordered_map>
17 #include <condition_variable>
19 using std::unordered_map, std::type_info, std::reference_wrapper, std::function, std::mutex, std::unique_lock, std::condition_variable;
38 template <
class THandlerClass,
class TEvent>
41 typedef void (THandlerClass::*
MemberFunc)(
const TEvent *,
bool &unregisterAfter);
48 virtual void exec(
const CscCoreEvent* event,
bool &unregisterAfter)
override;
50 THandlerClass* handlerInstance;
52 bool deleteObjectOnDestruct;
55 template <
class THandlerClass,
class TEvent>
57 (handlerInstance->*handlerMethod)(
static_cast<const TEvent*
>(event), unregisterAfter);
61 template <
class THandlerClass,
class TEvent>
63 handlerInstance(handlerInstance), handlerMethod(handlerMethod), deleteObjectOnDestruct(deleteObjectOnDestruct) {
66 template <
class THandlerClass,
class TEvent>
68 if (deleteObjectOnDestruct) {
69 delete handlerInstance;
84 template <
class TEvent>
87 template <
class TEvent>
90 void handle(
const TEvent* theEvent,
bool &unregisterAfter);
96 template <
class TEvent>
98 call(theEvent, unregisterAfter);
101 template <
class TEvent>
123 template <
class TObject,
class TEvent>
124 void registerHandler(
const string &handlerId, TObject *handlerObject,
void (TObject::*handlerMethod)(
const TEvent *event,
bool &unregisterAfter),
bool deleteObjectOnDestruct =
false) {
126 handlersMutex.lock();
127 map<string, HandlerFunctionBase*> &eventHandlersById = handlers[eventType];
128 if (eventHandlersById.find(handlerId) != eventHandlersById.end()) {
129 logger->warn(
"event handler " + handlerId +
", already exists - force unregister first");
130 delete eventHandlersById[handlerId];
131 eventHandlersById.erase(handlerId);
134 handlersMutex.unlock();
142 template <
class TEvent>
151 template <
class TEvent>
162 mutex eventQueueMutex;
163 condition_variable eventQueueConditionVariable;
164 vector<const CscCoreEvent *> eventQueue;
165 void notifyHandlersThread();
172 std::unique_ptr<CscLogger> logger;
void handle(const TEvent *theEvent, bool &unregisterAfter)
Definition: CscCoreEvents.h:97
void registerHandler(const string &handlerId, TObject *handlerObject, void(TObject::*handlerMethod)(const TEvent *event, bool &unregisterAfter), bool deleteObjectOnDestruct=false)
Definition: CscCoreEvents.h:124
Definition: CscCoreEvents.h:39
LambdaHandlerWrapper(HandleEventFunction< TEvent > call)
Definition: CscCoreEvents.h:102
static CscCoreEvents * getInstance()
Definition: CscCoreEvents.cpp:84
MemberFunctionHandler(THandlerClass *handlerInstance, MemberFunc handlerMethod, bool deleteObjectOnDestruct)
Definition: CscCoreEvents.h:62
void unregister(const string &handlerId)
Definition: CscCoreEvents.h:152
Definition: CscCoreEvents.h:77
function< void(const TEvent *event, bool &unregisterAfter)> HandleEventFunction
Definition: CscCoreEvents.h:85
~MemberFunctionHandler()
Definition: CscCoreEvents.h:67
void publish(const CscCoreEvent *event)
Definition: CscCoreEvents.cpp:92
Definition: CscCoreEvents.cpp:14
void registerHandler(const string &handlerId, HandleEventFunction< TEvent > call)
Definition: CscCoreEvents.h:143
Definition: CscCoreEvents.h:23
virtual ~HandlerFunctionBase()
Definition: CscCoreEvents.cpp:19
Definition: CscCoreEvents.h:74
reference_wrapper< const type_info > TypeInfoRef
Definition: CscCoreEvents.h:73
j template void())
Definition: json.hpp:4189
Definition: CscCoreEvents.h:88
void(THandlerClass::* MemberFunc)(const TEvent *, bool &unregisterAfter)
Definition: CscCoreEvents.h:41
bool operator()(TypeInfoRef lhs, TypeInfoRef rhs) const
Definition: CscCoreEvents.cpp:26
virtual void exec(const CscCoreEvent *event, bool &unregisterAfter)=0
virtual ~CscCoreEvent()
Definition: CscCoreEvents.cpp:16
virtual void exec(const CscCoreEvent *event, bool &unregisterAfter) override
Definition: CscCoreEvents.h:56
Definition: CscCoreEvents.h:28
Definition: CscCoreEvents.h:108
size_t operator()(TypeInfoRef code) const
Definition: CscCoreEvents.cpp:22