forked from jjzhang166/QEvent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QJEventLoop.h
116 lines (100 loc) · 2.29 KB
/
QJEventLoop.h
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include <memory>
#include <list>
#include <thread>
#include "QJMutex.h"
namespace qijia
{
struct IDataBase
{
virtual ~IDataBase() {}
virtual void run() = 0;
};
template <typename FunCall>
struct CDataBase : public IDataBase
{
CDataBase(FunCall&& f)
: m_fun(std::forward<FunCall>(f))
{}
void run()
{
m_fun();
}
FunCall m_fun;
};
class QJEventData
{
public:
template <typename FunCall, typename ... Args>
QJEventData(FunCall&& f, Args&& ... args)
{
m_pFun = makeCDataBase(std::bind(std::forward<FunCall>(f), std::forward<Args>(args)...));
}
void run()
{
m_pFun->run();
}
private:
template <typename FunCall>
std::shared_ptr<CDataBase<FunCall>> makeCDataBase(FunCall&& f)
{
return std::make_shared<CDataBase<FunCall>>(std::forward<FunCall>(f));
}
std::shared_ptr<IDataBase> m_pFun;
};
// this loop for other thread sendevent to main thread
class QJEventLoop
{
typedef std::list<std::shared_ptr<QJEventData>> eventDataList;
typedef std::shared_ptr<eventDataList> eventDataListPtr;
public:
void init()
{
m_mainId = (std::this_thread::get_id ());
}
bool loop()
{
if ( std::this_thread::get_id () != m_mainId)
{
//error not run in main thread
return false;
}
eventDataListPtr eventList;
{
qijia::QJMutex lock (mtx);
eventList.reset (new eventDataList(*m_eventList));
m_eventList->clear();
}
auto iter = eventList->begin();
while(iter != eventList->end())
{
(*iter)->run();
iter++;
}
return true;
}
static QJEventLoop& App()
{
static QJEventLoop instance;
return instance;
}
void addEventList(std::shared_ptr<QJEventData> event)
{
qijia::QJMutex lock (mtx);
if (!m_eventList.unique ())
{
m_eventList.reset (new eventDataList(*m_eventList));
}
m_eventList->push_back(event);
}
private:
QJEventLoop()
:m_mainId(std::this_thread::get_id ())
{
m_eventList.reset(new eventDataList);
}
std::thread::id m_mainId;
eventDataListPtr m_eventList;
std::mutex mtx;
};
}