20 #ifndef MIRTK_EventDelegate_H 21 #define MIRTK_EventDelegate_H 23 #include "mirtk/Observer.h" 25 #include "mirtk/FastDelegate.h" 26 #include "mirtk/UnorderedMap.h" 60 typedef FastDelegate0<> Delegate0;
61 typedef FastDelegate1<Event> Delegate1;
62 typedef FastDelegate2<Event, const void *> Delegate2;
63 typedef FastDelegate3<Observable *, Event, const void *> Delegate3;
125 DelegateMemento _Memento;
126 Callback() : _N(0), _Memento() {}
127 Callback(DelegateMemento memento,
int n) : _N(n), _Memento(memento) {}
128 Callback(
const Callback &o) : _N(o._N), _Memento(o._Memento) {}
131 void Forward(Callback &func,
Observable *obj,
Event event,
const void *data);
133 UnorderedMap<Event, Callback> _SingleEventDelegate;
134 Callback _AnyEventDelegate;
154 _AnyEventDelegate = Callback(delegate.GetMemento(), 0);
160 _AnyEventDelegate = Callback(delegate.GetMemento(), 1);
166 _AnyEventDelegate = Callback(delegate.GetMemento(), 2);
172 _AnyEventDelegate = Callback(delegate.GetMemento(), 3);
178 _SingleEventDelegate[event] = Callback(delegate.GetMemento(), 0);
184 _SingleEventDelegate[event] = Callback(delegate.GetMemento(), 1);
190 _SingleEventDelegate[event] = Callback(delegate.GetMemento(), 2);
196 _SingleEventDelegate[event] = Callback(delegate.GetMemento(), 3);
200 inline void EventDelegate::Forward(Callback &func,
Observable *obj,
Event event,
const void *data)
205 delegate.SetMemento(func._Memento);
206 if (delegate) delegate();
211 delegate.SetMemento(func._Memento);
212 if (delegate) delegate(event);
217 delegate.SetMemento(func._Memento);
218 if (delegate) delegate(event, data);
223 delegate.SetMemento(func._Memento);
224 if (delegate) delegate(obj, event, data);
233 Forward(_AnyEventDelegate, obj, event, data);
234 UnorderedMap<Event, Callback>::iterator it = _SingleEventDelegate.find(event);
235 if (it != _SingleEventDelegate.end()) Forward(it->second, obj, event, data);
241 #endif // MIRTK_EventDelegate_H void HandleEvent(Observable *, Event, const void *=NULL)
Forward event notification to registered callback (member) function.
EventDelegate()
Default constructor.
Event
Events that can be observed.
void Bind(Delegate0)
Bind callback (member) function for any event with no arguments.
~EventDelegate()
Destructor.