我是靠谱客的博主 甜美哈密瓜,这篇文章主要介绍C++ 简单实现信号槽,现在分享给大家,希望可以做个参考。

原理:保存函数绑定类的函数指针,进行回调。

1.信号参数为指定个数,指定类型

复制代码
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
#include <iostream> #include <stdlib.h> #include <vector> using namespace std; class SlotBase { public: virtual void Exec(int param1) = 0; virtual ~SlotBase() {} }; template<class T> class Slot :public SlotBase { typedef void (T::*Func)(int); public: Slot(T* pOjb, Func func) { m_func = func; m_pSlotBase = pOjb; } void Exec(int param1) { (m_pSlotBase->*m_func)(param1); } private: T* m_pSlotBase = NULL; Func m_func; }; class Signal { public: template<class T> void Bind(T* obj, void (T::*func)(int)) { m_pSlotSet.push_back(new Slot<T>(obj, func)); } void operator()(int param1) { for (int i = 0; i < (int)m_pSlotSet.size(); i++) { m_pSlotSet[i]->Exec(param1); } } private: vector< SlotBase* > m_pSlotSet; }; class TestFunc1 { public: void FunOfA(int param) { cout << __FUNCTION__ << " " << param << endl; } }; class TestSignal { public: TestSignal() { } void emit(int value) { ValueChanged(value); } public: Signal ValueChanged; }; #define Connect(sender,signal,receiver,method)((sender)->signal.Bind(receiver,method)); int main() { TestFunc1* pFunc1 = new TestFunc1; TestSignal* pSinal1 = new TestSignal(); Connect(pSinal1, ValueChanged, pFunc1, &TestFunc1::FunOfA); pSinal1->emit(1); delete pFunc1; delete pSinal1; getchar(); return 0; }

2.信号参数为指定个数,不定类

复制代码
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
114
115
#include <iostream> #include <stdlib.h> #include <vector> using namespace std; template<class T> class SlotBase { public: virtual void Exec(T param1) = 0; virtual ~SlotBase() {} }; template<class T, class T1> class Slot :public SlotBase<T1> { typedef void (T::*Func)(T1); public: Slot(T* pOjb, Func func) { m_func = func; m_pSlotBase = pOjb; } void Exec(T1 param1) { (m_pSlotBase->*m_func)(param1); } private: T* m_pSlotBase = NULL; Func m_func; }; template <class T1> class Signal { public: template<class T> void Bind(T* obj, void (T::*func)(T1)) { m_pSlotSet.push_back(new Slot<T, T1>(obj, func)); } void operator()(T1 param1) { for (int i = 0; i < (int)m_pSlotSet.size(); i++) { m_pSlotSet[i]->Exec(param1); } } private: vector< SlotBase<T1>* > m_pSlotSet; }; class TestFunc1 { public: void FunOfA(int param) { cout << __FUNCTION__ << " " << param << endl; } }; class TestFunc2 { public: void FuncOfB(std::string param) { cout << __FUNCTION__ << " " << param.c_str() << endl; } }; template<class T1> class TestSignal { public: TestSignal() { } void emit(T1 value) { ValueChanged(value); } public: Signal<T1> ValueChanged; }; #define Connect(sender,signal,receiver,method)((sender)->signal.Bind(receiver,method)); int main() { TestFunc1* pFunc1 = new TestFunc1; TestFunc2* pFunc2 = new TestFunc2; TestSignal<int>* pSinal1 = new TestSignal<int>(); TestSignal<std::string>* pSinal2 = new TestSignal<std::string>(); Connect(pSinal1, ValueChanged, pFunc1, &TestFunc1::FunOfA); Connect(pSinal2, ValueChanged, pFunc2, &TestFunc2::FuncOfB); pSinal1->emit(1); pSinal2->emit("hahhaha"); delete pFunc1; delete pFunc2; delete pSinal2; delete pSinal1; getchar(); return 0; }

3.信号参数为指定不定个数,不定类型

复制代码
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream> #include <stdlib.h> #include <vector> using namespace std; template< class ...Args> class SlotBase { public: virtual void Exec(Args...args) = 0; virtual ~SlotBase() {} }; template<class T, class ...Args> class Slot :public SlotBase<Args...> { typedef void (T::*Func)(Args...); public: Slot(T* pOjb, Func func) { m_func = func; m_pSlotBase = pOjb; } void Exec(Args...args) { (m_pSlotBase->*m_func)(args...); } private: T* m_pSlotBase = NULL; Func m_func; }; template <class...Args> class Signal { public: template<class T> void Bind(T* obj, void (T::*func)(Args...)) { m_pSlotSet.push_back(new Slot<T, Args...>(obj, func)); } void operator()(Args...args) { for (int i = 0; i < (int)m_pSlotSet.size(); i++) { m_pSlotSet[i]->Exec(args...); } } private: vector< SlotBase<Args...>* > m_pSlotSet; }; class TestFunc1 { public: void FunOfA(int param) { cout << __FUNCTION__ << " " << param << endl; } }; class TestFunc2 { public: void FuncOfB(std::string param) { cout << __FUNCTION__ << " " << param.c_str() << endl; } }; class TestFunc3 { public: void FuncOfC(int parm1,std::string param2,float params3) { cout << __FUNCTION__ << " " << parm1 << " " << param2.c_str() << " " << params3 << endl; } }; template<class...Args> class TestSignal { public: TestSignal() { } void emit(Args...args) { ValueChanged(args...); } public: Signal<Args...> ValueChanged; }; #define Connect(sender,signal,receiver,method)((sender)->signal.Bind(receiver,method)); int main() { TestFunc1* pFunc1 = new TestFunc1; TestFunc2* pFunc2 = new TestFunc2; TestFunc3* pFunc3 = new TestFunc3; TestSignal<int>* pSinal1 = new TestSignal<int>(); TestSignal<std::string>* pSinal2 = new TestSignal<std::string>(); TestSignal<int,std::string,float>* pSinal3= new TestSignal<int, std::string, float>(); Connect(pSinal1, ValueChanged, pFunc1, &TestFunc1::FunOfA); Connect(pSinal2, ValueChanged, pFunc2, &TestFunc2::FuncOfB); Connect(pSinal3, ValueChanged, pFunc3, &TestFunc3::FuncOfC); pSinal1->emit(1); pSinal2->emit("hahhaha"); pSinal3->emit(1,"heheh",2.3f); delete pFunc1; delete pFunc2; delete pSinal2; delete pSinal1; delete pFunc3; delete pSinal3; getchar(); return 0; }

 

最后

以上就是甜美哈密瓜最近收集整理的关于C++ 简单实现信号槽的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部