Samchon Framework for CPP  1.0.0
event_main.cpp
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  : EventDispatcher()
32  {
33  };
34 
35  void run()
36  {
37  new Event(this, Event::ACTIVATE);
38  dispatch(shared_ptr<Event>(new Event(this, Event::ACTIVATE)));
39 
40  for (size_t i = 0; i < 100; i++)
41  {
42  this_thread::sleep_for(chrono::seconds(1));
43  dispatch(shared_ptr<Event>(new ProgressEvent(this, i + 1, 100)));
44  }
45 
46  dispatch(shared_ptr<Event>(new Event(this, Event::COMPLETE)));
47  };
48 };
49 
50 void handleActivate(shared_ptr<Event> event, void *addiction)
51 {
52  cout << "Activated" << endl;
53 }
54 void handleComplete(shared_ptr<Event> event, void *addiction)
55 {
56  cout << "Completed" << endl;
57 }
58 void handleProgress(shared_ptr<Event> event, void *addiction)
59 {
60  cout << dynamic_pointer_cast<ProgressEvent>(event)->getPercent() * 100.0 << "%" << endl;
61 }
62 
63 void main()
64 {
65  Process process;
66 
67  process.addEventListener(Event::ACTIVATE, handleActivate);
68  process.addEventListener(Event::COMPLETE, handleComplete);
69  process.addEventListener(ProgressEvent::PROGRESS, handleProgress);
70 
71  process.run();
72  system("pause");
73 }
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.