File: callbacktest.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (56 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download | duplicates (3)
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
#include "wvcallback.h"
#include <stdio.h>

struct A
{
    int x, y;
    A(int _x = 0, int _y = 0)
        { x = _x; y = _y; }
    
    A add(const A &a)
        { return A(x+a.x, y+a.y); }
};

typedef WvCallback<A, const A &, void *> ACallback;
typedef WvCallback<A, const A &> A2Callback;
typedef WvCallback<A, void *> A3Callback;


static A bunk(const A &a, void *userdata)
{
    int incr = (int)userdata;
    return A(a.x+incr, a.y+incr*2);
}


// one-parameter version of bunk()
static A bunk1(const A &a)
{
    return bunk(a, (void *)1);
}


static void print_a(const A &a)
{
    printf("result: %d/%d\n", a.x, a.y);
}


int main()
{
    A a(1000, 2000);
    
    ACallback c0(bunk);
    A2Callback c1(bunk1);
    // FIXME: I am broken. Please show this to somebody
    // who can fix.... can fix.... can fix.....
    // A3Callback c2(WvBoundCallback<A3Callback, const A &>(bunk, a));
    A2Callback c3(&a, &A::add);
    
    print_a(c0(a, (void *)5));
    print_a(c1(a));
    // print_a(c2((void *)2));
    print_a(c3(a));
    
    return 0;
}