update for 0.4.0 release
[vala-lang.git] / tests / delegates.vala
blob6ff52c83d149e16ce41c1a8a99dee1024f84230b
1 using GLib;
3 public static delegate void Maman.VoidCallback ();
5 public static delegate int Maman.ActionCallback ();
7 public delegate void Maman.InstanceCallback (int i);
9 struct Maman.DelegateStruct {
10 public VoidCallback callback;
13 interface Maman.Foo : Object {
14 public abstract void foo_method (int i);
17 class Maman.Bar : Object, Foo {
18 const DelegateStruct const_delegate_struct = { do_void_action };
20 public Bar () {
23 static void do_void_action () {
24 stdout.printf (" 2");
27 static int do_action () {
28 return 4;
31 void do_instance_action (int i) {
32 assert (i == 42);
34 stdout.printf (" 6");
37 static void call_instance_delegate (InstanceCallback instance_cb) {
38 instance_cb (42);
41 static void test_function_pointers () {
42 stdout.printf ("testing function pointers:");
43 var table = new HashTable<string, Bar>.full (str_hash, str_equal, g_free, Object.unref);
44 stdout.printf (" 1");
46 table.insert ("foo", new Bar ());
47 stdout.printf (" 2");
49 var bar = table.lookup ("foo");
50 stdout.printf (" 3\n");
53 public void foo_method (int i) {
56 static void test_delegates_interface_method () {
57 // http://bugzilla.gnome.org/show_bug.cgi?id=518109
58 var bar = new Bar ();
59 call_instance_delegate (bar.foo_method);
62 static int main (string[] args) {
63 stdout.printf ("Delegate Test: 1");
65 VoidCallback void_cb = do_void_action;
67 void_cb ();
69 stdout.printf (" 3");
71 ActionCallback cb = do_action;
73 stdout.printf (" %d", cb ());
75 stdout.printf (" 5");
77 var bar = new Bar ();
79 InstanceCallback instance_cb = bar.do_instance_action;
80 call_instance_delegate (instance_cb);
82 stdout.printf (" 7\n");
84 test_function_pointers ();
86 test_delegates_interface_method ();
88 return 0;