summaryrefslogtreecommitdiff
path: root/Examples/java/extend
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/java/extend')
-rw-r--r--Examples/java/extend/Makefile18
-rw-r--r--Examples/java/extend/example.cxx4
-rw-r--r--Examples/java/extend/example.h56
-rw-r--r--Examples/java/extend/example.i15
-rw-r--r--Examples/java/extend/index.html19
-rw-r--r--Examples/java/extend/runme.java88
6 files changed, 200 insertions, 0 deletions
diff --git a/Examples/java/extend/Makefile b/Examples/java/extend/Makefile
new file mode 100644
index 0000000..14c3017
--- /dev/null
+++ b/Examples/java/extend/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: java
+
+java::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java_cpp
+ javac *.java
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile java_clean
+
+check: all
diff --git a/Examples/java/extend/example.cxx b/Examples/java/extend/example.cxx
new file mode 100644
index 0000000..450d756
--- /dev/null
+++ b/Examples/java/extend/example.cxx
@@ -0,0 +1,4 @@
+/* File : example.cxx */
+
+#include "example.h"
+
diff --git a/Examples/java/extend/example.h b/Examples/java/extend/example.h
new file mode 100644
index 0000000..b27ab97
--- /dev/null
+++ b/Examples/java/extend/example.h
@@ -0,0 +1,56 @@
+/* File : example.h */
+
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <cmath>
+
+class Employee {
+private:
+ std::string name;
+public:
+ Employee(const char* n): name(n) {}
+ virtual std::string getTitle() { return getPosition() + " " + getName(); }
+ virtual std::string getName() { return name; }
+ virtual std::string getPosition() const { return "Employee"; }
+ virtual ~Employee() { printf("~Employee() @ %p\n", this); }
+};
+
+
+class Manager: public Employee {
+public:
+ Manager(const char* n): Employee(n) {}
+ virtual std::string getPosition() const { return "Manager"; }
+};
+
+
+class EmployeeList {
+ std::vector<Employee*> list;
+public:
+ EmployeeList() {
+ list.push_back(new Employee("Bob"));
+ list.push_back(new Employee("Jane"));
+ list.push_back(new Manager("Ted"));
+ }
+ void addEmployee(Employee *p) {
+ list.push_back(p);
+ std::cout << "New employee added. Current employees are:" << std::endl;
+ std::vector<Employee*>::iterator i;
+ for (i=list.begin(); i!=list.end(); i++) {
+ std::cout << " " << (*i)->getTitle() << std::endl;
+ }
+ }
+ const Employee *get_item(int i) {
+ return list[i];
+ }
+ ~EmployeeList() {
+ std::vector<Employee*>::iterator i;
+ std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl;
+ for (i=list.begin(); i!=list.end(); i++) {
+ delete *i;
+ }
+ std::cout << "~EmployeeList empty." << std::endl;
+ }
+};
+
diff --git a/Examples/java/extend/example.i b/Examples/java/extend/example.i
new file mode 100644
index 0000000..c8ec32e
--- /dev/null
+++ b/Examples/java/extend/example.i
@@ -0,0 +1,15 @@
+/* File : example.i */
+%module(directors="1") example
+%{
+#include "example.h"
+%}
+
+%include "std_vector.i"
+%include "std_string.i"
+
+/* turn on director wrapping for Manager */
+%feature("director") Employee;
+%feature("director") Manager;
+
+%include "example.h"
+
diff --git a/Examples/java/extend/index.html b/Examples/java/extend/index.html
new file mode 100644
index 0000000..c7c2c12
--- /dev/null
+++ b/Examples/java/extend/index.html
@@ -0,0 +1,19 @@
+<html>
+<head>
+<title>SWIG:Examples:java:extend</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/java/extend/</tt>
+<hr>
+
+<H2>Extending a simple C++ class in Java</H2>
+
+<p>
+This example illustrates the extending of a C++ class with cross language polymorphism.
+
+<hr>
+</body>
+</html>
diff --git a/Examples/java/extend/runme.java b/Examples/java/extend/runme.java
new file mode 100644
index 0000000..629bb14
--- /dev/null
+++ b/Examples/java/extend/runme.java
@@ -0,0 +1,88 @@
+// This file illustrates the cross language polymorphism using directors.
+
+
+// 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 runme {
+ static {
+ try {
+ System.loadLibrary("example");
+ } catch (UnsatisfiedLinkError e) {
+ System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
+ System.exit(1);
+ }
+ }
+
+ public static void main(String argv[])
+ {
+
+ // Create an instance of CEO, a class derived from the Java proxy of the
+ // underlying C++ class. The calls to getName() and getPosition() are standard,
+ // the call to getTitle() uses the director wrappers to call CEO.getPosition().
+
+ CEO e = new CEO("Alice");
+ System.out.println( e.getName() + " is a " + e.getPosition() );
+ System.out.println( "Just call her \"" + e.getTitle() + "\"" );
+ System.out.println( "----------------------" );
+
+
+ // Create a new EmployeeList instance. This class does not have a C++
+ // director wrapper, but can be used freely with other classes that do.
+
+ EmployeeList list = new EmployeeList();
+
+ // EmployeeList owns its items, so we must surrender ownership of objects we add.
+ e.disownMemory();
+ list.addEmployee(e);
+ System.out.println( "----------------------" );
+
+ // 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, 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.
+
+ System.out.println( "(position, title) for items 0-3:" );
+
+ System.out.println( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" );
+ System.out.println( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" );
+ System.out.println( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" );
+ System.out.println( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" );
+ System.out.println( "----------------------" );
+
+ // Time to delete the EmployeeList, which will delete all the Employee*
+ // items it contains. The last item is our CEO, which gets destroyed as well.
+ list.delete();
+ System.out.println( "----------------------" );
+
+ // All done.
+
+ System.out.println( "java exit" );
+
+ }
+}