From 123434f227a963cf26f9637136bf759a824f930a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 18 Feb 2013 13:13:28 +0000 Subject: Added super-experimental proof-of-concept for SignalCall and DeferredSignal --- src/tests/signalcall.cpp | 200 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 src/tests/signalcall.cpp 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 @@ +#include +#include +#include + +#include + +template +class SignalCallable +{ +public: + SignalCallable() + { + } + + virtual ~SignalCallable() + { + } + + virtual ret call()=0; +}; + +template +class SignalCall0 : public SignalCallable +{ +public: + SignalCall0( const Bu::Signal0 &sRef ) : + sRef( sRef ) + { + } + + virtual ~SignalCall0() + { + } + + virtual ret call() + { + return sRef(); + } + +private: + Bu::Signal0 sRef; +}; + +template +class SignalCall1 : public SignalCallable +{ +public: + SignalCall1( const Bu::Signal1 &sRef, const p1t &p1 ) : + sRef( sRef ), p1( p1 ) + { + } + + virtual ~SignalCall1() + { + } + + virtual ret call() + { + return sRef( p1 ); + } + +private: + Bu::Signal1 sRef; + p1t p1; +}; + +template +class SignalCall2 : public SignalCallable +{ +public: + SignalCall2( const Bu::Signal2 &sRef, const p1t &p1, const p2t &p2 ) : + sRef( sRef ), p1( p1 ), p2( p2 ) + { + } + + virtual ~SignalCall2() + { + } + + virtual ret call() + { + return sRef( p1, p2 ); + } + +private: + Bu::Signal2 sRef; + p1t p1; + p2t p2; +}; + +template +class DeferredSignal2 : public Bu::Signal2 +{ + typedef Bu::Signal2 Parent; +public: + DeferredSignal2() : Parent() { } + DeferredSignal2( Bu::_Slot2 *pCb ) : Parent( pCb ) { } + DeferredSignal2( const DeferredSignal2 &rSrc ) : + Parent( (const Parent &)rSrc ) { } + virtual ~DeferredSignal2() { } + + void operator()( p1t p1, p2t p2 ) + { + lCalls.enqueue( SignalCall2( (Parent &)*this, p1, p2 ) ); + } + + DeferredSignal2 &operator=( const Bu::Signal2 &rhs ) + { + ((Parent &)*this) = rhs; + return *this; + } + + bool operator==( const Bu::Signal2 &rhs ) const + { + return ((Parent &)*this) == rhs; + } + + bool hasNext() const + { + return !lCalls.isEmpty(); + } + + ret callNext() + { + return lCalls.dequeue().call(); + } + +private: + Bu::List< SignalCall2 > lCalls; +}; + + +// +// Slots +// +int area( int x, int y ) +{ + return x*y; +} + +int diff( int x, int y ) +{ + return x-y; +} + +int thing( int x, int y ) +{ + return x*x-y*y; +} + +int square( int x ) +{ + return x*x; +} + +int rand() +{ + Bu::RandomCmwc c; + c.seed( time( NULL ) ); + return c.rand(); +} + +int main() +{ + // Direct use of SignalCall objects + Bu::println("Testing SignalCall classes:"); + + typedef Bu::List * > CallList; + CallList calls; + + calls.append( new SignalCall2( Bu::slot(&area), 5, 10 ) ); + calls.append( new SignalCall2( Bu::slot(&diff), 5, 15 ) ); + calls.append( new SignalCall2( Bu::slot(&thing), 10, 5 ) ); + calls.append( new SignalCall1( Bu::slot(&square), 12 ) ); + calls.append( new SignalCall0( Bu::slot(&rand)) ); + + for( CallList::iterator i = calls.begin(); i; i++ ) + { + Bu::println(" - Result: %1").arg( (*i)->call() ); + } + + // Deferred Signals: + Bu::println(""); + Bu::println("Testing DeferredSignals:"); + + DeferredSignal2 sigArea; + sigArea = Bu::slot(&area); + + sigArea( 5, 5 ); + sigArea( 10, 10 ); + sigArea( 15, 15 ); + + while( sigArea.hasNext() ) + { + Bu::println(" - Result: %1").arg( sigArea.callNext() ); + } + + return 0; +} + -- cgit v1.2.3