Logo Search packages:      
Sourcecode: poco version File versions  Download package

Poco::AbstractEvent< TArgs, TStrategy, TDelegate > Class Template Reference

#include <AbstractEvent.h>

Inheritance diagram for Poco::AbstractEvent< TArgs, TStrategy, TDelegate >:

Poco::FIFOEvent< KeySet > Poco::BasicEvent< TArgs > Poco::FIFOEvent< TArgs > Poco::PriorityEvent< TArgs >

List of all members.


Detailed Description

template<class TArgs, class TStrategy, class TDelegate>
class Poco::AbstractEvent< TArgs, TStrategy, TDelegate >

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. The TStrategy parameter must be a subclass of NotificationStrategy. The parameter TDelegate can either be a subclass of AbstractDelegate or of PriorityAbstractDelegate.

Note that AbstractEvent should never be used directly. One ought to use one of its subclasses which set the TStrategy and TDelegate template parameters to fixed values. For most use-cases the BasicEvent template will be sufficient.

Use events by adding them as public members to the object which is throwing notifications:

class MyData { public: Poco::BasicEvent<int> AgeChanged;

MyData(); ... };

Throwing the event can be done either by the events notify() or notifyAsync() method:

Alternatively, instead of notify(), operator () can be used.

void MyData::setAge(int i) { this->_age = i; AgeChanged(this, this->_age); }

Note that notify and notifyAsync 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 or FIFOEvent the Delegate template is used, in case of an PriorityEvent a PriorityDelegate is used. Mixing of observers, e.g. using a PriorityDelegate with a BasicEvent is not possible and checked for during compile time. Events require the observers to follow the following method signature:

void onEvent(const void* pSender, 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 += Delegate<MyController, int>(this, &MyController::onDataChanged); }

In some cases it might be desirable to work with automatically expiring registrations:

_data.DataChanged += Expire<int>(Delegate<MyController, int>(this, &MyController::onDataChanged), 1000);

This will add a delegate to the event which will automatically be removed in 1000 millisecs.

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 -= Delegate<MyController, int>(this, &MyController::onDataChanged); }

For further examples refer to the event testsuites.

Definition at line 55 of file AbstractEvent.h.


Public Member Functions

 AbstractEvent (const TStrategy &strat)
void clear ()
 Removes all delegates.
void disable ()
void enable ()
 Enables the event.
bool isEnabled () const
void notify (const void *pSender, TArgs &args)
ActiveResult< TArgs > notifyAsync (const void *pSender, const TArgs &args)
void operator() (const void *pSender, TArgs &args)
void operator+= (const TDelegate &aDelegate)
void operator-= (const TDelegate &aDelegate)

Protected Member Functions

TArgs executeAsyncImpl (const NotifyAsyncParams &par)

Protected Attributes

bool _enabled
 The strategy used to notify observers.
ActiveMethod< TArgs,
NotifyAsyncParams,
AbstractEvent
_executeAsync
FastMutex _mutex
TStrategy _strategy

Private Member Functions

 AbstractEvent (const AbstractEvent &other)
AbstractEventoperator= (const AbstractEvent &other)

Classes

struct  NotifyAsyncParams

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

Generated by  Doxygen 1.6.0   Back to index