#include <Event.h>
Public Member Functions | |
AbstractEvent () | |
AbstractEvent (const DefaultStrategy< TArgs > &rStrategy) | |
void | Clear () |
void | Disable () |
void | Enable () |
kt_bool | IsEmpty () const |
kt_bool | IsEnabled () const |
void | Notify (const void *pSender, TArgs &rArgs) |
void | operator() (const void *pSender, TArgs &args) |
void | operator+= (const AbstractDelegate< TArgs > &rDelegate) |
void | operator-= (const AbstractDelegate< TArgs > &rDelegate) |
virtual | ~AbstractEvent () |
Protected Attributes | |
kt_bool | m_Enabled |
Mutex | m_Mutex |
DefaultStrategy< TArgs > | m_Strategy |
Private Member Functions | |
AbstractEvent (const AbstractEvent &rOther) | |
AbstractEvent & | operator= (const AbstractEvent &rOther) |
An AbstractEvent is the super-class of all events. It works similar to the way C# handles notifications (aka events in C#). Events can be used to send information to a set of observers which are registered at the event. The type of the data is specified with the template parameter TArgs.
#include <Karto/Event.h>
Use events by adding them as public members to the object which is throwing notifications:
class MyData { public: karto::BasicEvent<int> AgeChanged; MyData(); ... };
Throwing the event can be done either by the events Notify() method:
Alternatively, instead of Notify(), operator() can be used.
void MyData::setAge(int i) { this->_age = i; AgeChanged(this, this->_age); }
Note that Notify() do not catch exceptions, i.e. in case a delegate throws an exception, the notify is immediately aborted and the exception is thrown back to the caller.
Delegates can register methods at the event. In the case of a BasicEvent the Delegate template is used.
Events require the observers to follow one of the following method signature:
void OnEvent(const void* pSender, TArgs& args); void OnEvent(TArgs& args); static void OnEvent(const void* pSender, TArgs& args); static void OnEvent(void* pSender, TArgs& args); static void OnEvent(TArgs& args);
For performance reasons arguments are always sent by reference. This also allows observers to modify the sent argument. To prevent that, use <const TArg> as template parameter. A non-conformant method signature leads to compile errors.
Assuming that the observer meets the method signature requirement, it can register this method with the += operator:
class MyController { protected: MyData _data; void onDataChanged(void* pSender, int& data); ... }; MyController::MyController() { _data.AgeChanged += karto::delegate(this, &MyController::onDataChanged); }
Unregistering happens via the -= operator. Forgetting to unregister a method will lead to segmentation faults later, when one tries to send a notify to a no longer existing object.
MyController::~MyController() { _data.DataChanged -= karto::delegate(this, &MyController::onDataChanged); }
karto::AbstractEvent< TArgs >::AbstractEvent | ( | ) | [inline] |
karto::AbstractEvent< TArgs >::AbstractEvent | ( | const DefaultStrategy< TArgs > & | rStrategy | ) | [inline] |
virtual karto::AbstractEvent< TArgs >::~AbstractEvent | ( | ) | [inline, virtual] |
karto::AbstractEvent< TArgs >::AbstractEvent | ( | const AbstractEvent< TArgs > & | rOther | ) | [private] |
void karto::AbstractEvent< TArgs >::Clear | ( | ) | [inline] |
void karto::AbstractEvent< TArgs >::Disable | ( | ) | [inline] |
void karto::AbstractEvent< TArgs >::Enable | ( | ) | [inline] |
kt_bool karto::AbstractEvent< TArgs >::IsEmpty | ( | ) | const [inline] |
kt_bool karto::AbstractEvent< TArgs >::IsEnabled | ( | ) | const [inline] |
void karto::AbstractEvent< TArgs >::Notify | ( | const void * | pSender, |
TArgs & | rArgs | ||
) | [inline] |
Sends a notification to all registered delegates. The order is determined by the TStrategy. This method is blocking. While executing, other objects can change the list of delegates. These changes don't influence the current active notifications but are activated with the next notify. If one of the delegates throws an exception, the notify method is immediately aborted and the exception is reported to the caller.
pSender | |
rArgs |
void karto::AbstractEvent< TArgs >::operator() | ( | const void * | pSender, |
TArgs & | args | ||
) | [inline] |
void karto::AbstractEvent< TArgs >::operator+= | ( | const AbstractDelegate< TArgs > & | rDelegate | ) | [inline] |
Adds a delegate to the event. If the observer is equal to an already existing one (determined by the < operator), it will simply replace the existing observer. This behavior is determined by the TStrategy. Current implementations (DefaultStrategy) follow that guideline but future ones can deviate.
rDelegate |
void karto::AbstractEvent< TArgs >::operator-= | ( | const AbstractDelegate< TArgs > & | rDelegate | ) | [inline] |
AbstractEvent& karto::AbstractEvent< TArgs >::operator= | ( | const AbstractEvent< TArgs > & | rOther | ) | [private] |
kt_bool karto::AbstractEvent< TArgs >::m_Enabled [protected] |
Mutex karto::AbstractEvent< TArgs >::m_Mutex [mutable, protected] |
DefaultStrategy<TArgs> karto::AbstractEvent< TArgs >::m_Strategy [protected] |