forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_object.hpp
88 lines (75 loc) · 2.64 KB
/
sf_object.hpp
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
/**
* @version 1.0.0
* @author skyfire
* @mail [email protected]
* @see http://github.com/skyfireitdiy/sflib
* @file sf_object.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
/*
* sf_object 基础对象
* 提供了信号---槽的通信机制、AOP编程
*/
#pragma once
#include "sf_object.h"
namespace skyfire
{
template<typename _VectorType, typename _FuncType>
int sf_object::__sf_bind_helper(std::recursive_mutex &mu, _VectorType &vec, _FuncType func, bool mul_thread) {
std::lock_guard<std::recursive_mutex> lck(mu);
int bind_id = rand();
while(std::find_if(vec.begin(),vec.end(),[=](auto p){
return std::get<2>(p) == bind_id;
}) != vec.end())
{
bind_id = rand();
}
vec.push_back(std::make_tuple(func, mul_thread, bind_id));
return bind_id;
}
inline sf_object::~sf_object() {
__p_msg_queue__->remove_msg(this);
}
template<typename _VectorType>
void sf_object::__sf_aop_unbind_helper(std::recursive_mutex &mu, _VectorType &vec, int bind_id) {
std::lock_guard<std::recursive_mutex> lck(mu);
vec.erase(std::remove_if(vec.begin(),vec.end(),[=](auto p){
return std::get<1>(p) == bind_id;
}), vec.end());
}
template<typename _VectorType, typename _FuncType>
int sf_object::__sf_aop_after_add_helper(std::recursive_mutex &mu, _VectorType &vec, _FuncType func) {
std::lock_guard<std::recursive_mutex> lck(mu);
int bind_id = rand();
while(std::find_if(vec.begin(),vec.end(),[=](auto p){
return std::get<1>(p) == bind_id;
}) != vec.end())
{
bind_id = rand();
}
vec.push_back(std::make_tuple(func, bind_id));
return bind_id;
}
template<typename _VectorType, typename _FuncType>
int sf_object::__sf_aop_before_add_helper(std::recursive_mutex &mu, _VectorType &vec, _FuncType func) {
std::lock_guard<std::recursive_mutex> lck(mu);
int bind_id = rand();
while(std::find_if(vec.begin(),vec.end(),[=](auto p){
return std::get<1>(p) == bind_id;
}) != vec.end())
{
bind_id = rand();
}
vec.insert(vec.begin() ,std::make_tuple(func, bind_id));
return bind_id;
}
template<typename _VectorType>
void sf_object::__sf_signal_unbind_helper(std::recursive_mutex &mu, _VectorType &vec, int bind_id) {
std::lock_guard<std::recursive_mutex> lck(mu);
vec.erase(std::remove_if(vec.begin(),vec.end(),[=](auto p){
return std::get<2>(p) == bind_id;
}), vec.end());
}
}