forked from Yurii-huang/pluma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlumaPipe.h
112 lines (89 loc) · 2.8 KB
/
PlumaPipe.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
#ifndef PLUMAPIPE_H
#define PLUMAPIPE_H
#include <list>
#include <functional>
#include <iostream>
#include <thread>
#include <bitset>
#include <map>
#include "PlumaCore.h"
template<typename...Args>
class PlumaPipe
{
typedef std::multimap<PlumaCore::MethodVarient,std::function<void(Args...)>> CallBackMapT; //函数转发列表
CallBackMapT _mapping;
public:
enum ConnType //size is one
{
Ct_CallBack = 0b00000001, //默认回调 defualt
Ct_ThJoined = 0b00000011, //线程等待
Ct_ThDirect = 0b00000010, //线程分离
Ct_OnlyOne = 0b00000100 //只有绑定一次
};
explicit PlumaPipe() //has defualt value
{
}
virtual ~PlumaPipe()
{
}
template<typename Ct,typename Ft = void(Ct::*)(Args...)>
void connect(Ct* handler,Ft func,ConnType type = Ct_CallBack)
{
switch (type)
{
case Ct_CallBack:
_mapping.insert({PlumaCore::_ConvtAnyBAToFormat<Ft>(func),
[=](Args...as)
{
(handler->*func)(as...);
}
});
break;
case Ct_ThJoined:
_mapping.insert({PlumaCore::_ConvtAnyBAToFormat<Ft>(func),
[=](Args...as){
std::thread th(func,handler,as...);
th.join();
}});
break;
case Ct_ThDirect:
_mapping.insert({PlumaCore::_ConvtAnyBAToFormat<Ft>(func),
[=](Args...as)
{
std::thread th(func,handler,as...);
th.detach();
}
});
break;
case Ct_OnlyOne:
if(_mapping.find(PlumaCore::_ConvtAnyBAToFormat<Ft>(func)) != _mapping.end())
return;
else
{
_mapping.insert({PlumaCore::_ConvtAnyBAToFormat<Ft>(func),
[=](Args...as)
{
(handler->*func)(as...);
}
});
}
break;
default:
throw "Error, can't connect class method,case connect type is unkonwn";
break;
}
}
template<typename Ct,typename Ft = void(Ct::*)(Args...)>
void disconnect(Ct* handler,Ft func)
{
_mapping.erase(PlumaCore::_ConvtAnyBAToFormat<Ft>(func));
}
void send(Args...as)
{
for(auto var:_mapping)
{
(var.second)(as...);
}
}
};
#endif // PLUMAPIPE_H