#include <UEventsManager.h>
Classes | |
class | Pipe |
Static Public Member Functions | |
static void | addHandler (UEventsHandler *handler) |
static void | createPipe (const UEventsSender *sender, const UEventsHandler *receiver, const std::string &eventName) |
static void | post (UEvent *event, bool async=true, const UEventsSender *sender=0) |
static void | removeAllPipes (const UEventsSender *sender) |
static void | removeHandler (UEventsHandler *handler) |
static void | removeNullPipes (const UEventsSender *sender) |
static void | removePipe (const UEventsSender *sender, const UEventsHandler *receiver, const std::string &eventName) |
Protected Member Functions | |
virtual void | mainLoop () |
UEventsManager () | |
virtual | ~UEventsManager () |
Static Protected Member Functions | |
static UEventsManager * | getInstance () |
Private Member Functions | |
void | _addHandler (UEventsHandler *handler) |
void | _createPipe (const UEventsSender *sender, const UEventsHandler *receiver, const std::string &eventName) |
void | _postEvent (UEvent *event, bool async=true, const UEventsSender *sender=0) |
void | _removeAllPipes (const UEventsSender *sender) |
void | _removeHandler (UEventsHandler *handler) |
void | _removeNullPipes (const UEventsSender *sender) |
void | _removePipe (const UEventsSender *sender, const UEventsHandler *receiver, const std::string &eventName) |
virtual bool | dispatchEvent (UEvent *event, const UEventsSender *sender) |
virtual void | dispatchEvents () |
std::list< UEventsHandler * > | getPipes (const UEventsSender *sender, const std::string &eventName) |
virtual void | mainLoopKill () |
Private Attributes | |
std::list< std::pair< UEvent *, const UEventsSender * > > | events_ |
UMutex | eventsMutex_ |
std::list< UEventsHandler * > | handlers_ |
UMutex | handlersMutex_ |
std::list< Pipe > | pipes_ |
UMutex | pipesMutex_ |
USemaphore | postEventSem_ |
Static Private Attributes | |
static UDestroyer< UEventsManager > | destroyer_ |
static UEventsManager * | instance_ = 0 |
Friends | |
class | UDestroyer< UEventsManager > |
This class is used to post events between threads in the application. It is Thread-Safe and the events are sent to receivers in the same order they are posted (FIFO). It works like the design pattern Mediator. It is also a Singleton, so it can be used anywhere in the application.
To send an event, use UEventsManager::post(). Events are automatically deleted after they are posted.
The EventsManager have a list of handlers to which it sends posted events. To add an handler, use UEventsManager::addHandler(). To remove, use UEventsManager::removeHandler().
// Anywhere in the code: UEventsManager::post(new MyEvent()); // where MyEvent is an implemented UEvent
Definition at line 78 of file UEventsManager.h.
UEventsManager::UEventsManager | ( | ) | [protected] |
Definition at line 136 of file UEventsManager.cpp.
UEventsManager::~UEventsManager | ( | ) | [protected, virtual] |
Definition at line 140 of file UEventsManager.cpp.
void UEventsManager::_addHandler | ( | UEventsHandler * | handler | ) | [private] |
Definition at line 245 of file UEventsManager.cpp.
void UEventsManager::_createPipe | ( | const UEventsSender * | sender, |
const UEventsHandler * | receiver, | ||
const std::string & | eventName | ||
) | [private] |
Definition at line 367 of file UEventsManager.cpp.
void UEventsManager::_postEvent | ( | UEvent * | event, |
bool | async = true , |
||
const UEventsSender * | sender = 0 |
||
) | [private] |
Definition at line 300 of file UEventsManager.cpp.
void UEventsManager::_removeAllPipes | ( | const UEventsSender * | sender | ) | [private] |
Definition at line 444 of file UEventsManager.cpp.
void UEventsManager::_removeHandler | ( | UEventsHandler * | handler | ) | [private] |
Definition at line 269 of file UEventsManager.cpp.
void UEventsManager::_removeNullPipes | ( | const UEventsSender * | sender | ) | [private] |
Definition at line 461 of file UEventsManager.cpp.
void UEventsManager::_removePipe | ( | const UEventsSender * | sender, |
const UEventsHandler * | receiver, | ||
const std::string & | eventName | ||
) | [private] |
Definition at line 414 of file UEventsManager.cpp.
void UEventsManager::addHandler | ( | UEventsHandler * | handler | ) | [static] |
This method is used to add an events handler to the list of handlers.
handler | the handler to be added. |
Definition at line 28 of file UEventsManager.cpp.
void UEventsManager::createPipe | ( | const UEventsSender * | sender, |
const UEventsHandler * | receiver, | ||
const std::string & | eventName | ||
) | [static] |
Definition at line 67 of file UEventsManager.cpp.
bool UEventsManager::dispatchEvent | ( | UEvent * | event, |
const UEventsSender * | sender | ||
) | [private, virtual] |
Definition at line 201 of file UEventsManager.cpp.
void UEventsManager::dispatchEvents | ( | ) | [private, virtual] |
Definition at line 170 of file UEventsManager.cpp.
UEventsManager * UEventsManager::getInstance | ( | ) | [static, protected] |
Definition at line 125 of file UEventsManager.cpp.
std::list< UEventsHandler * > UEventsManager::getPipes | ( | const UEventsSender * | sender, |
const std::string & | eventName | ||
) | [private] |
Definition at line 329 of file UEventsManager.cpp.
void UEventsManager::mainLoop | ( | ) | [protected, virtual] |
The UEventsManager's main loop.
Implements UThread.
Definition at line 156 of file UEventsManager.cpp.
void UEventsManager::mainLoopKill | ( | ) | [private, virtual] |
Reimplemented to wake up UEventsManager on termination.
Reimplemented from UThread.
Definition at line 165 of file UEventsManager.cpp.
void UEventsManager::post | ( | UEvent * | event, |
bool | async = true , |
||
const UEventsSender * | sender = 0 |
||
) | [static] |
This method is used to post an event to handlers.
Event can be posted asynchronously or not. In the first case, the event is dispatched by the UEventsManager's thread. In the second case, the event is handled immediately by event's receivers, thus in the sender thread.
event | the event to be posted. |
async | if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous). |
Definition at line 54 of file UEventsManager.cpp.
void UEventsManager::removeAllPipes | ( | const UEventsSender * | sender | ) | [static] |
Definition at line 99 of file UEventsManager.cpp.
void UEventsManager::removeHandler | ( | UEventsHandler * | handler | ) | [static] |
This method is used to remove an events handler from the list of handlers.
handler | the handler to be removed. |
Definition at line 41 of file UEventsManager.cpp.
void UEventsManager::removeNullPipes | ( | const UEventsSender * | sender | ) | [static] |
Definition at line 112 of file UEventsManager.cpp.
void UEventsManager::removePipe | ( | const UEventsSender * | sender, |
const UEventsHandler * | receiver, | ||
const std::string & | eventName | ||
) | [static] |
Definition at line 83 of file UEventsManager.cpp.
friend class UDestroyer< UEventsManager > [friend] |
Definition at line 155 of file UEventsManager.h.
UDestroyer< UEventsManager > UEventsManager::destroyer_ [static, private] |
Definition at line 243 of file UEventsManager.h.
std::list<std::pair<UEvent*, const UEventsSender * > > UEventsManager::events_ [private] |
Definition at line 244 of file UEventsManager.h.
UMutex UEventsManager::eventsMutex_ [private] |
Definition at line 246 of file UEventsManager.h.
std::list<UEventsHandler*> UEventsManager::handlers_ [private] |
Definition at line 245 of file UEventsManager.h.
UMutex UEventsManager::handlersMutex_ [private] |
Definition at line 247 of file UEventsManager.h.
UEventsManager * UEventsManager::instance_ = 0 [static, private] |
Definition at line 242 of file UEventsManager.h.
std::list<Pipe> UEventsManager::pipes_ [private] |
Definition at line 249 of file UEventsManager.h.
UMutex UEventsManager::pipesMutex_ [private] |
Definition at line 250 of file UEventsManager.h.
USemaphore UEventsManager::postEventSem_ [private] |
Definition at line 248 of file UEventsManager.h.