CodeWriter: Write [Diagnostics] attribute
[vala-lang.git] / tests / delegates / delegates.vala
blobe80fd3fe83f73a7b33791896289d51ca84416771
1 using GLib;
3 public enum ParameterEnum { FOO, BAR }
5 public static delegate void Maman.VoidCallback ();
7 public static delegate int Maman.ActionCallback ();
9 public delegate void Maman.InstanceCallback (int i);
10 public delegate Maman.SelfCallback Maman.SelfCallback (Maman.SelfCallback scb);
12 public delegate ParameterEnum Maman.EnumDelegate (ParameterEnum pe);
14 struct Maman.DelegateStruct {
15 public VoidCallback callback;
18 interface Maman.Foo : Object {
19 public abstract void foo_method (int i);
22 class Maman.Bar : Object, Foo {
23 const DelegateStruct const_delegate_struct = { do_void_action };
25 public Bar () {
28 static void do_void_action () {
29 stdout.printf (" 2");
32 static int do_action () {
33 return 4;
36 void do_instance_action (int i) {
37 assert (i == 42);
39 stdout.printf (" 6");
42 static void call_instance_delegate (InstanceCallback instance_cb) {
43 instance_cb (42);
46 void assign_instance_delegate (out InstanceCallback instance_cb) {
47 instance_cb = foo_method;
50 static void test_function_pointers () {
51 stdout.printf ("testing function pointers:");
52 var table = new HashTable<string, Bar>.full (str_hash, str_equal, g_free, Object.unref);
53 stdout.printf (" 1");
55 table.insert ("foo", new Bar ());
56 stdout.printf (" 2");
58 var bar = table.lookup ("foo");
59 stdout.printf (" 3\n");
62 public void foo_method (int i) {
65 static void test_delegates_interface_method () {
66 // http://bugzilla.gnome.org/show_bug.cgi?id=518109
67 var bar = new Bar ();
68 call_instance_delegate (bar.foo_method);
71 public static int main () {
72 stdout.printf ("Delegate Test: 1");
74 VoidCallback void_cb = do_void_action;
76 void_cb ();
78 stdout.printf (" 3");
80 ActionCallback cb = do_action;
82 stdout.printf (" %d", cb ());
84 stdout.printf (" 5");
86 var bar = new Bar ();
88 InstanceCallback instance_cb = bar.do_instance_action;
89 call_instance_delegate (instance_cb);
91 bar.assign_instance_delegate (out instance_cb);
92 call_instance_delegate (instance_cb);
94 stdout.printf (" 7\n");
96 test_function_pointers ();
98 test_delegates_interface_method ();
100 return 0;
104 void main () {
105 Maman.Bar.main ();