ML Reference
mlEventSource.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2011, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_EVENT_SOURCE_H
14#define ML_EVENT_SOURCE_H
15
18
19#include "mlBase.h"
20
21ML_START_NAMESPACE
22
23class BaseEvent;
24
25typedef void BaseEventCallback(void*, BaseEvent*);
26
27//----------------------------------------------------------------------
31{
32public:
34 EventSource(const EventSource& evSource);
35 ~EventSource() override;
36
39 void addEventListener(BaseEventCallback* cb, void* userData);
40
43 void removeEventListener(BaseEventCallback* cb, void* userData);
44
48 bool hasEventListeners() const;
49
53
54protected:
60 void sendEvent(BaseEvent* event, void* skipListener = nullptr);
61
62private:
64 struct ListenerEntry {
65 ListenerEntry() : cb(nullptr), userData(nullptr) {}
66 ListenerEntry(BaseEventCallback* aCB, void* aUserData) : cb(aCB), userData(aUserData) {}
67 void sendEvent(BaseEvent* event) const { (*cb)(userData, event); }
68 bool isValid() const { return (cb != nullptr); }
69 bool operator==(const ListenerEntry& b) const { return cb == b.cb && userData == b.userData; }
70
72 void* userData;
73 };
74
76 std::vector<ListenerEntry> _eventListeners;
77 bool _inEventListenerIteration;
78 bool _eventListenersRemovedInIteration;
79};
80
81//----------------------------------------------------------------------
84
86{
87public:
88 BaseEvent() : _source(nullptr) {}
89 virtual ~BaseEvent() {}
90
92 EventSource* source() const { return _source; }
93
97
98private:
99 friend class EventSource;
100
102 EventSource* _source;
103};
104
105//----------------------------------------------------------------------
108
116
117
118ML_END_NAMESPACE
119
120
122template<typename ToTypePtr>
123inline ToTypePtr mlbaseevent_cast(ML_UTILS_NAMESPACE::BaseEvent* from) {
124 if (from) {
125 if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
126 return static_cast<ToTypePtr>(from);
127 } else {
128 return NULL;
129 }
130 } else {
131 return NULL;
132 }
133}
134
136template<typename ToTypePtr>
137inline ToTypePtr mlbaseevent_cast(const ML_UTILS_NAMESPACE::BaseEvent* from) {
138 if (from) {
139 if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
140 return static_cast<ToTypePtr>(from);
141 } else {
142 return NULL;
143 }
144 } else {
145 return NULL;
146 }
147}
148
149
150#endif // __mlEventSource_H
151
152
BaseEvent is the base class for all events emitted from EventSourceBase.
virtual ~BaseEvent()
EventSource * source() const
get the Base object emitting this event
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:59
EventSourceRemovedEvent is used to indicate when the EventSourceBase object is removed.
EventSourceBase class adds event listener handling to Base.
~EventSource() override
EventSource(const EventSource &evSource)
void addEventListener(BaseEventCallback *cb, void *userData)
add event listener callback to this Base object - the userData will be the first argument when the ca...
void sendEvent(BaseEvent *event, void *skipListener=nullptr)
Macro for the declaration of the runtime type system methods, defined in mlRuntimeSubClass....
bool hasEventListeners() const
check if any event listeners have been added to this Base object; this can be used to skip the sendEv...
void removeEventListener(BaseEventCallback *cb, void *userData)
remove event listener callback from this Base object - arguments must be the same as for the addEvent...
#define ML_ABSTRACT_CLASS_HEADER(className)
Same like ML_ABSTRACT_CLASS_HEADER_EXPORTED with a non existing export symbol.
ToTypePtr mlbaseevent_cast(ml::BaseEvent *from)
cast operation for safely casting BaseEvents to derived types
#define ML_CLASS_HEADER(className)
Same like ML_CLASS_HEADER_EXPORTED with a non existing export symbol.
#define ML_ABSTRACT_ROOT_CLASS_HEADER(className)
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition mlUtilities.h:20
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition mlMatrix2.h:425
void BaseEventCallback(void *, BaseEvent *)