-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
executable file
·160 lines (137 loc) · 3.95 KB
/
test.cpp
File metadata and controls
executable file
·160 lines (137 loc) · 3.95 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include "sigslot.hpp"
using namespace std;
// Hello, World! (Beginner)
// The following example writes "Hello, World!" using signals and slots. First,
// we create a signal sig, a signal that takes no arguments and has a void return value.
// Next, we connect the hello function object to the signal using the connect method.
// Finally, use the signal sig like a function to call the slots, which in turns invokes HelloWorld::operator() to print "Hello, World!".
struct HelloWorld
{
void operator()() const
{
cout << "Hello, World!" << endl;
}
};
void test1()
{
utils::sigslot<void()> sig;
HelloWorld hello;
sig.connect(hello);
sig();
}
// Slots aren't expected to exist indefinitely after they are connected.
// Often slots are only used to receive a few events and are then disconnected,
// and the programmer needs control to decide when a slot should no longer be connected.
void test2()
{
utils::sigslot<void()> sig;
HelloWorld hello;
auto id = sig.connect(hello);
sig();
sig.disconnect(id);
sig();
}
// Connecting Multiple Slots (Beginner)
// Calling a single slot from a signal isn't very interesting,
// so we can make the Hello, World program more interesting by splitting the work of printing "Hello, World!" into two completely separate slots.
// The first slot will print "Hello" and may look like this:
struct Hello
{
void operator()() const
{
std::cout << "Hello";
}
};
struct World
{
void operator()() const
{
std::cout << ", World!" << std::endl;
}
};
void test3()
{
utils::sigslot<void()> sig;
Hello hello;
World world;
sig.connect(hello);
sig.connect(world);
sig();
}
// Slot Arguments (Beginner)
// Signals can propagate arguments to each of the slots they call.
// For instance, a signal that propagates mouse motion events might want to pass along the new mouse coordinates and whether the mouse buttons are pressed.
// As an example, we'll create a signal that passes two float arguments to its slots.
// Then we'll create a few slots that print the results of various arithmetic operations on these values.
void print_args(float x, float y)
{
std::cout << "The arguments are " << x << " and " << y << std::endl;
}
void print_sum(float x, float y)
{
std::cout << "The sum is " << x + y << std::endl;
}
void print_product(float x, float y)
{
std::cout << "The product is " << x * y << std::endl;
}
void print_difference(float x, float y)
{
std::cout << "The difference is " << x - y << std::endl;
}
void print_quotient(float x, float y)
{
std::cout << "The quotient is " << x / y << std::endl;
}
void test4()
{
utils::sigslot<void(float, float)> sig;
sig.connect(&print_args);
sig.connect(&print_sum);
sig.connect(&print_product);
sig.connect(&print_difference);
sig.connect(&print_quotient);
sig(5., 3.);
}
// Automatic Connection Management (Intermediate)
// SignalSlot can automatically track the lifetime of objects involved in signal/slot connections,
// including automatic disconnection of slots when objects involved in the slot call are destroyed.
class subject
{
public:
void function(int c)
{
cout << "in subject::function c=" << c << endl;
}
};
void test5()
{
using SigSlot = utils::sigslot<void(int)>;
SigSlot sig;
auto s = std::make_shared<subject>();
sig.connect(SigSlot::SlotType(std::bind(&subject::function, s.get(), std::placeholders::_1)).track(s));
sig(99);
s.reset();
sig(66);
}
//test lamda
void test6()
{
using SigSlot = utils::sigslot<void(int)>;
SigSlot sig;
sig.connect([](int a) {
cout << "in lamda a= " << a << endl;
});
sig(88);
}
int main()
{
test1();
test2();
test3();
test4();
test5();
test6();
return 0;
}