MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
22
23class BaseEvent;
24
25typedef void BaseEventCallback(void*, BaseEvent*);
26
27//----------------------------------------------------------------------
31{
32public:
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 {
66 ListenerEntry()
67 : cb(nullptr)
68 , userData(nullptr)
69 {}
70 ListenerEntry(BaseEventCallback* aCB, void* aUserData)
71 : cb(aCB)
72 , userData(aUserData)
73 {}
74 void sendEvent(BaseEvent* event) const
75 {
76 (*cb)(userData, event);
77 }
78 bool isValid() const
79 {
80 return (cb != nullptr);
81 }
82 bool operator==(const ListenerEntry& b) const
83 {
84 return cb == b.cb && userData == b.userData;
85 }
86
88 void* userData;
89 };
90
92 std::vector<ListenerEntry> _eventListeners;
93 bool _inEventListenerIteration;
94 bool _eventListenersRemovedInIteration;
95};
96
97//----------------------------------------------------------------------
100
102{
103public:
105 : _source(nullptr)
106 {}
107 virtual ~BaseEvent()
108 {}
109
112 {
113 return _source;
114 }
115
119
120private:
121 friend class EventSource;
122
124 EventSource* _source;
125};
126
127//----------------------------------------------------------------------
130
139
140
142
143
145template <typename ToTypePtr> inline ToTypePtr mlbaseevent_cast(ML_UTILS_NAMESPACE::BaseEvent* from)
146{
147 if (from) {
148 if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
149 return static_cast<ToTypePtr>(from);
150 }
151 else {
152 return NULL;
153 }
154 }
155 else {
156 return NULL;
157 }
158}
159
161template <typename ToTypePtr> inline ToTypePtr mlbaseevent_cast(const ML_UTILS_NAMESPACE::BaseEvent* from)
162{
163 if (from) {
164 if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
165 return static_cast<ToTypePtr>(from);
166 }
167 else {
168 return NULL;
169 }
170 }
171 else {
172 return NULL;
173 }
174}
175
176
177#endif // __mlEventSource_H
BaseEvent is the base class for all events emitted from EventSourceBase.
virtual ~BaseEvent()
EventSource * source() const
Returns the Base object emitting this event.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:63
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)
Adds event listener callback to this Base object.
void sendEvent(BaseEvent *event, void *skipListener=nullptr)
Macro for the declaration of the runtime type system methods, defined in mlRuntimeSubClass....
bool hasEventListeners() const
Checks whether any event listeners have been added to this Base object; this can be used to skip the ...
void removeEventListener(BaseEventCallback *cb, void *userData)
Removes event listener callback from this Base object.
#define ML_ABSTRACT_CLASS_HEADER(className)
Similar to 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.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
#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
Definition mlUtilities.h:18
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Returns true if yes.
Definition mlMatrix2.h:399
void BaseEventCallback(void *, BaseEvent *)