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 &&)
 Move Constructor. More...
 
virtual ~EventDispatcher ()
 Default Destructor. More...
 
void addEventListener (int, Listener, void *=nullptr)
 Register an event listener. More...
 
void removeEventListener (int, Listener, void *=nullptr)
 Remove a registered event listener. More...
 
void dispatch (std::shared_ptr< Event >)
 Dispatches an event to all listeners. More...
 

Static Public Attributes

static size_t THREAD_SIZE = 2
 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
1 #include <iostream>
2 #include <thread>
3 #include <chrono>
4 
5 #include <samchon/library/EventDispatcher.hpp>
6 #include <samchon/library/Event.hpp>
7 #include <samchon/library/ProgressEvent.hpp>
8 
9 #ifdef _WIN64
10 # ifdef _DEBUG
11 # pragma comment(lib, "x64/Debug/SamchonFramework.lib")
12 # else
13 # pragma comment(lib, "x64/Release/SamchonFramework.lib")
14 # endif
15 #else
16 # ifdef _DEBUG
17 # pragma comment(lib, "Debug/SamchonFramework.lib")
18 # else
19 # pragma comment(lib, "Release/SamchonFramework.lib")
20 # endif
21 #endif
22 
23 using namespace std;
24 using namespace samchon::library;
25 
26 class Process
27  : public EventDispatcher
28 {
29 public:
30  Process()
31  {
32 
33  };
34 
35  void run()
36  {
37  dispatchEvent(shared_ptr<Event>(new Event(this, Event::ACTIVATE)));
38 
39  for (size_t i = 0; i < 100; i++)
40  {
41  this_thread::sleep_for(chrono::seconds(1));
42  dispatchEvent(shared_ptr<Event>(new ProgressEvent(this, i + 1, 100)));
43  }
44 
45  dispatchEvent(shared_ptr<Event>(new Event(this, Event::COMPLETE)));
46  };
47 };
48 
49 void handleActivate(shared_ptr<Event> event)
50 {
51  cout << "Activated" << endl;
52 }
53 void handleComplete(shared_ptr<Event> event)
54 {
55  cout << "Completed" << endl;
56 }
57 void handleProgress(shared_ptr<Event> event)
58 {
59  cout << dynamic_pointer_cast<ProgressEvent>(event)->getPercent() * 100.0 << "%" << endl;
60 }
61 
62 void main()
63 {
64  Process process;
65 
66  process.addEventListener(Event::ACTIVATE, handleActivate);
67  process.addEventListener(Event::COMPLETE, handleComplete);
68  process.addEventListener(ProgressEvent::PROGRESS, handleProgress);
69 
70  process.run();
71  system("pause");
72 }
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
Represent an event running on background.
Definition: Event.hpp:42
Event representing a progress.
Abstract class for dispatching Event.
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 Ryoung Ju

Definition at line 51 of file EventDispatcher.hpp.

Constructor & Destructor Documentation

EventDispatcher::EventDispatcher ( )

Default Constructor.

Definition at line 12 of file EventDispatcher.cpp.

EventDispatcher::EventDispatcher ( const EventDispatcher obj)

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 15 of file EventDispatcher.cpp.

EventDispatcher::EventDispatcher ( EventDispatcher &&  obj)

Move Constructor.

Parameters
eventDispatcherThe object to move

Definition at line 19 of file EventDispatcher.cpp.

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

Here is the call graph for this function:

EventDispatcher::~EventDispatcher ( )
virtual

Default Destructor.

Definition at line 39 of file EventDispatcher.cpp.

Member Function Documentation

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

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 54 of file EventDispatcher.cpp.

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

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 60 of file EventDispatcher.cpp.

void EventDispatcher::dispatch ( std::shared_ptr< Event event)

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 83 of file EventDispatcher.cpp.

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

Here is the call 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 61 of file EventDispatcher.hpp.

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

A rw_mutex for concurrency.

Definition at line 66 of file EventDispatcher.hpp.

size_t EventDispatcher::THREAD_SIZE = 2
static

Numer of threads for background.

Definition at line 168 of file EventDispatcher.hpp.


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