4
#include "../FbTk/Signal.hh"
5
#include "../FbTk/MemFun.hh"
6
#include "../FbTk/RelaySignal.hh"
7
#include "../FbTk/Observer.hh"
8
#include "../FbTk/Subject.hh"
15
void operator() () const {
16
cout << "No Argument." << endl;
21
void operator ()( int value ) {
22
cout << "One argument = " << value << endl;
27
template <typename T1, typename T2>
28
void operator ()( const T1& value, const T2& message ) {
29
cout << "Two arguments, (1) = " << value << ", (2) = " << message << endl;
33
struct ThreeArguments {
34
void operator ()( int value, const string& message, double value2 ) {
35
cout << "Two arguments, (1) = " << value << ", (2) = " << message
36
<< ", (3) = " << value2 << endl;
40
struct FunctionClass {
42
cout << "FunctionClass created." << endl;
45
cout << "FunctionClass deleted." << endl;
48
cout << "Printing." << endl;
51
void takeIt( string& str ) {
52
cout << "FunctionClass::takeIt( " << str << " )" << endl;
55
void showMessage( int value, const string& message ) {
56
cout << "(" << value << "): " << message << endl;
58
void showMessage2( const string& message1, const string& message2) {
59
cout << "(" << message1 << ", " << message2 << ")" << endl;
61
void threeArgs( int value, const string& str, double pi ) {
62
cout << "(" << value << "): " << str << ", pi = " << pi << endl;
68
void printInt(int value) {
69
cout << "Int:" << value << endl;
71
void printString(string value) {
72
cout << "string:" << value << endl;
74
void printFloat(float value) {
75
cout << "Float:" << value << endl;
81
using FbTk::SignalTracker;
84
no_arg.connect( NoArgument() );
86
Signal<void, int> one_arg;
87
one_arg.connect( OneArgument() );
89
Signal<void, int, const string&> two_args;
90
two_args.connect( TwoArguments() );
92
Signal<void, int, const string&, double> three_args;
93
three_args.connect( ThreeArguments() );
98
two_args.emit( 10, "Message" );
99
three_args.emit( 10, "Three", 3.141592 );
101
// test signal tracker
103
cout << "---- tracker ----" << endl;
104
SignalTracker tracker;
105
// setup two new slots and track them
106
SignalTracker::TrackID id_no_arg = tracker.join( no_arg, NoArgument() );
107
SignalTracker::TrackID id_one_arg = tracker.join( one_arg, OneArgument() );
109
// two outputs each from these two signals
113
// stop tracking id_one_arg, which should keep the slot after this scope,
114
// the id_no_arg connection should be destroyed after this.
115
tracker.leave( id_one_arg );
116
cout << "---- tracker end ----" << endl;
119
// now we should have one output from no_arg and two outputs from one_arg
126
no_arg.connect(MemFun(obj, &FunctionClass::print));
129
string takeThis("Take this");
130
Signal<void, string&> ref_arg;
131
ref_arg.connect(MemFun(obj, &FunctionClass::takeIt));
132
ref_arg.emit( takeThis );
135
two_args.connect(MemFun(obj, &FunctionClass::showMessage));
136
two_args.emit(10, "This is a message");
139
three_args.connect(MemFun(obj, &FunctionClass::threeArgs));
140
three_args.emit(9, "nine", 3.141592);
142
// Test ignore signals
144
cout << "----------- Testing ignoring arguments for signal." << endl;
145
using FbTk::MemFunIgnoreArgs;
146
// Create a signal that emits with three arguments, and connect
147
// sinks that takes less than three arguments.
148
Signal<void, string, string, float> more_args;
149
more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::print));
150
more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::takeIt));
151
more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::showMessage2));
152
more_args.emit("This should be visible for takeIt(string)",
153
"Visible to the two args function.",
157
// Test relay new signals to old
159
cout << "---------- Testing relay of signals" << endl;
160
struct Observer: public FbTk::Observer {
161
void update(FbTk::Subject* subj) {
162
cout << "Observer called." << endl;
165
// setup old subject->observer listening
166
FbTk::Subject destination;
168
destination.attach(&obs);
169
// create a new signal and relay it to the
171
FbTk::Signal<void, string> source;
172
FbTk::relaySignal(source, destination);
173
// the new signal should now make the old
174
// subject notify its observers
175
source.emit("hello world");
178
// Test argument selector
180
using namespace FbTk;
181
Signal<void, int, string, float> source;
184
source.connect(MemFunSelectArg0(printer, &Printer::printInt));
185
source.connect(MemFunSelectArg1(printer, &Printer::printString));
186
source.connect(MemFunSelectArg2(printer, &Printer::printFloat));
188
source.emit(10, "hello", 3.141592);
190
Signal<void, string, int> source2;
191
source2.connect(MemFunSelectArg0(printer, &Printer::printString));
192
source2.connect(MemFunSelectArg1(printer, &Printer::printInt));
193
source2.emit("world", 37);