-
Notifications
You must be signed in to change notification settings - Fork 13
/
example.cpp
170 lines (134 loc) · 3.71 KB
/
example.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "rocket.hpp"
#include <iostream>
struct Testing : rocket::trackable
{
int hello(float a)
{
std::cout << "Testing: " << a << std::endl;
return 0;
}
};
struct NonDefaultConstructible
{
explicit NonDefaultConstructible(int x)
: value{ x }
{
}
~NonDefaultConstructible()
{
std::cout << "Destructor called for value: " << value << std::endl;
}
NonDefaultConstructible(NonDefaultConstructible&& n)
: value{ n.value }
{
n.value = 0;
}
NonDefaultConstructible& operator = (NonDefaultConstructible&& n)
{
value = n.value;
n.value = 0;
return *this;
}
private:
int value;
NonDefaultConstructible() = delete;
NonDefaultConstructible(NonDefaultConstructible const&) = delete;
NonDefaultConstructible& operator = (NonDefaultConstructible const&) = delete;
};
struct TestShared : std::enable_shared_from_this<TestShared>
{
int hello(int a)
{
return 321;
}
};
int main()
{
NonDefaultConstructible n{ 1337 };
{
rocket::stable_list<NonDefaultConstructible> list;
list.push_back(std::move(n));
}
{
rocket::stable_list<int> list1{ 1, 2, 3, 4, 5 };
rocket::stable_list<int> list2{ list1.crbegin(), list1.crend() };
list2.resize(3);
std::cout << "List size: " << list2.size() << std::endl;
for (auto elem : list2)
std::cout << elem << ' ';
std::cout << std::endl;
}
rocket::signal<int(int)> test;
test.connect([](int x) {
return x * 3;
});
test.connect([](int x) {
return x * 1;
});
test.connect([](int x) {
return x * 2;
});
{
// Give me the minimal value of all slots
typedef rocket::minimum<int> selector;
std::cout << "Minimum: " << test.invoke<selector>(5) << std::endl;
}
{
// Give me the last result in an optional (default behaviour)
rocket::optional<int> r{ test(5) };
std::cout << "Optional: " << *r << std::endl;
}
// Connect a new slot via scoped connections
{
rocket::scoped_connection scoped{
test.connect([](int x) {
return x * 4;
})
};
// Give me all results in a list
typedef rocket::range<int> selector;
std::cout << "Range: ";
for (int x : test.invoke<selector>(5)) {
std::cout << x << " ";
}
std::cout << std::endl;
}
{
// The connections are only connected as long as the testing-object is alive
Testing testing;
test.connect(testing, &Testing::hello);
test.connect(testing, &Testing::hello);
test(1337);
}
{
auto classPtr = std::make_shared<TestShared>();
auto f = rocket::bind_weak_ptr(classPtr, &TestShared::hello);
f(2);
}
{
// A slot that kills itself after the first call
test.connect([](int) {
// Get the connection object associated with this slot and kill it
rocket::current_connection().disconnect();
std::cout << "called slot disconnect!" << std::endl;
return 0;
});
test(1337);
test(1337);
}
{
// A slot that aborts emission after the first call
test.connect([](int) {
rocket::abort_emission();
std::cout << "called abort!" << std::endl;
return 0;
});
test.connect([](int) {
std::cout << "This should never show up, as the previous slot aborts the emission!" << std::endl;
return 0;
});
test(1337);
}
std::cin.get();
return 0;
}