-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
147 lines (130 loc) · 3.58 KB
/
main.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "NativeSignal.h"
#include "TemplateSignal.h"
#include "MuduoSignal.h"
#include "Signal.h"
#include <iostream>
#include <cmath>
#include <functional>
#include <boost/signals2.hpp>
#include <boost/bind.hpp>
using namespace std;
void test_native_signal() {
using namespace NativeSignal;
Signal signal;
Method method;
int x = 0;
signal.connect(&method, &Method::method1);
cout << signal(x) << "\n";
signal.connect(&method, &Method::method2);
cout << signal(x) << "\n";
signal.connect(&method, &Method::method3);
cout << signal(x) << "\n";
}
void test2() {
cout << "Test2\n";
}
class Test3 {
int x_;
public:
Test3(int x):x_(x) {}
void test3() {
// x_ = 4;
cout << "Test3 " << x_ << "\n";
}
};
void test_template_signal() {
using namespace TemplateSignal;
Signal<void()> signal;
signal.connect([](){
cout << "Test1\n";
});
function<void()> test2_func = test2;
signal.connect(test2);
//signal.connect(test2_func);
signal.connect(Test3(0), &Test3::test3);
Test3 test4(4);
signal.connect(&test4, &Test3::test3);
//signal();
Signal<double(string, string)> signal1;
signal1.connect([](string x, string y) -> double {
return x.size() - y.size();
});
cout << "signal1: " << signal1("123", "1") << "\n";
string x = "123", y = "1";
string &&z = "hello right value";
//cout << "signal1: " << signal1(x, y) << "\n";
}
void test_muduo_signal() {
using namespace muduo;
vector<Slot> slots;
Signal<void()> signal;
slots.push_back(
signal.connect([](){
cout << "test1\n";
}));
slots.push_back(signal.connect(test2));
slots.push_back(signal.connect(bind(&Test3::test3, Test3(0))));
signal.call();
}
double sqrt1(int x, int y) {
cout << "native function: " << std::sqrt(x * x + y * y) << endl;
}
class Sqrt {
public:
double cal(int x, int y) {
cout << "member function: " << std::sqrt(x * x + y * y) << endl;
}
};
void test_signal() {
using namespace Utils;
vector<Slot> slots;
Signal<void()> signal;
slots.push_back(
signal.connect([](){
cout << "test1\n";
}));
signal.connect(test2);
// slots.push_back(signal.connect(test2));
slots.push_back(signal.connect(bind(&Test3::test3, Test3(0))));
signal();
Signal<double(int, int)> signal1;
slots.push_back(signal1.connect([](int x, int y) -> double {
cout << "lambda function: " << std::sqrt(x * x + y * y) << endl;
}));
slots.push_back(signal1.connect(sqrt1));
slots.push_back(signal1.connect(std::bind(&Sqrt::cal, Sqrt(), std::placeholders::_1, std::placeholders::_2)));
signal1(3, 4);
}
void print() {
cout << "\n";
}
template<typename T, typename... Args>
void print(T t, Args&&... args) {
cout << t << " ";
print(std::forward<Args>(args)...);
}
void test_boost_signal() {
boost::signals2::signal<bool(const string&, const string&)> signal;
function<bool(const string&, const string&)> compare = [](const string& x, const string& y) -> bool {
if (x < y) {
print(x, "is less than", y);
}
};
signal.connect(compare);
signal.connect([](const string& x, const string& y) -> bool {
if (x > y) {
print(x, "is greater than", y);
}
});
signal("123", "1");
string x = "1", y = "123";
signal(x, y);
}
int main() {
//test_template_signal();
test_boost_signal();
// const Test3 t(10);
// bind(&Test3::test3, t)();
//test_signal();
return 0;
}