template<class TArgs>
class karto::AbstractEvent< TArgs >
////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////
/** 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.
Use events by adding them as public members to the object which is throwing notifications:
class MyData
{
public:
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()
{
}
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()
{
}
Definition at line 806 of file Event.h.