forked from caozhiyi/CppNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_thread.cpp
69 lines (58 loc) · 1.53 KB
/
func_thread.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
#include "func_thread.h"
#include "info_router.h"
FuncThread::FuncThread(std::shared_ptr<InfoRouter>& router):
_func_router(router) {
}
FuncThread::~FuncThread() {
}
void FuncThread::Run() {
while (!_stop) {
auto t = Pop();
if (t) {
if (CallFunc(t->_func_name, t->_func_param_ret)){
_func_router->PushRet(t);
}
} else {
continue;
}
}
}
void FuncThread::Stop() {
_stop = true;
Push(nullptr);
}
bool FuncThread::RegisterFunc(const std::string& name, const CommonFunc& func) {
std::unique_lock<std::mutex> lock(_mutex);
if (_func_map.count(name)) {
return false;
}
_func_map[name] = func;
return true;
}
bool FuncThread::RemoveFunc(const std::string& name) {
std::unique_lock<std::mutex> lock(_mutex);
auto iter = _func_map.find(name);
if (iter != _func_map.end()) {
_func_map.erase(iter);
return true;
}
return false;
}
CommonFunc FuncThread::FindFunc(const std::string& name) {
std::unique_lock<std::mutex> lock(_mutex);
auto iter = _func_map.find(name);
if (iter != _func_map.end()) {
return iter->second;
}
return nullptr;
}
bool FuncThread::CallFunc(const std::string& name, std::vector<cppnet::Any>& param_ret) {
auto iter = _func_map.find(name);
if (iter == _func_map.end()) {
return false;
}
//client responsible for parameter verification
//so can direct call here
param_ret = iter->second(param_ret);
return true;
}