summaryrefslogtreecommitdiff
path: root/src/tests/signalcall.cpp
blob: 8b4b1746c0240c95b37f25989c1d085d260ef605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <bu/signals.h>
#include <bu/sio.h>
#include <bu/randomcmwc.h>

#include <time.h>

template<typename ret>
class SignalCallable
{
public:
    SignalCallable()
    {
    }

    virtual ~SignalCallable()
    {
    }

    virtual ret call()=0;
};

template<typename ret>
class SignalCall0 : public SignalCallable<ret>
{
public:
    SignalCall0( const Bu::Signal0<ret> &sRef ) :
        sRef( sRef )
    {
    }

    virtual ~SignalCall0()
    {
    }

    virtual ret call()
    {
        return sRef();
    }

private:
    Bu::Signal0<ret> sRef;
};

template<typename ret, typename p1t>
class SignalCall1 : public SignalCallable<ret>
{
public:
    SignalCall1( const Bu::Signal1<ret, p1t> &sRef, const p1t &p1 ) :
        sRef( sRef ), p1( p1 )
    {
    }

    virtual ~SignalCall1()
    {
    }

    virtual ret call()
    {
        return sRef( p1 );
    }

private:
    Bu::Signal1<ret, p1t> sRef;
    p1t p1;
};

template<typename ret, typename p1t, typename p2t>
class SignalCall2 : public SignalCallable<ret>
{
public:
    SignalCall2( const Bu::Signal2<ret, p1t, p2t> &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<ret, p1t, p2t> sRef;
    p1t p1;
    p2t p2;
};

template<typename ret, typename p1t, typename p2t>
class DeferredSignal2 : public Bu::Signal2<ret, p1t, p2t>
{
    typedef Bu::Signal2<ret, p1t, p2t> Parent;
public:
    DeferredSignal2() : Parent() { }
    DeferredSignal2( Bu::_Slot2<ret, p1t, p2t> *pCb ) : Parent( pCb ) { }
    DeferredSignal2( const DeferredSignal2<ret, p1t, p2t> &rSrc ) :
        Parent( (const Parent &)rSrc ) { }
    virtual ~DeferredSignal2() { }

    void operator()( p1t p1, p2t p2 )
    {
        lCalls.enqueue( SignalCall2<ret, p1t, p2t>( (Parent &)*this, p1, p2 ) );
    }

    DeferredSignal2<ret, p1t, p2t> &operator=( const Bu::Signal2<ret, p1t, p2t> &rhs )
    {
        ((Parent &)*this) = rhs;
        return *this;
    }

    bool operator==( const Bu::Signal2<ret, p1t, p2t> &rhs ) const
    {
        return ((Parent &)*this) == rhs;
    }

    bool hasNext() const
    {
        return !lCalls.isEmpty();
    }

    ret callNext()
    {
        return lCalls.dequeue().call();
    }

private:
    Bu::List< SignalCall2<ret, p1t, p2t> > 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<SignalCallable<int> * > CallList;
    CallList calls;

    calls.append( new SignalCall2<int, int, int>( Bu::slot(&area), 5, 10 ) );
    calls.append( new SignalCall2<int, int, int>( Bu::slot(&diff), 5, 15 ) );
    calls.append( new SignalCall2<int, int, int>( Bu::slot(&thing), 10, 5 ) );
    calls.append( new SignalCall1<int, int>( Bu::slot(&square), 12 ) );
    calls.append( new SignalCall0<int>( 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<int, int, int> 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;
}