diff options
author | Mike Buland <eichlan@xagasoft.com> | 2013-02-18 13:13:28 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2013-02-18 13:13:28 +0000 |
commit | 123434f227a963cf26f9637136bf759a824f930a (patch) | |
tree | df195b1fe23e8c5beb946a52c661d5b98b8c9598 | |
parent | c79fd5604d9cc252469bd7544fbd3eecc593b3e1 (diff) | |
download | libbu++-123434f227a963cf26f9637136bf759a824f930a.tar.gz libbu++-123434f227a963cf26f9637136bf759a824f930a.tar.bz2 libbu++-123434f227a963cf26f9637136bf759a824f930a.tar.xz libbu++-123434f227a963cf26f9637136bf759a824f930a.zip |
Added super-experimental proof-of-concept for SignalCall and DeferredSignal
Diffstat (limited to '')
-rw-r--r-- | src/tests/signalcall.cpp | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/src/tests/signalcall.cpp b/src/tests/signalcall.cpp new file mode 100644 index 0000000..8b4b174 --- /dev/null +++ b/src/tests/signalcall.cpp | |||
@@ -0,0 +1,200 @@ | |||
1 | #include <bu/signals.h> | ||
2 | #include <bu/sio.h> | ||
3 | #include <bu/randomcmwc.h> | ||
4 | |||
5 | #include <time.h> | ||
6 | |||
7 | template<typename ret> | ||
8 | class SignalCallable | ||
9 | { | ||
10 | public: | ||
11 | SignalCallable() | ||
12 | { | ||
13 | } | ||
14 | |||
15 | virtual ~SignalCallable() | ||
16 | { | ||
17 | } | ||
18 | |||
19 | virtual ret call()=0; | ||
20 | }; | ||
21 | |||
22 | template<typename ret> | ||
23 | class SignalCall0 : public SignalCallable<ret> | ||
24 | { | ||
25 | public: | ||
26 | SignalCall0( const Bu::Signal0<ret> &sRef ) : | ||
27 | sRef( sRef ) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | virtual ~SignalCall0() | ||
32 | { | ||
33 | } | ||
34 | |||
35 | virtual ret call() | ||
36 | { | ||
37 | return sRef(); | ||
38 | } | ||
39 | |||
40 | private: | ||
41 | Bu::Signal0<ret> sRef; | ||
42 | }; | ||
43 | |||
44 | template<typename ret, typename p1t> | ||
45 | class SignalCall1 : public SignalCallable<ret> | ||
46 | { | ||
47 | public: | ||
48 | SignalCall1( const Bu::Signal1<ret, p1t> &sRef, const p1t &p1 ) : | ||
49 | sRef( sRef ), p1( p1 ) | ||
50 | { | ||
51 | } | ||
52 | |||
53 | virtual ~SignalCall1() | ||
54 | { | ||
55 | } | ||
56 | |||
57 | virtual ret call() | ||
58 | { | ||
59 | return sRef( p1 ); | ||
60 | } | ||
61 | |||
62 | private: | ||
63 | Bu::Signal1<ret, p1t> sRef; | ||
64 | p1t p1; | ||
65 | }; | ||
66 | |||
67 | template<typename ret, typename p1t, typename p2t> | ||
68 | class SignalCall2 : public SignalCallable<ret> | ||
69 | { | ||
70 | public: | ||
71 | SignalCall2( const Bu::Signal2<ret, p1t, p2t> &sRef, const p1t &p1, const p2t &p2 ) : | ||
72 | sRef( sRef ), p1( p1 ), p2( p2 ) | ||
73 | { | ||
74 | } | ||
75 | |||
76 | virtual ~SignalCall2() | ||
77 | { | ||
78 | } | ||
79 | |||
80 | virtual ret call() | ||
81 | { | ||
82 | return sRef( p1, p2 ); | ||
83 | } | ||
84 | |||
85 | private: | ||
86 | Bu::Signal2<ret, p1t, p2t> sRef; | ||
87 | p1t p1; | ||
88 | p2t p2; | ||
89 | }; | ||
90 | |||
91 | template<typename ret, typename p1t, typename p2t> | ||
92 | class DeferredSignal2 : public Bu::Signal2<ret, p1t, p2t> | ||
93 | { | ||
94 | typedef Bu::Signal2<ret, p1t, p2t> Parent; | ||
95 | public: | ||
96 | DeferredSignal2() : Parent() { } | ||
97 | DeferredSignal2( Bu::_Slot2<ret, p1t, p2t> *pCb ) : Parent( pCb ) { } | ||
98 | DeferredSignal2( const DeferredSignal2<ret, p1t, p2t> &rSrc ) : | ||
99 | Parent( (const Parent &)rSrc ) { } | ||
100 | virtual ~DeferredSignal2() { } | ||
101 | |||
102 | void operator()( p1t p1, p2t p2 ) | ||
103 | { | ||
104 | lCalls.enqueue( SignalCall2<ret, p1t, p2t>( (Parent &)*this, p1, p2 ) ); | ||
105 | } | ||
106 | |||
107 | DeferredSignal2<ret, p1t, p2t> &operator=( const Bu::Signal2<ret, p1t, p2t> &rhs ) | ||
108 | { | ||
109 | ((Parent &)*this) = rhs; | ||
110 | return *this; | ||
111 | } | ||
112 | |||
113 | bool operator==( const Bu::Signal2<ret, p1t, p2t> &rhs ) const | ||
114 | { | ||
115 | return ((Parent &)*this) == rhs; | ||
116 | } | ||
117 | |||
118 | bool hasNext() const | ||
119 | { | ||
120 | return !lCalls.isEmpty(); | ||
121 | } | ||
122 | |||
123 | ret callNext() | ||
124 | { | ||
125 | return lCalls.dequeue().call(); | ||
126 | } | ||
127 | |||
128 | private: | ||
129 | Bu::List< SignalCall2<ret, p1t, p2t> > lCalls; | ||
130 | }; | ||
131 | |||
132 | |||
133 | // | ||
134 | // Slots | ||
135 | // | ||
136 | int area( int x, int y ) | ||
137 | { | ||
138 | return x*y; | ||
139 | } | ||
140 | |||
141 | int diff( int x, int y ) | ||
142 | { | ||
143 | return x-y; | ||
144 | } | ||
145 | |||
146 | int thing( int x, int y ) | ||
147 | { | ||
148 | return x*x-y*y; | ||
149 | } | ||
150 | |||
151 | int square( int x ) | ||
152 | { | ||
153 | return x*x; | ||
154 | } | ||
155 | |||
156 | int rand() | ||
157 | { | ||
158 | Bu::RandomCmwc c; | ||
159 | c.seed( time( NULL ) ); | ||
160 | return c.rand(); | ||
161 | } | ||
162 | |||
163 | int main() | ||
164 | { | ||
165 | // Direct use of SignalCall objects | ||
166 | Bu::println("Testing SignalCall classes:"); | ||
167 | |||
168 | typedef Bu::List<SignalCallable<int> * > CallList; | ||
169 | CallList calls; | ||
170 | |||
171 | calls.append( new SignalCall2<int, int, int>( Bu::slot(&area), 5, 10 ) ); | ||
172 | calls.append( new SignalCall2<int, int, int>( Bu::slot(&diff), 5, 15 ) ); | ||
173 | calls.append( new SignalCall2<int, int, int>( Bu::slot(&thing), 10, 5 ) ); | ||
174 | calls.append( new SignalCall1<int, int>( Bu::slot(&square), 12 ) ); | ||
175 | calls.append( new SignalCall0<int>( Bu::slot(&rand)) ); | ||
176 | |||
177 | for( CallList::iterator i = calls.begin(); i; i++ ) | ||
178 | { | ||
179 | Bu::println(" - Result: %1").arg( (*i)->call() ); | ||
180 | } | ||
181 | |||
182 | // Deferred Signals: | ||
183 | Bu::println(""); | ||
184 | Bu::println("Testing DeferredSignals:"); | ||
185 | |||
186 | DeferredSignal2<int, int, int> sigArea; | ||
187 | sigArea = Bu::slot(&area); | ||
188 | |||
189 | sigArea( 5, 5 ); | ||
190 | sigArea( 10, 10 ); | ||
191 | sigArea( 15, 15 ); | ||
192 | |||
193 | while( sigArea.hasNext() ) | ||
194 | { | ||
195 | Bu::println(" - Result: %1").arg( sigArea.callNext() ); | ||
196 | } | ||
197 | |||
198 | return 0; | ||
199 | } | ||
200 | |||