summaryrefslogtreecommitdiff
path: root/Examples/java/class
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/java/class')
-rw-r--r--Examples/java/class/Makefile18
-rw-r--r--Examples/java/class/example.cxx28
-rw-r--r--Examples/java/class/example.dsp162
-rw-r--r--Examples/java/class/example.h39
-rw-r--r--Examples/java/class/example.i10
-rw-r--r--Examples/java/class/index.html197
-rw-r--r--Examples/java/class/runme.java70
7 files changed, 524 insertions, 0 deletions
diff --git a/Examples/java/class/Makefile b/Examples/java/class/Makefile
new file mode 100644
index 0000000..14c3017
--- /dev/null
+++ b/Examples/java/class/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/class/example.cxx b/Examples/java/class/example.cxx
new file mode 100644
index 0000000..1e8e203
--- /dev/null
+++ b/Examples/java/class/example.cxx
@@ -0,0 +1,28 @@
+/* File : example.c */
+
+#include "example.h"
+#define M_PI 3.14159265358979323846
+
+/* Move the shape to a new location */
+void Shape::move(double dx, double dy) {
+ x += dx;
+ y += dy;
+}
+
+int Shape::nshapes = 0;
+
+double Circle::area(void) {
+ return M_PI*radius*radius;
+}
+
+double Circle::perimeter(void) {
+ return 2*M_PI*radius;
+}
+
+double Square::area(void) {
+ return width*width;
+}
+
+double Square::perimeter(void) {
+ return 4*width;
+}
diff --git a/Examples/java/class/example.dsp b/Examples/java/class/example.dsp
new file mode 100644
index 0000000..fbe87e7
--- /dev/null
+++ b/Examples/java/class/example.dsp
@@ -0,0 +1,162 @@
+# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
+
+CFG=example - Win32 Release
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "example.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "example - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
+# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
+# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x809 /d "_DEBUG"
+# ADD RSC /l 0x809 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept
+# Begin Special Build Tool
+SOURCE="$(InputPath)"
+PostBuild_Desc=Java compile post-build step
+PostBuild_Cmds=echo on "%JAVA_BIN%\javac" *.java
+# End Special Build Tool
+
+!ELSEIF "$(CFG)" == "example - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x809 /d "NDEBUG"
+# ADD RSC /l 0x809 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /machine:I386
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /machine:I386 /out:"example.dll"
+# Begin Special Build Tool
+SOURCE="$(InputPath)"
+PostBuild_Desc=Java compile post-build step
+PostBuild_Cmds=echo on "%JAVA_BIN%\javac" *.java
+# End Special Build Tool
+
+!ENDIF
+
+# Begin Target
+
+# Name "example - Win32 Debug"
+# Name "example - Win32 Release"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=.\example.cxx
+# End Source File
+# Begin Source File
+
+SOURCE=.\example_wrap.cxx
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=.\example.h
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# Begin Source File
+
+SOURCE=.\example.i
+
+!IF "$(CFG)" == "example - Win32 Debug"
+
+# Begin Custom Build
+InputPath=.\example.i
+InputName=example
+
+"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ echo In order to function correctly, please ensure the following environment variables are correctly set:
+ echo JAVA_INCLUDE: %JAVA_INCLUDE%
+ echo JAVA_BIN: %JAVA_BIN%
+ echo on
+ ..\..\..\swig.exe -c++ -java $(InputPath)
+
+# End Custom Build
+
+!ELSEIF "$(CFG)" == "example - Win32 Release"
+
+# Begin Custom Build
+InputPath=.\example.i
+InputName=example
+
+"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ echo In order to function correctly, please ensure the following environment variables are correctly set:
+ echo JAVA_INCLUDE: %JAVA_INCLUDE%
+ echo JAVA_BIN: %JAVA_BIN%
+ echo on
+ ..\..\..\swig.exe -c++ -java $(InputPath)
+
+# End Custom Build
+
+!ENDIF
+
+# End Source File
+# End Target
+# End Project
diff --git a/Examples/java/class/example.h b/Examples/java/class/example.h
new file mode 100644
index 0000000..46d9013
--- /dev/null
+++ b/Examples/java/class/example.h
@@ -0,0 +1,39 @@
+/* File : example.h */
+
+class Shape {
+public:
+ Shape() {
+ nshapes++;
+ }
+ virtual ~Shape() {
+ nshapes--;
+ };
+ double x, y;
+ void move(double dx, double dy);
+ virtual double area(void) = 0;
+ virtual double perimeter(void) = 0;
+ static int nshapes;
+};
+
+class Circle : public Shape {
+private:
+ double radius;
+public:
+ Circle(double r) : radius(r) { };
+ virtual double area(void);
+ virtual double perimeter(void);
+};
+
+class Square : public Shape {
+private:
+ double width;
+public:
+ Square(double w) : width(w) { };
+ virtual double area(void);
+ virtual double perimeter(void);
+};
+
+
+
+
+
diff --git a/Examples/java/class/example.i b/Examples/java/class/example.i
new file mode 100644
index 0000000..75700b3
--- /dev/null
+++ b/Examples/java/class/example.i
@@ -0,0 +1,10 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
diff --git a/Examples/java/class/index.html b/Examples/java/class/index.html
new file mode 100644
index 0000000..cf9130c
--- /dev/null
+++ b/Examples/java/class/index.html
@@ -0,0 +1,197 @@
+<html>
+<head>
+<title>SWIG:Examples:java:class</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/java/class/</tt>
+<hr>
+
+<H2>Wrapping a simple C++ class</H2>
+
+<p>
+This example illustrates the high level form of C++ class wrapping performed
+by SWIG. In this case, a C++ class has a proxy Java class, which
+provides access to C++ class members.
+
+<h2>The C++ Code</h2>
+
+Suppose you have some C++ classes described by the following (and admittedly lame)
+header file:
+
+<blockquote>
+<pre>
+/* File : example.h */
+
+class Shape {
+public:
+ Shape() {
+ nshapes++;
+ }
+ virtual ~Shape() {
+ nshapes--;
+ };
+ double x, y;
+ void move(double dx, double dy);
+ virtual double area() = 0;
+ virtual double perimeter() = 0;
+ static int nshapes;
+};
+
+class Circle : public Shape {
+private:
+ double radius;
+public:
+ Circle(double r) : radius(r) { };
+ virtual double area();
+ virtual double perimeter();
+};
+
+class Square : public Shape {
+private:
+ double width;
+public:
+ Square(double w) : width(w) { };
+ virtual double area();
+ virtual double perimeter();
+};
+</pre>
+</blockquote>
+
+<h2>The SWIG interface</h2>
+
+A simple SWIG interface for this can be built by simply grabbing the header file
+like this:
+
+<blockquote>
+<pre>
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+</pre>
+</blockquote>
+
+Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
+<blockquote>
+<pre>
+% swig -c++ -java example.i
+</pre>
+</blockquote>
+
+<h2>A sample Java program</h2>
+
+Click <a href="runme.java">here</a> to see a Java program that calls the C++ functions from Java.
+
+<h2>Key points</h2>
+
+<ul>
+<li>To create a new object, you call a constructor like this:
+
+<blockquote>
+<pre>
+Circle c = new Circle(10);
+</pre>
+</blockquote>
+
+<p>
+<li>To access member data, a pair of accessor functions are used.
+For example:
+
+<blockquote>
+<pre>
+c.setX(15); // Set member data
+x = c.getX(); // Get member data
+</pre>
+</blockquote>
+
+<p>
+<li>To invoke a member function, you simply do this
+
+<blockquote>
+<pre>
+System.out.println( "The area is " + c.area() );
+</pre>
+</blockquote>
+
+<p>
+<li>To invoke a destructor, simply do this
+
+<blockquote>
+<pre>
+c.delete(); // Deletes a shape
+</pre>
+</blockquote>
+
+<p>
+<li>Static member variables are wrapped with java static get and set access functions. For example:
+
+<blockquote>
+<pre>
+n = Shape.getNshapes(); // Get a static data member
+Shape.setNshapes(13); // Set a static data member
+</pre>
+</blockquote>
+
+</ul>
+
+<h2>General Comments</h2>
+
+<ul>
+<li>This high-level interface using proxy classes is not the only way to handle C++ code.
+A low level interface using c functions to access member variables and member functions is the alternative SWIG
+approach. This entails passing around the c pointer or c++ 'this' pointer and as such it is not difficult to crash the JVM.
+The abstraction of the underlying pointer by the java proxy classes far better fits the java programming paradigm.
+
+<p>
+<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
+hierarchy (including multiple inheritance). However Java classes can only derive from one base class so multiple inheritance
+is not implemented. Java classes can implement more than one interface so there is scope for improvement in the future.
+
+<p>
+<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
+short and incomplete list:
+
+<p>
+<ul>
+<li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
+conflicts so you must give an alternative name to any overloaded method name using the
+%name directive like this:
+
+<blockquote>
+<pre>
+void foo(int a);
+%name(foo2) void foo(double a, double b);
+</pre>
+</blockquote>
+
+<p>
+<li>Overloaded operators. Not supported at all. The only workaround for this is
+to write a helper function. For example:
+
+<blockquote>
+<pre>
+%inline %{
+ Vector *vector_add(Vector *a, Vector *b) {
+ ... whatever ...
+ }
+%}
+</pre>
+</blockquote>
+
+<p>
+<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
+
+</ul>
+</ul>
+
+<hr>
+</body>
+</html>
diff --git a/Examples/java/class/runme.java b/Examples/java/class/runme.java
new file mode 100644
index 0000000..e1ea0d7
--- /dev/null
+++ b/Examples/java/class/runme.java
@@ -0,0 +1,70 @@
+// This example illustrates how C++ classes can be used from Java using SWIG.
+// The Java class gets mapped onto the C++ class and behaves as if it is a Java class.
+
+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[])
+ {
+ // ----- Object creation -----
+
+ System.out.println( "Creating some objects:" );
+ Circle c = new Circle(10);
+ System.out.println( " Created circle " + c );
+ Square s = new Square(10);
+ System.out.println( " Created square " + s );
+
+ // ----- Access a static member -----
+
+ System.out.println( "\nA total of " + Shape.getNshapes() + " shapes were created" );
+
+ // ----- Member data access -----
+
+ // Notice how we can do this using functions specific to
+ // the 'Circle' class.
+ c.setX(20);
+ c.setY(30);
+
+ // Now use the same functions in the base class
+ Shape shape = s;
+ shape.setX(-10);
+ shape.setY(5);
+
+ System.out.println( "\nHere is their current position:" );
+ System.out.println( " Circle = (" + c.getX() + " " + c.getY() + ")" );
+ System.out.println( " Square = (" + s.getX() + " " + s.getY() + ")" );
+
+ // ----- Call some methods -----
+
+ System.out.println( "\nHere are some properties of the shapes:" );
+ Shape[] shapes = {c,s};
+ for (int i=0; i<shapes.length; i++)
+ {
+ System.out.println( " " + shapes[i].toString() );
+ System.out.println( " area = " + shapes[i].area() );
+ System.out.println( " perimeter = " + shapes[i].perimeter() );
+ }
+
+ // Notice how the area() and perimeter() functions really
+ // invoke the appropriate virtual method on each object.
+
+ // ----- Delete everything -----
+
+ System.out.println( "\nGuess I'll clean up now" );
+
+ // Note: this invokes the virtual destructor
+ // You could leave this to the garbage collector
+ c.delete();
+ s.delete();
+
+ System.out.println( Shape.getNshapes() + " shapes remain" );
+ System.out.println( "Goodbye" );
+ }
+}