Samchon Framework for CPP  1.0.0
samchon::example::event Namespace Reference

An example using event module. More...

Detailed Description

An example using event module.

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.
See also
library::Event
library::StaticEventDispatcher
Author
Jeongho Nam http://samchon.org