Add Android runtime example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13832 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
72fc742f88
commit
9e34a0d376
2 changed files with 58 additions and 44 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
# see top-level Makefile.in
|
# see top-level Makefile.in
|
||||||
class
|
class
|
||||||
|
extend
|
||||||
simple
|
simple
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,22 @@ import android.widget.TextView;
|
||||||
import android.widget.ScrollView;
|
import android.widget.ScrollView;
|
||||||
import android.text.method.ScrollingMovementMethod;
|
import android.text.method.ScrollingMovementMethod;
|
||||||
|
|
||||||
|
|
||||||
|
// CEO class, which overrides Employee::getPosition().
|
||||||
|
class CEO extends Manager {
|
||||||
|
public CEO(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
public String getPosition() {
|
||||||
|
return "CEO";
|
||||||
|
}
|
||||||
|
// Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory.
|
||||||
|
public void disownMemory() {
|
||||||
|
swigCMemOwn = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public class SwigExtend extends Activity
|
public class SwigExtend extends Activity
|
||||||
{
|
{
|
||||||
TextView outputText = null;
|
TextView outputText = null;
|
||||||
|
|
@ -44,61 +60,58 @@ public class SwigExtend extends Activity
|
||||||
/** Calls into C/C++ code */
|
/** Calls into C/C++ code */
|
||||||
public void nativeCall()
|
public void nativeCall()
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
// ----- Object creation -----
|
|
||||||
|
|
||||||
outputText.append( "Creating some objects:\n" );
|
// Create an instance of CEO, a class derived from the Java proxy of the
|
||||||
Circle c = new Circle(10);
|
// underlying C++ class. The calls to getName() and getPosition() are standard,
|
||||||
outputText.append( " Created circle " + c + "\n");
|
// the call to getTitle() uses the director wrappers to call CEO.getPosition().
|
||||||
Square s = new Square(10);
|
|
||||||
outputText.append( " Created square " + s + "\n");
|
|
||||||
|
|
||||||
// ----- Access a static member -----
|
CEO e = new CEO("Alice");
|
||||||
|
outputText.append( e.getName() + " is a " + e.getPosition() + "\n");
|
||||||
|
outputText.append( "Just call her \"" + e.getTitle() + "\"\n" );
|
||||||
|
outputText.append( "----------------------\n" );
|
||||||
|
|
||||||
outputText.append( "\nA total of " + Shape.getNshapes() + " shapes were created\n" );
|
|
||||||
|
|
||||||
// ----- Member data access -----
|
// Create a new EmployeeList instance. This class does not have a C++
|
||||||
|
// director wrapper, but can be used freely with other classes that do.
|
||||||
|
|
||||||
// Notice how we can do this using functions specific to
|
EmployeeList list = new EmployeeList();
|
||||||
// the 'Circle' class.
|
|
||||||
c.setX(20);
|
|
||||||
c.setY(30);
|
|
||||||
|
|
||||||
// Now use the same functions in the base class
|
// EmployeeList owns its items, so we must surrender ownership of objects we add.
|
||||||
Shape shape = s;
|
e.disownMemory();
|
||||||
shape.setX(-10);
|
list.addEmployee(e);
|
||||||
shape.setY(5);
|
outputText.append( "----------------------\n" );
|
||||||
|
|
||||||
outputText.append( "\nHere is their current position:\n" );
|
// Now we access the first four items in list (three are C++ objects that
|
||||||
outputText.append( " Circle = (" + c.getX() + " " + c.getY() + ")\n" );
|
// EmployeeList's constructor adds, the last is our CEO). The virtual
|
||||||
outputText.append( " Square = (" + s.getX() + " " + s.getY() + ")\n" );
|
// methods of all these instances are treated the same. For items 0, 1, and
|
||||||
|
// 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
|
||||||
|
// getPosition which resolves in Java. The call to getPosition is
|
||||||
|
// slightly different, however, because of the overidden getPosition() call, since
|
||||||
|
// now the object reference has been "laundered" by passing through
|
||||||
|
// EmployeeList as an Employee*. Previously, Java resolved the call
|
||||||
|
// immediately in CEO, but now Java thinks the object is an instance of
|
||||||
|
// class Employee. So the call passes through the
|
||||||
|
// Employee proxy class and on to the C wrappers and C++ director,
|
||||||
|
// eventually ending up back at the Java 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 Java implementation
|
||||||
|
// in CEO. All this routing takes place transparently.
|
||||||
|
|
||||||
// ----- Call some methods -----
|
outputText.append( "(position, title) for items 0-3:\n" );
|
||||||
|
|
||||||
outputText.append( "\nHere are some properties of the shapes:\n" );
|
outputText.append( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"\n" );
|
||||||
Shape[] shapes = {c,s};
|
outputText.append( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"\n" );
|
||||||
for (int i=0; i<shapes.length; i++)
|
outputText.append( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"\n" );
|
||||||
{
|
outputText.append( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"\n" );
|
||||||
outputText.append( " " + shapes[i].toString() + "\n" );
|
outputText.append( "----------------------\n" );
|
||||||
outputText.append( " area = " + shapes[i].area() + "\n" );
|
|
||||||
outputText.append( " perimeter = " + shapes[i].perimeter() + "\n" );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Notice how the area() and perimeter() functions really
|
// Time to delete the EmployeeList, which will delete all the Employee*
|
||||||
// invoke the appropriate virtual method on each object.
|
// items it contains. The last item is our CEO, which gets destroyed as well.
|
||||||
|
/* Causes app to die
|
||||||
// ----- Delete everything -----
|
list.delete();
|
||||||
|
|
||||||
outputText.append( "\nGuess I'll clean up now\n" );
|
|
||||||
|
|
||||||
// Note: this invokes the virtual destructor
|
|
||||||
// You could leave this to the garbage collector
|
|
||||||
c.delete();
|
|
||||||
s.delete();
|
|
||||||
|
|
||||||
outputText.append( Shape.getNshapes() + " shapes remain\n" );
|
|
||||||
outputText.append( "Goodbye\n" );
|
|
||||||
*/
|
*/
|
||||||
|
outputText.append( "----------------------\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** static constructor */
|
/** static constructor */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue