Public Member Functions | Protected Attributes | Private Member Functions
karto::AbstractEvent< TArgs > Class Template Reference

#include <Event.h>

Inheritance diagram for karto::AbstractEvent< TArgs >:
Inheritance graph
[legend]

List of all members.

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)
AbstractEventoperator= (const AbstractEvent &rOther)

Detailed Description

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.

     #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);
     }

Definition at line 806 of file Event.h.


Constructor & Destructor Documentation

template<class TArgs>
karto::AbstractEvent< TArgs >::AbstractEvent ( ) [inline]

Default constructor

Definition at line 812 of file Event.h.

template<class TArgs>
karto::AbstractEvent< TArgs >::AbstractEvent ( const DefaultStrategy< TArgs > &  rStrategy) [inline]

Constructs an event with a default strategy

Parameters:
rStrategy

Definition at line 821 of file Event.h.

template<class TArgs>
virtual karto::AbstractEvent< TArgs >::~AbstractEvent ( ) [inline, virtual]

Destructor

Definition at line 830 of file Event.h.

template<class TArgs>
karto::AbstractEvent< TArgs >::AbstractEvent ( const AbstractEvent< TArgs > &  rOther) [private]

Member Function Documentation

template<class TArgs>
void karto::AbstractEvent< TArgs >::Clear ( ) [inline]

Removes all delegates.

Definition at line 935 of file Event.h.

template<class TArgs>
void karto::AbstractEvent< TArgs >::Disable ( ) [inline]

Disables the event. Notify will be ignored, but adding/removing delegates is still allowed.

Definition at line 917 of file Event.h.

template<class TArgs>
void karto::AbstractEvent< TArgs >::Enable ( ) [inline]

Enables the event.

Definition at line 907 of file Event.h.

template<class TArgs>
kt_bool karto::AbstractEvent< TArgs >::IsEmpty ( ) const [inline]

Checks if any delegates are registered at the delegate.

Definition at line 944 of file Event.h.

template<class TArgs>
kt_bool karto::AbstractEvent< TArgs >::IsEnabled ( ) const [inline]

Checks if we are enabled

Definition at line 926 of file Event.h.

template<class TArgs>
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.

Parameters:
pSender
rArgs

Definition at line 879 of file Event.h.

template<class TArgs>
void karto::AbstractEvent< TArgs >::operator() ( const void *  pSender,
TArgs &  args 
) [inline]

Same as Notify

Definition at line 864 of file Event.h.

template<class TArgs>
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.

Parameters:
rDelegate

Definition at line 843 of file Event.h.

template<class TArgs>
void karto::AbstractEvent< TArgs >::operator-= ( const AbstractDelegate< TArgs > &  rDelegate) [inline]

Removes a delegate from the event. If the delegate is equal to an already existing one is determined by the < operator. If the observer is not found, the unregister will be ignored

Parameters:
rDelegate

Definition at line 855 of file Event.h.

template<class TArgs>
AbstractEvent& karto::AbstractEvent< TArgs >::operator= ( const AbstractEvent< TArgs > &  rOther) [private]

Member Data Documentation

template<class TArgs>
kt_bool karto::AbstractEvent< TArgs >::m_Enabled [protected]

Enabled flag

Definition at line 954 of file Event.h.

template<class TArgs>
Mutex karto::AbstractEvent< TArgs >::m_Mutex [mutable, protected]

Mutex

Definition at line 964 of file Event.h.

template<class TArgs>
DefaultStrategy<TArgs> karto::AbstractEvent< TArgs >::m_Strategy [protected]

Strategy

Definition at line 959 of file Event.h.


The documentation for this class was generated from the following file:


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Sun Apr 2 2017 03:53:09