import source from 1.3.40
[external/swig.git] / Examples / octave / callback / runme.m
1 # file: runme.m
2
3 # This file illustrates the cross language polymorphism using directors.
4
5 example 
6
7 OctCallback=@() subclass(example.Callback(), \
8                          'run',@(self) printf("OctCallback.run()\n"));
9
10 # Create an Caller instance
11
12 caller = example.Caller();
13
14 # Add a simple C++ callback (caller owns the callback, so
15 # we disown it first)
16
17 printf("Adding and calling a normal C++ callback\n");
18 printf("----------------------------------------\n");
19
20 callback = example.Callback().__disown();
21 caller.setCallback(callback);
22 caller.call();
23 caller.delCallback();
24
25 printf("Adding and calling a Octave callback\n");
26 printf("------------------------------------\n");
27
28 # Add a Octave callback (caller owns the callback, so we
29 # disown it first by calling __disown).
30
31 caller.setCallback(OctCallback().__disown())
32 caller.call();
33 caller.delCallback();
34
35 printf("Adding and calling another Octave callback\n");
36 printf("------------------------------------------\n");
37
38 # Let's do the same but use the weak reference this time.
39
40 callback = OctCallback().__disown();
41 caller.setCallback(callback);
42 caller.call();
43 caller.delCallback();
44
45 # careful-- using callback here may cause problems; octave_swig_type still
46 # exists, but is holding a destroyed object (the C++ example.Callback).
47 # to manually drop the octave-side reference, you can use
48 clear callback;
49
50 # Let's call them directly now
51
52 printf("Calling Octave and C++ callbacks directly\n");
53 printf("------------------------------------------\n");
54
55 a = OctCallback();
56 a.run();
57 a.Callback.run();
58
59
60 # All done.
61
62 printf("octave exit\n");
63