ML Reference
mlEventSource.h
Go to the documentation of this file.
1 // **InsertLicense** code
2 //-------------------------------------------------------------------------
5 
10 //-------------------------------------------------------------------------
11 #ifndef __mlEventSource_H
12 #define __mlEventSource_H
13 
14 #include "mlBase.h"
15 
16 ML_START_NAMESPACE
17 
18 class BaseEvent;
19 
20 typedef void BaseEventCallback(void*, BaseEvent*);
21 
22 //----------------------------------------------------------------------
26 {
27 public:
29  EventSource(const EventSource& evSource) : Base(evSource) {}
30  virtual ~EventSource();
31 
34  void addEventListener(BaseEventCallback* cb, void* userData);
35 
38  void removeEventListener(BaseEventCallback* cb, void* userData);
39 
43  bool hasEventListeners() const;
44 
48 
49 protected:
55  void sendEvent(BaseEvent* event, void* skipListener = NULL);
56 
57 private:
59  struct ListenerEntry {
60  ListenerEntry(BaseEventCallback* aCB, void* aUserData) : cb(aCB), userData(aUserData) {}
61  void sendEvent(BaseEvent* event) const { (*cb)(userData, event); }
62  bool operator==(const ListenerEntry& b) const { return cb == b.cb && userData == b.userData; }
63 
65  void* userData;
66  };
67 
69  std::list<ListenerEntry> _eventListeners;
70 };
71 
72 //----------------------------------------------------------------------
75 
77 {
78 public:
79  BaseEvent() : _source(NULL) {}
80  virtual ~BaseEvent() {}
81 
83  EventSource* source() const { return _source; }
84 
88 
89 private:
90  friend class EventSource;
91 
93  EventSource* _source;
94 };
95 
96 //----------------------------------------------------------------------
99 
101 {
102 public:
104 
106 };
107 
108 
109 ML_END_NAMESPACE
110 
111 
113 template<typename ToTypePtr>
114 inline ToTypePtr mlbaseevent_cast(ML_UTILS_NAMESPACE::BaseEvent* from) {
115  if (from) {
116  if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
117  return static_cast<ToTypePtr>(from);
118  } else {
119  return NULL;
120  }
121  } else {
122  return NULL;
123  }
124 }
125 
127 template<typename ToTypePtr>
128 inline ToTypePtr mlbaseevent_cast(const ML_UTILS_NAMESPACE::BaseEvent* from) {
129  if (from) {
130  if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
131  return static_cast<ToTypePtr>(from);
132  } else {
133  return NULL;
134  }
135  } else {
136  return NULL;
137  }
138 }
139 
140 
141 #endif // __mlEventSource_H
142 
143 
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition: mlMatrix2.h:445
Class representing general ML objects that support import/export via strings (setPersistentState() an...
EventSource(const EventSource &evSource)
Definition: mlEventSource.h:29
void BaseEventCallback(void *, BaseEvent *)
Definition: mlEventSource.h:20
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
EventSourceBase class adds event listener handling to Base.
Definition: mlEventSource.h:25
EventSource * source() const
get the Base object emitting this event
Definition: mlEventSource.h:83
virtual ~BaseEvent()
Definition: mlEventSource.h:80
BaseEvent is the base class for all events emitted from EventSourceBase.
Definition: mlEventSource.h:76
#define ML_CLASS_HEADER(className)
Same like ML_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_ABSTRACT_CLASS_HEADER(className)
Same like ML_ABSTRACT_CLASS_HEADER_EXPORTED with a non existing export symbol.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:54
EventSourceRemovedEvent is used to indicate when the EventSourceBase object is removed.