aboutsummaryrefslogtreecommitdiff
path: root/src/tests/testSignals.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/testSignals.cc')
-rw-r--r--src/tests/testSignals.cc172
1 files changed, 0 insertions, 172 deletions
diff --git a/src/tests/testSignals.cc b/src/tests/testSignals.cc
deleted file mode 100644
index d316a62..0000000
--- a/src/tests/testSignals.cc
+++ /dev/null
@@ -1,172 +0,0 @@
1#include <iostream>
2using namespace std;
3
4#include "../FbTk/Signal.hh"
5#include "../FbTk/MemFun.hh"
6
7#include <string>
8
9
10
11struct NoArgument {
12 void operator() () const {
13 cout << "No Argument." << endl;
14 }
15};
16
17struct OneArgument {
18 void operator ()( int value ) {
19 cout << "One argument = " << value << endl;
20 }
21};
22
23struct TwoArguments {
24 template <typename T1, typename T2>
25 void operator ()( const T1& value, const T2& message ) {
26 cout << "Two arguments, (1) = " << value << ", (2) = " << message << endl;
27 }
28};
29
30struct ThreeArguments {
31 void operator ()( int value, const string& message, double value2 ) {
32 cout << "Two arguments, (1) = " << value << ", (2) = " << message
33 << ", (3) = " << value2 << endl;
34 }
35};
36
37struct FunctionClass {
38 FunctionClass() {
39 cout << "FunctionClass created." << endl;
40 }
41 ~FunctionClass() {
42 cout << "FunctionClass deleted." << endl;
43 }
44 void print() {
45 cout << "Printing." << endl;
46 }
47
48 void takeIt( string& str ) {
49 cout << "FunctionClass::takeIt( " << str << " )" << endl;
50 }
51
52 void showMessage( int value, const string& message ) {
53 cout << "(" << value << "): " << message << endl;
54 }
55 void showMessage2( const string& message1, const string& message2) {
56 cout << "(" << message1 << ", " << message2 << ")" << endl;
57 }
58 void threeArgs( int value, const string& str, double pi ) {
59 cout << "(" << value << "): " << str << ", pi = " << pi << endl;
60 }
61
62};
63
64struct Printer {
65 void printInt(int value) {
66 cout << "Int:" << value << endl;
67 }
68 void printString(string value) {
69 cout << "string:" << value << endl;
70 }
71 void printFloat(float value) {
72 cout << "Float:" << value << endl;
73 }
74};
75
76int main() {
77 using FbTk::Signal;
78 using FbTk::SignalTracker;
79
80 Signal<> no_arg;
81 no_arg.connect( NoArgument() );
82
83 Signal<int> one_arg;
84 one_arg.connect( OneArgument() );
85
86 Signal<int, const string&> two_args;
87 two_args.connect( TwoArguments() );
88
89 Signal<int, const string&, double> three_args;
90 three_args.connect( ThreeArguments() );
91
92 // emit test
93 no_arg.emit();
94 one_arg.emit( 10 );
95 two_args.emit( 10, "Message" );
96 three_args.emit( 10, "Three", 3.141592 );
97
98 // test signal tracker
99 {
100 cout << "---- tracker ----" << endl;
101 SignalTracker tracker;
102 // setup two new slots and track them
103 SignalTracker::TrackID id_no_arg = tracker.join( no_arg, NoArgument() );
104 SignalTracker::TrackID id_one_arg = tracker.join( one_arg, OneArgument() );
105
106 // two outputs each from these two signals
107 no_arg.emit();
108 one_arg.emit( 31 );
109
110 // stop tracking id_one_arg, which should keep the slot after this scope,
111 // the id_no_arg connection should be destroyed after this.
112 tracker.leave( id_one_arg );
113 cout << "---- tracker end ----" << endl;
114 }
115
116 // now we should have one output from no_arg and two outputs from one_arg
117 no_arg.emit();
118 one_arg.emit( 2 );
119
120 using FbTk::MemFun;
121 FunctionClass obj;
122 no_arg.clear();
123 no_arg.connect(MemFun(obj, &FunctionClass::print));
124 no_arg.emit();
125
126 string takeThis("Take this");
127 Signal<string&> ref_arg;
128 ref_arg.connect(MemFun(obj, &FunctionClass::takeIt));
129 ref_arg.emit( takeThis );
130
131 two_args.clear();
132 two_args.connect(MemFun(obj, &FunctionClass::showMessage));
133 two_args.emit(10, "This is a message");
134
135 three_args.clear();
136 three_args.connect(MemFun(obj, &FunctionClass::threeArgs));
137 three_args.emit(9, "nine", 3.141592);
138
139 // Test ignore signals
140 {
141 cout << "----------- Testing ignoring arguments for signal." << endl;
142 using FbTk::MemFunIgnoreArgs;
143 // Create a signal that emits with three arguments, and connect
144 // sinks that takes less than three arguments.
145 Signal<string, string, float> more_args;
146 more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::print));
147 more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::takeIt));
148 more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::showMessage2));
149 more_args.emit("This should be visible for takeIt(string)",
150 "Visible to the two args function.",
151 2.9);
152
153 }
154
155 // Test argument selector
156 {
157 using namespace FbTk;
158 Signal<int, string, float> source;
159
160 Printer printer;
161 source.connect(MemFunSelectArg0(printer, &Printer::printInt));
162 source.connect(MemFunSelectArg1(printer, &Printer::printString));
163 source.connect(MemFunSelectArg2(printer, &Printer::printFloat));
164
165 source.emit(10, "hello", 3.141592);
166
167 Signal<string, int> source2;
168 source2.connect(MemFunSelectArg0(printer, &Printer::printString));
169 source2.connect(MemFunSelectArg1(printer, &Printer::printInt));
170 source2.emit("world", 37);
171 }
172}