-
Notifications
You must be signed in to change notification settings - Fork 60
/
EventEngine.cpp
92 lines (77 loc) · 1.73 KB
/
EventEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "EventEngine.h"
#include <thread>
void EventEngine::run()
{
while (__active_thread.load())
{
// 获取任务
Event evt;
if (__queue.wait_and_pop(evt, -1))
{
process(evt);
}
}
}
void EventEngine::process(Event event)
{
std::lock_guard<std::mutex> lock(handlers_mutex);
if (__handlers.find(event.type_) != __handlers.end())
{
std::list<std::shared_ptr<CallBack>>& handlerList = __handlers[event.type_];
std::for_each(handlerList.cbegin(), handlerList.cend(), [&event](const std::shared_ptr<CallBack>& func_ptr)
{
func_ptr->Execute(event);
});
}
}
void EventEngine::onTimer()
{
putEvent(Event(EVENT_TIMER));
}
void EventEngine::ee_begin()
{
// 启动定时器线程
__timer = new QTimer(this);
__timer->setSingleShot(false);
connect(__timer, SIGNAL(timeout()), this, SLOT(onTimer()), Qt::DirectConnection);
__timer->start(1000);
// 打开队列开关
__active_put.store(true);
// 打开事件引擎线程
__active_thread.store(true);
start();
}
void EventEngine::ee_stop()
{
// 关闭定时器线程
__timer->stop();
// 关闭队列开关
__active_put.store(false);
while (!__queue.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// 关闭事件引擎线程
__active_thread.store(false);
__queue.notify_all(); // 通知等待在空队列上的线程
this->quit();
this->wait();
}
void EventEngine::removeEvent(std::string type)
{
// 因为无法区分特定 obj + obj_ordinary_function, 故Event关系按type整体移除
std::lock_guard<std::mutex> lock(handlers_mutex);
// 判断event_type是否存在,存在则删除事件关联
auto handle_list = __handlers.find(type);
if (handle_list != __handlers.end())
{
__handlers.erase(handle_list);
}
}
void EventEngine::putEvent(Event event)
{
if (__active_put.load())
{
__queue.push_back(event);
}
}