C++ 0x 使用可变参数模板类 实现 C# 的委托机制
生活随笔
收集整理的這篇文章主要介紹了
C++ 0x 使用可变参数模板类 实现 C# 的委托机制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #ifndef _ZTC_DELEGATE_H_
2 #define _ZTC_DELEGATE_H_
3
4 #include <vector>
5 #include <functional>
6
7 ///
8 // C++ 使用 可變參數模板類, 來實現
9 // C#中的 委托
10 // Anchor: ztc
11 // Date : 2014-01-10
12 ///
13
14 template<typename R, typename ...Args>
15 class Delegate {
16 public:
17 template<typename U>
18 Delegate& operator += (const U &func) {
19 funcList.push_back(std::function<R(Args...)>(func));
20 funcPointers.push_back((void*)&func);
21 return *this;
22 }
23
24 template<typename U>
25 Delegate& operator -= (const U &func) {
26 int i = -1;
27 for (auto iter = funcPointers.begin(); iter != funcPointers.end(); iter++) {
28 i++;
29 if (*iter == (void*)&func) {
30 funcPointers.erase(iter);
31 funcList.erase(funcList.begin() + i);
32 break;
33 }
34 }
35 return *this;
36 }
37
38 std::vector<R> operator()(Args...args) {
39 std::vector<R> ret;
40 for (auto f : funcList) {
41 ret.push_back(f(args...));
42 }
43 return ret;
44 }
45 private:
46 std::vector<std::function<R(Args...)>> funcList;
47 std::vector<void*> funcPointers;
48 };
49
50 template<typename ...Args>
51 class Delegate<void, Args...> {
52 public:
53 template<typename U>
54 Delegate& operator += (const U &func) {
55 std::cout << "注冊方法 " << typeid(func).name() << std::endl;
56 funcList.push_back(std::function<void(Args...)>(func));
57 funcPointers.push_back((void*)&func);
58 return *this;
59 }
60
61 template<typename U>
62 Delegate& operator -= (const U &func) {
63 std::cout << "卸載方法 " << typeid(func).name() << std::endl;
64 int i = -1;
65 for (auto iter = funcPointers.begin(); iter != funcPointers.end(); iter++) {
66 i++;
67 if (*iter == (void*)&func) {
68 funcPointers.erase(iter);
69 funcList.erase(funcList.begin() + i);
70 break;
71 }
72 }
73 return *this;
74 }
75
76 void operator() (Args... args) {
77 for (auto f : funcList) {
78 f(args...);
79 }
80 }
81 private:
82 std::vector<std::function<void(Args...)>> funcList;
83 std::vector<void*> funcPointers;
84 };
85
86 #endif // _ZTC_DELEGATE_H_ ztc_Delegate.hpp
4 int foo(int a, int b) { 5 return a * a + b * b; 6 } 7 // 普通無參無返回函數
8 void kaoo() { 9 std::cout << "kaooooooo" << std::endl; 10 } 11 void kaoo2() { 12 std::cout << "kaooooo22222oo" << std::endl; 13 } 14 // 類成員函數
15 class Test { 16 public: 17 void funcInClass() { 18 std::cout << "Function In Class" << std::endl; 19 } 20 }; 21 22 int main() { 23 // 定義事件 有返回值 24 Delegate<int, int, int> OnSomething; 25 // 定義事件 無返回值 26 Delegate<void> OnKao; 27 28 // 注冊方法 29 OnSomething += [](int a, int b) {return a + b; }; 30 OnSomething += [](int a, int b) {return a * b; }; 31 OnSomething += foo; 32 33 // 類的成員函數 需要 使用 Bind 34 Test c; 35 auto cf = std::bind(&Test::funcInClass, c); 36 37 // 注冊類成員函數 38 OnKao += cf; 39 // 注冊普通函數 40 OnKao += kaoo; 41 OnKao += kaoo2; 42 // 調用事件 43 OnKao(); 44 // 卸載類成員函數 45 OnKao -= cf; 46 // 制裁普通函數 47 OnKao -= kaoo; 48 // 調用方法 49 OnKao(); 50 51 // 調用事件 得到 結果 52 auto ret = OnSomething(2, 6); 53 54 // 顯示結果 55 for (auto r : ret) { 56 std::cout << r << std::endl; 57 } 58 59 return 0; 60 }
?
1 #include <iostream> 2 #include "ztc_Delegate.hpp" 3 // 普通函數4 int foo(int a, int b) { 5 return a * a + b * b; 6 } 7 // 普通無參無返回函數
8 void kaoo() { 9 std::cout << "kaooooooo" << std::endl; 10 } 11 void kaoo2() { 12 std::cout << "kaooooo22222oo" << std::endl; 13 } 14 // 類成員函數
15 class Test { 16 public: 17 void funcInClass() { 18 std::cout << "Function In Class" << std::endl; 19 } 20 }; 21 22 int main() { 23 // 定義事件 有返回值 24 Delegate<int, int, int> OnSomething; 25 // 定義事件 無返回值 26 Delegate<void> OnKao; 27 28 // 注冊方法 29 OnSomething += [](int a, int b) {return a + b; }; 30 OnSomething += [](int a, int b) {return a * b; }; 31 OnSomething += foo; 32 33 // 類的成員函數 需要 使用 Bind 34 Test c; 35 auto cf = std::bind(&Test::funcInClass, c); 36 37 // 注冊類成員函數 38 OnKao += cf; 39 // 注冊普通函數 40 OnKao += kaoo; 41 OnKao += kaoo2; 42 // 調用事件 43 OnKao(); 44 // 卸載類成員函數 45 OnKao -= cf; 46 // 制裁普通函數 47 OnKao -= kaoo; 48 // 調用方法 49 OnKao(); 50 51 // 調用事件 得到 結果 52 auto ret = OnSomething(2, 6); 53 54 // 顯示結果 55 for (auto r : ret) { 56 std::cout << r << std::endl; 57 } 58 59 return 0; 60 }
?
轉載于:https://www.cnblogs.com/easyfrog/p/3513487.html
總結
以上是生活随笔為你收集整理的C++ 0x 使用可变参数模板类 实现 C# 的委托机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UISwitch的大小
- 下一篇: 图像同态滤波 python实现_8图像增