aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorHenrik Kinnunen <fluxgen@fluxbox.org>2008-09-18 20:24:35 (GMT)
committerHenrik Kinnunen <fluxgen@fluxbox.org>2008-09-18 20:24:35 (GMT)
commite4d4717703b365bc14f189bf36b3edb1e4430b90 (patch)
tree1e678e6d0569714ab8c78fa0900c18ee1bb0f10a /src/tests
parent8e97963e4211963f960c52c8a8f4bf5cd135ad2f (diff)
downloadfluxbox-e4d4717703b365bc14f189bf36b3edb1e4430b90.zip
fluxbox-e4d4717703b365bc14f189bf36b3edb1e4430b90.tar.bz2
Added new Signal/Slot system in FbTk
This is suppose to replace the obsolete Subject/Observer classes. See the src/tests/testSignals.cc for basic usage.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/Makefile6
-rw-r--r--src/tests/testSignals.cc121
2 files changed, 126 insertions, 1 deletions
diff --git a/src/tests/Makefile b/src/tests/Makefile
index 8d1c482..668126e 100644
--- a/src/tests/Makefile
+++ b/src/tests/Makefile
@@ -7,7 +7,8 @@ COMPILEFILE=$(CXX) -c $(CXXFLAGS)
7FONT_OBJ = ../FbTk/libFbTk.a 7FONT_OBJ = ../FbTk/libFbTk.a
8COMPILE = ${CXX} ${CXXFLAGS} ${XLIBS} 8COMPILE = ${CXX} ${CXXFLAGS} ${XLIBS}
9 9
10all: testMenu testFont testTexture movetest 10all: testMenu testFont testTexture movetest testSignals
11
11 12
12.cc.o: 13.cc.o:
13 $(CXX) -c $(CXXFLAGS) $< 14 $(CXX) -c $(CXXFLAGS) $<
@@ -15,6 +16,9 @@ all: testMenu testFont testTexture movetest
15glxtest: ../FbTk/App.hh glxtest.cc 16glxtest: ../FbTk/App.hh glxtest.cc
16 ${CXX} glxtest.cc ${CXXFLAGS} ${XLIBS} -lGL -lGLU -lXpm -o glxtest 17 ${CXX} glxtest.cc ${CXXFLAGS} ${XLIBS} -lGL -lGLU -lXpm -o glxtest
17 18
19testSignals: testSignals.o ../FbTk/Signal.hh ../FbTk/MemFun.hh
20 $(CXX) $(LIBS) testSignals.o -o testSignals
21
18testStringUtil: StringUtiltest.o 22testStringUtil: StringUtiltest.o
19 $(CXX) $(LIBS) StringUtiltest.o ../FbTk/libFbTk.a -o testStringUtil 23 $(CXX) $(LIBS) StringUtiltest.o ../FbTk/libFbTk.a -o testStringUtil
20 24
diff --git a/src/tests/testSignals.cc b/src/tests/testSignals.cc
new file mode 100644
index 0000000..86096bf
--- /dev/null
+++ b/src/tests/testSignals.cc
@@ -0,0 +1,121 @@
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 void operator ()( int value, const string& message ) {
25 cout << "Two arguments, (1) = " << value << ", (2) = " << message << endl;
26 }
27};
28
29struct ThreeArguments {
30 void operator ()( int value, const string& message, double value2 ) {
31 cout << "Two arguments, (1) = " << value << ", (2) = " << message
32 << ", (3) = " << value2 << endl;
33 }
34};
35
36struct FunctionClass {
37 FunctionClass() {
38 cout << "FunctionClass created." << endl;
39 }
40 ~FunctionClass() {
41 cout << "FunctionClass deleted." << endl;
42 }
43 void print() {
44 cout << "Printing." << endl;
45 }
46
47 void takeIt( string& str ) {
48 cout << "takeIt( " << str << ")" << endl;
49 }
50
51 void showMessage( int value, const string& message ) {
52 cout << "(" << value << "): " << message << endl;
53 }
54 void threeArgs( int value, const string& str, double pi ) {
55 cout << "(" << value << "): " << str << ", pi = " << pi << endl;
56 }
57};
58
59int main() {
60 using FbTk::Signal;
61 using FbTk::SignalTracker;
62
63 Signal<void> no_arg;
64 no_arg.connect( NoArgument() );
65
66 Signal<void, int> one_arg;
67 one_arg.connect( OneArgument() );
68
69 Signal<void, int, const string&> two_args;
70 two_args.connect( TwoArguments() );
71
72 Signal<void, int, const string&, double> three_args;
73 three_args.connect( ThreeArguments() );
74
75 // emit test
76 no_arg.emit();
77 one_arg.emit( 10 );
78 two_args.emit( 10, "Message" );
79 three_args.emit( 10, "Three", 3.141592 );
80
81 // test signal tracker
82 {
83 cout << "---- tracker ----" << endl;
84 SignalTracker tracker;
85 // setup two new slots and track them
86 SignalTracker::TrackID id_no_arg = tracker.join( no_arg, NoArgument() );
87 SignalTracker::TrackID id_one_arg = tracker.join( one_arg, OneArgument() );
88
89 // two outputs each from these two signals
90 no_arg.emit();
91 one_arg.emit( 31 );
92
93 // stop tracking id_one_arg, which should keep the slot after this scope,
94 // the id_no_arg connection should be destroyed after this.
95 tracker.leave( id_one_arg );
96 cout << "---- tracker end ----" << endl;
97 }
98
99 // now we should have one output from no_arg and two outputs from one_arg
100 no_arg.emit();
101 one_arg.emit( 2 );
102
103 using FbTk::MemFun;
104 FunctionClass obj;
105 no_arg.clear();
106 no_arg.connect(MemFun(obj, &FunctionClass::print));
107 no_arg.emit();
108
109 string takeThis("Take this");
110 Signal<void, string&> ref_arg;
111 ref_arg.connect(MemFun(obj, &FunctionClass::takeIt));
112 ref_arg.emit( takeThis );
113
114 two_args.clear();
115 two_args.connect(MemFun(obj, &FunctionClass::showMessage));
116 two_args.emit(10, "This is a message");
117
118 three_args.clear();
119 three_args.connect(MemFun(obj, &FunctionClass::threeArgs));
120 three_args.emit(9, "nine", 3.141592);
121}