20 #ifndef MIRTK_Observable_H 21 #define MIRTK_Observable_H 23 #include "mirtk/Object.h" 25 #include "mirtk/Observer.h" 26 #include "mirtk/OrderedSet.h" 27 #include "mirtk/Event.h" 48 typedef OrderedSet<Observer *> ObserverSet;
51 typedef ObserverSet::iterator ObserverIterator;
54 mirtkPublicAttributeMacro(
bool, Changed);
57 mirtkAttributeMacro(ObserverSet, Observers);
103 #define MIRTK_LOG_EVENT(msg) \ 107 Broadcast(mirtk::LogEvent, ss.str().c_str()); \ 126 inline void Observable::CopyAttributes(
const Observable &other)
128 _Changed = other._Changed;
137 CopyAttributes(other);
143 if (
this != &other) {
144 Object::operator =(other);
145 CopyAttributes(other);
154 for (ObserverIterator it = _Observers.begin(); it != _Observers.end(); ++it) {
155 (*it)->StopObserving(
this);
173 return static_cast<int>(_Observers.size());
179 _Observers.insert(&observer);
180 observer.StartObserving(
this);
186 observer.StopObserving(
this);
187 _Observers.erase(&observer);
197 for (ObserverIterator it = _Observers.begin(); it != _Observers.end(); ++it) {
198 (*it)->HandleEvent(
this, event, data);
214 #endif // MIRTK_Observable_H void DeleteObserver(Observer &)
Delete observer.
void Broadcast(Event, const void *=NULL)
Broadcast event to observers.
void AddObserver(Observer &)
Add observer.
void NotifyObservers(Event, const void *=NULL)
Notify all observers about given event if this object has changed.
int NumberOfObservers() const
Number of current observers.
void ClearObservers()
Delete all observers.
Observable()
Default constructor.
Event
Events that can be observed.
virtual ~Observable()
Destructor.
Observable & operator=(const Observable &)
Assignment operator.