File: runme.m

package info (click to toggle)
swig2.0 2.0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,260 kB
  • sloc: cpp: 49,839; ansic: 25,403; java: 8,412; python: 6,579; cs: 5,773; yacc: 5,158; makefile: 5,098; sh: 4,806; ruby: 3,673; perl: 2,384; lisp: 1,741; php: 1,701; tcl: 971; ml: 619; xml: 85
file content (74 lines) | stat: -rw-r--r-- 3,002 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# file: runme.m

# This file illustrates the cross language polymorphism using directors.

example 


# CEO class, which overrides Employee::getPosition().

CEO=@(name) subclass(example.Manager(name),'getPosition',@(self) "CEO");

# Create an instance of our employee extension class, CEO. The calls to
# getName() and getPosition() are standard, the call to getTitle() uses
# the director wrappers to call CEO.getPosition. e = CEO("Alice")

e = CEO("Alice");
printf("%s is a %s\n",e.getName(),e.getPosition());
printf("Just call her \"%s\"\n",e.getTitle());
printf("----------------------\n");


# Create a new EmployeeList instance.  This class does not have a C++
# director wrapper, but can be used freely with other classes that do.

list = example.EmployeeList();

# EmployeeList owns its items, so we must surrender ownership of objects
# we add. This involves first calling the __disown__ method to tell the
# C++ director to start reference counting. We reassign the resulting
# weakref.proxy to e so that no hard references remain. This can also be
# done when the object is constructed, as in: e =
# CEO("Alice").__disown()

e = e.__disown();
list.addEmployee(e);
printf("----------------------\n");

# Now we access the first four items in list (three are C++ objects that
# EmployeeList's constructor adds, the last is our CEO). The virtual
# methods of all these instances are treated the same. For items 0, 1, and
# 2, both all methods resolve in C++. For item 3, our CEO, getTitle calls
# getPosition which resolves in Octave. The call to getPosition is
# slightly different, however, from the e.getPosition() call above, since
# now the object reference has been "laundered" by passing through
# EmployeeList as an Employee*. Previously, Octave resolved the call
# immediately in CEO, but now Octave thinks the object is an instance of
# class Employee (actually EmployeePtr). So the call passes through the
# Employee proxy class and on to the C wrappers and C++ director,
# eventually ending up back at the CEO implementation of getPosition().
# The call to getTitle() for item 3 runs the C++ Employee::getTitle()
# method, which in turn calls getPosition(). This virtual method call
# passes down through the C++ director class to the Octave implementation
# in CEO. All this routing takes place transparently.

printf("(position, title) for items 0-3:\n");
for i=0:3,
  printf("  %s, \"%s\"\n",list.get_item(i).getPosition(), list.get_item(i).getTitle());
endfor
printf("----------------------\n");

# Time to delete the EmployeeList, which will delete all the Employee*
# items it contains. The last item is our CEO, which gets destroyed as its
# reference count goes to zero. The Octave destructor runs, and is still
# able to call self.getName() since the underlying C++ object still
# exists. After this destructor runs the remaining C++ destructors run as
# usual to destroy the object.

clear list;
printf("----------------------\n");

# All done.

printf("octave exit\n");