Samchon Framework for CPP  1.0.0
samchon::library::EventDispatcher Class Reference

Abstract class for dispatching Event. More...

#include <EventDispatcher.hpp>

Collaboration diagram for samchon::library::EventDispatcher:

Public Member Functions

 EventDispatcher ()
 Default Constructor. More...
 
 EventDispatcher (const EventDispatcher &)
 Copy Constructor. More...
 
 EventDispatcher (EventDispatcher &&obj)
 Move Constructor. More...
 
virtual ~EventDispatcher ()
 Default Destructor. More...
 
void addEventListener (int type, Listener listener, void *addiction=nullptr)
 Register an event listener. More...
 
void removeEventListener (int type, Listener listener, void *addiction=nullptr)
 Remove a registered event listener. More...
 
void dispatch (std::shared_ptr< Event > event)
 Dispatches an event to all listeners. More...
 

Static Public Member Functions

static size_t & THREAD_SIZE ()
 Numer of threads for background. More...
 

Private Attributes

std::unordered_map< int, std::unordered_map< Listener, std::unordered_set< void * > > > listeners
 A container storing listeners. More...
 
RWMutex mtx
 A rw_mutex for concurrency. More...
 

Detailed Description

Abstract class for dispatching Event.

EventDispatcher is the base class for all classes that dispatch events.

All the events are sent asynchronously. To avoid from creating tooo enourmouse threads dispatching events, all event sending processes will acuiqre a semaphore. The default permitted size of the semaphore is 2.

  • Number of thread pools used to sending events is 2.
library_event.png
Example Source
Deprecated:

EventDispatcher is a candidate to be deprecated.

Since C++11, calling member method of a class by a new thread passing by static method and using void pointer are recommeded to avoid. As the reason, using std::thread and std::bind will be better.

See also
samchon::library
Author
Jeongho Nam http://samchon.org, Jun Ryung Ju https://github.com/ArtBlnd

Definition at line 50 of file EventDispatcher.hpp.

Constructor & Destructor Documentation

samchon::library::EventDispatcher::EventDispatcher ( )
inline

Default Constructor.

Definition at line 74 of file EventDispatcher.hpp.

samchon::library::EventDispatcher::EventDispatcher ( const EventDispatcher )
inline

Copy Constructor.

Copying an EventDispatcher instance does not copy the event listeners attached to it. (If your newly created node needs an event listener, you must attach the listener after creating the node.)

Parameters
eventDispatcherThe object to copy

Definition at line 87 of file EventDispatcher.hpp.

samchon::library::EventDispatcher::EventDispatcher ( EventDispatcher &&  obj)
inline

Move Constructor.

Parameters
eventDispatcherThe object to move

Definition at line 97 of file EventDispatcher.hpp.

References samchon::library::UniqueWriteLock::unlock().

Here is the call graph for this function:

virtual samchon::library::EventDispatcher::~EventDispatcher ( )
inlinevirtual

Default Destructor.

Definition at line 120 of file EventDispatcher.hpp.

Member Function Documentation

void samchon::library::EventDispatcher::addEventListener ( int  type,
Listener  listener,
void *  addiction = nullptr 
)
inline

Register an event listener.

Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.

Warning
Copying an EventDispatcher instance does not copy the event listeners attached to it. (If your newly created node needs an event listener, you must attach the listener after creating the node.) However, if you move an EventDispatcher instance, the event listeners attached to it move along with it.
If you no longer need an event listener, remove it by calling removeEventListener, or EventDispatcher already try to send events to the no longer needed listener and it can cause some confliction.
Parameters
typeThe type of event.
listenerThe listener function processes the event.
addictionSomething to be addicted following the listener.

Definition at line 154 of file EventDispatcher.hpp.

void samchon::library::EventDispatcher::removeEventListener ( int  type,
Listener  listener,
void *  addiction = nullptr 
)
inline

Remove a registered event listener.

Removes a listener from the EventDispatcher object. If there is no matching listener registered with the EventDispatcher object, a call to this method has no effect

Parameters
typeThe type of event.
listenerThe listener function to remove.
addictionSomethhing to be addicted following the listener.

Definition at line 172 of file EventDispatcher.hpp.

void samchon::library::EventDispatcher::dispatch ( std::shared_ptr< Event event)
inline

Dispatches an event to all listeners.

Dispatches an event into the event flow in the background. The Event::source is the EventDispatcher object upon which the dispatchEvent.

Parameters
eventThe Event object that is dispatched into the event flow.
Returns
Whether there's some listener to listen the event

Definition at line 205 of file EventDispatcher.hpp.

References THREAD_SIZE(), and samchon::library::UniqueReadLock::unlock().

Here is the call graph for this function:

static size_t& samchon::library::EventDispatcher::THREAD_SIZE ( )
inlinestatic

Numer of threads for background.

Definition at line 317 of file EventDispatcher.hpp.

Referenced by dispatch().

Here is the caller graph for this function:

Member Data Documentation

std::unordered_map<int, std::unordered_map<Listener, std::unordered_set<void*> > > samchon::library::EventDispatcher::listeners
private

A container storing listeners.

Definition at line 60 of file EventDispatcher.hpp.

RWMutex samchon::library::EventDispatcher::mtx
private

A rw_mutex for concurrency.

Definition at line 65 of file EventDispatcher.hpp.


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