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

Represent an event running on background. More...

#include <Event.hpp>

Collaboration diagram for samchon::library::Event:

Public Member Functions

 Event (EventDispatcher *, int)
 Construct from source and type. More...
 
auto getSource () const -> EventDispatcher *
 Get source of the Event. More...
 
auto getType () const -> int
 Get type of the Event. More...
 

Protected Attributes

EventDispatchersource
 Source of the event. More...
 
int type
 Type of the event. More...
 

Detailed Description

Represent an event running on background.

The Event class is used as the base class for the creation of Event objects, which are passed as parameters to event listeners when an event occurs.

The properties of the Event class carry basic information about an event, such as the event's type or source (who made the event) of the event.

For many events, such as the events represented by the Event class constants, this basic information is sufficient. Other events, however, may require more detailed information.

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
Event(EventDispatcher *, int)
Construct from source and type.
Definition: Event.cpp:5
Event representing a progress.
Abstract class for dispatching Event.
Deprecated:

Event 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

Definition at line 42 of file Event.hpp.

Constructor & Destructor Documentation

Event::Event ( EventDispatcher source,
int  type 
)

Construct from source and type.

The event object owns its source and type

Parameters
sourceSource of the event; who made the event
typeType of the event

Definition at line 5 of file Event.cpp.

References source, and type.

Member Function Documentation

auto Event::getSource ( ) const -> EventDispatcher*

Get source of the Event.

Definition at line 11 of file Event.cpp.

References source.

auto Event::getType ( ) const -> int

Get type of the Event.

Definition at line 15 of file Event.cpp.

References type.

Member Data Documentation

EventDispatcher* samchon::library::Event::source
protected

Source of the event.

Definition at line 56 of file Event.hpp.

Referenced by Event(), and getSource().

int samchon::library::Event::type
protected

Type of the event.

Definition at line 61 of file Event.hpp.

Referenced by Event(), and getType().


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