blob: 504ff0824a61ff164f11f3ea253721a6ab731fa5 (
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
|
Program: test
using GLib;
public static delegate void Maman.VoidCallback ();
public static delegate int Maman.ActionCallback ();
public delegate void Maman.InstanceCallback (int i);
struct Maman.DelegateStruct {
public VoidCallback callback;
}
interface Maman.Foo : Object {
public abstract void foo_method (int i);
}
class Maman.Bar : Object, Foo {
const DelegateStruct const_delegate_struct = { do_void_action };
public Bar () {
}
static void do_void_action () {
stdout.printf (" 2");
}
static int do_action () {
return 4;
}
void do_instance_action (int i) {
assert (i == 42);
stdout.printf (" 6");
}
static void call_instance_delegate (InstanceCallback instance_cb) {
instance_cb (42);
}
static void test_function_pointers () {
stdout.printf ("testing function pointers:");
var table = new HashTable<string, Bar>.full (str_hash, str_equal, g_free, Object.unref);
stdout.printf (" 1");
table.insert ("foo", new Bar ());
stdout.printf (" 2");
var bar = table.lookup ("foo");
stdout.printf (" 3\n");
}
public void foo_method (int i) {
}
static void test_delegates_interface_method () {
// http://bugzilla.gnome.org/show_bug.cgi?id=518109
var bar = new Bar ();
call_instance_delegate (bar.foo_method);
}
static int main (string[] args) {
stdout.printf ("Delegate Test: 1");
VoidCallback void_cb = do_void_action;
void_cb ();
stdout.printf (" 3");
ActionCallback cb = do_action;
stdout.printf (" %d", cb ());
stdout.printf (" 5");
var bar = new Bar ();
InstanceCallback instance_cb = bar.do_instance_action;
call_instance_delegate (instance_cb);
stdout.printf (" 7\n");
test_function_pointers ();
test_delegates_interface_method ();
return 0;
}
}
|