rename java run tests from main to runme for consistency across the languages
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10932 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a5d91ce4f1
commit
9cf5256b93
26 changed files with 55 additions and 55 deletions
56
Examples/java/callback/runme.java
Normal file
56
Examples/java/callback/runme.java
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
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[] args)
|
||||
{
|
||||
System.out.println("Adding and calling a normal C++ callback");
|
||||
System.out.println("----------------------------------------");
|
||||
|
||||
Caller caller = new Caller();
|
||||
Callback callback = new Callback();
|
||||
|
||||
caller.setCallback(callback);
|
||||
caller.call();
|
||||
caller.delCallback();
|
||||
|
||||
callback = new JavaCallback();
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Adding and calling a Java callback");
|
||||
System.out.println("------------------------------------");
|
||||
|
||||
caller.setCallback(callback);
|
||||
caller.call();
|
||||
caller.delCallback();
|
||||
|
||||
// Test that a double delete does not occur as the object has already been deleted from the C++ layer.
|
||||
// Note that the garbage collector can also call the delete() method via the finalizer (callback.finalize())
|
||||
// at any point after here.
|
||||
callback.delete();
|
||||
|
||||
System.out.println();
|
||||
System.out.println("java exit");
|
||||
}
|
||||
}
|
||||
|
||||
class JavaCallback extends Callback
|
||||
{
|
||||
public JavaCallback()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
System.out.println("JavaCallback.run()");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> op
|
|||
|
||||
<h2>A sample Java program</h2>
|
||||
|
||||
Click <a href="main.java">here</a> to see a Java program that calls the C++ functions from Java.
|
||||
Click <a href="runme.java">here</a> to see a Java program that calls the C++ functions from Java.
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
|
|
|
|||
70
Examples/java/class/runme.java
Normal file
70
Examples/java/class/runme.java
Normal file
|
|
@ -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" );
|
||||
}
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ to see a SWIG interface with some constant declarations in it.
|
|||
Click <a href="../../../Doc/Manual/Java.html#constants">here</a> for the section on constants in the SWIG and Java documentation.
|
||||
<p>
|
||||
|
||||
Click <a href="main.java">here</a> to see a Java program that prints out the values
|
||||
Click <a href="runme.java">here</a> to see a Java program that prints out the values
|
||||
of the constants contained in the above file.</p>
|
||||
<h2>Key points</h2>
|
||||
<ul>
|
||||
|
|
|
|||
44
Examples/java/constants/runme.java
Normal file
44
Examples/java/constants/runme.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import java.lang.reflect.*;
|
||||
|
||||
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[])
|
||||
{
|
||||
System.out.println("ICONST = " + example.ICONST + " (should be 42)");
|
||||
System.out.println("FCONST = " + example.FCONST + " (should be 2.1828)");
|
||||
System.out.println("CCONST = " + example.CCONST + " (should be 'x')");
|
||||
System.out.println("CCONST2 = " + example.CCONST2 + " (this should be on a new line)");
|
||||
System.out.println("SCONST = " + example.SCONST + " (should be 'Hello World')");
|
||||
System.out.println("SCONST2 = " + example.SCONST2 + " (should be '\"Hello World\"')");
|
||||
System.out.println("EXPR = " + example.EXPR + " (should be 48.5484)");
|
||||
System.out.println("iconst = " + example.iconst + " (should be 37)");
|
||||
System.out.println("fconst = " + example.fconst + " (should be 3.14)");
|
||||
|
||||
// Use reflection to check if these variables are defined:
|
||||
try
|
||||
{
|
||||
System.out.println("EXTERN = " + example.class.getField("EXTERN") + " (Arg! This shouldn't print anything)");
|
||||
}
|
||||
catch (NoSuchFieldException e)
|
||||
{
|
||||
System.out.println("EXTERN isn't defined (good)");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
System.out.println("FOO = " + example.class.getField("FOO") + " (Arg! This shouldn't print anything)");
|
||||
}
|
||||
catch (NoSuchFieldException e)
|
||||
{
|
||||
System.out.println("FOO isn't defined (good)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ See the documentation for the other approaches for wrapping enums.
|
|||
<ul>
|
||||
<li><a href="example.h">example.h</a>. Header file containing some enums.
|
||||
<li><a href="example.i">example.i</a>. Interface file.
|
||||
<li><a href="main.java">main.java</a>. Sample Java program.
|
||||
<li><a href="runme.java">runme.java</a>. Sample Java program.
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
|
|
|||
38
Examples/java/enum/runme.java
Normal file
38
Examples/java/enum/runme.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
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[])
|
||||
{
|
||||
// Print out the value of some enums
|
||||
System.out.println("*** color ***");
|
||||
System.out.println(" " + color.RED + " = " + color.RED.swigValue());
|
||||
System.out.println(" " + color.BLUE + " = " + color.BLUE.swigValue());
|
||||
System.out.println(" " + color.GREEN + " = " + color.GREEN.swigValue());
|
||||
|
||||
System.out.println("\n*** Foo::speed ***");
|
||||
System.out.println(" Foo::" + Foo.speed.IMPULSE + " = " + Foo.speed.IMPULSE.swigValue());
|
||||
System.out.println(" Foo::" + Foo.speed.WARP + " = " + Foo.speed.WARP.swigValue());
|
||||
System.out.println(" Foo::" + Foo.speed.LUDICROUS + " = " + Foo.speed.LUDICROUS.swigValue());
|
||||
|
||||
System.out.println("\nTesting use of enums with functions\n");
|
||||
|
||||
example.enum_test(color.RED, Foo.speed.IMPULSE);
|
||||
example.enum_test(color.BLUE, Foo.speed.WARP);
|
||||
example.enum_test(color.GREEN, Foo.speed.LUDICROUS);
|
||||
|
||||
System.out.println( "\nTesting use of enum with class method" );
|
||||
Foo f = new Foo();
|
||||
|
||||
f.enum_test(Foo.speed.IMPULSE);
|
||||
f.enum_test(Foo.speed.WARP);
|
||||
f.enum_test(Foo.speed.LUDICROUS);
|
||||
}
|
||||
}
|
||||
88
Examples/java/extend/runme.java
Normal file
88
Examples/java/extend/runme.java
Normal file
|
|
@ -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" );
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -66,7 +66,7 @@ Here are some files that illustrate this with a simple example:
|
|||
<li><a href="example.c">example.c</a>
|
||||
<li><a href="example.h">example.h</a>
|
||||
<li><a href="example.i">example.i</a> (SWIG interface)
|
||||
<li><a href="main.java">main.java</a> (Sample program)
|
||||
<li><a href="runme.java">runme.java</a> (Sample program)
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
|
|
|||
33
Examples/java/funcptr/runme.java
Normal file
33
Examples/java/funcptr/runme.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
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[]) {
|
||||
|
||||
|
||||
int a = 37;
|
||||
int b = 42;
|
||||
|
||||
// Now call our C function with a bunch of callbacks
|
||||
|
||||
System.out.println( "Trying some C callback functions" );
|
||||
System.out.println( " a = " + a );
|
||||
System.out.println( " b = " + b );
|
||||
System.out.println( " ADD(a,b) = " + example.do_op(a,b,example.ADD) );
|
||||
System.out.println( " SUB(a,b) = " + example.do_op(a,b,example.SUB) );
|
||||
System.out.println( " MUL(a,b) = " + example.do_op(a,b,example.MUL) );
|
||||
|
||||
System.out.println( "Here is what the C callback function classes are called in Java" );
|
||||
System.out.println( " ADD = " + example.ADD.getClass().getName() );
|
||||
System.out.println( " SUB = " + example.SUB.getClass().getName() );
|
||||
System.out.println( " MUL = " + example.MUL.getClass().getName() );
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ certain C declarations are turned into constants.
|
|||
<h2>Running the examples</h2>
|
||||
Please see the <a href="../../Doc/Manual/Windows.html">Windows</a> page in the main manual for information on using the examples on Windows. <p>
|
||||
|
||||
On Unix most of the examples work by making the Makefile before executing the program main.java. The Makefile will output the swig generated JNI c code as well as the Java wrapper classes. Additionally the JNI c/c++ code is compiled into the shared object (dynamic link library) which is needed for dynamic linking to the native code. The Makefiles also compile the Java files using javac.
|
||||
On Unix most of the examples work by making the Makefile before executing the program runme.java. The Makefile will output the swig generated JNI c code as well as the Java wrapper classes. Additionally the JNI c/c++ code is compiled into the shared object (dynamic link library) which is needed for dynamic linking to the native code. The Makefiles also compile the Java files using javac.
|
||||
<p>
|
||||
Ensure that the dynamic link library file is in the appropriate path before executing the Java program. For example in Unix, libexample.so must be in the LD_LIBRARY_PATH.
|
||||
<p>
|
||||
|
|
@ -39,7 +39,7 @@ A Unix example:
|
|||
<pre>
|
||||
$ make
|
||||
$ export LD_LIBRARY_PATH=. #ksh
|
||||
$ java main
|
||||
$ java runme
|
||||
</pre>
|
||||
</blockquote>
|
||||
<p>
|
||||
|
|
|
|||
40
Examples/java/multimap/runme.java
Normal file
40
Examples/java/multimap/runme.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
|
||||
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[]) {
|
||||
|
||||
// Call our gcd() function
|
||||
int x = 42;
|
||||
int y = 105;
|
||||
int g = example.gcd(x,y);
|
||||
System.out.println("The gcd of " + x + " and " + y + " is " + g);
|
||||
|
||||
// Call the gcdmain() function
|
||||
String[] args = {"gcdmain","42","105"};
|
||||
example.gcdmain(args);
|
||||
|
||||
// Call the count function
|
||||
System.out.println(example.count("Hello World", 'l'));
|
||||
|
||||
// Call the capitalize function
|
||||
String[] capitalizeMe = {"hello world"};
|
||||
example.capitalize(capitalizeMe);
|
||||
System.out.println(capitalizeMe[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -18,7 +18,7 @@ This example compares wrapping a c global function using the manual way and the
|
|||
|
||||
<ul>
|
||||
<li><a href="example.i">example.i</a>. Interface file comparing code wrapped by SWIG and wrapped manually.
|
||||
<li><a href="main.java">main.java</a>. Sample Java program showing calls to both manually wrapped and SWIG wrapped c functions.
|
||||
<li><a href="runme.java">runme.java</a>. Sample Java program showing calls to both manually wrapped and SWIG wrapped c functions.
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
|
|
|||
19
Examples/java/native/runme.java
Normal file
19
Examples/java/native/runme.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
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[]) {
|
||||
SWIGTYPE_p_Point p = example.point_create(1, 2);
|
||||
System.out.println("auto wrapped : " + example.point_toString1(p));
|
||||
System.out.println("manual wrapped: " + example.point_toString2(p));
|
||||
example.free(new SWIGTYPE_p_void(SWIGTYPE_p_Point.getCPtr(p), false)); //clean up c allocated memory
|
||||
}
|
||||
}
|
||||
|
|
@ -144,7 +144,7 @@ extraction.
|
|||
<ul>
|
||||
<li> <a href="example.c">example.c</a> (C Source)
|
||||
<li> <a href="example.i">example.i</a> (Swig interface)
|
||||
<li> <a href="main.java">main.java</a> (Java program)
|
||||
<li> <a href="runme.java">runme.java</a> (Java program)
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
|
|
|||
55
Examples/java/pointer/runme.java
Normal file
55
Examples/java/pointer/runme.java
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
|
||||
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[]) {
|
||||
|
||||
// First create some objects using the pointer library.
|
||||
System.out.println("Testing the pointer library");
|
||||
SWIGTYPE_p_int a = example.new_intp();
|
||||
SWIGTYPE_p_int b = example.new_intp();
|
||||
SWIGTYPE_p_int c = example.new_intp();
|
||||
example.intp_assign(a,37);
|
||||
example.intp_assign(b,42);
|
||||
|
||||
// Note that getCPtr() has package access by default
|
||||
System.out.println(" a =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(a)));
|
||||
System.out.println(" b =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(b)));
|
||||
System.out.println(" c =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(c)));
|
||||
|
||||
// Call the add() function with some pointers
|
||||
example.add(a,b,c);
|
||||
|
||||
// Now get the result
|
||||
int res = example.intp_value(c);
|
||||
System.out.println(" 37 + 42 =" + res);
|
||||
|
||||
// Clean up the pointers
|
||||
example.delete_intp(a);
|
||||
example.delete_intp(b);
|
||||
example.delete_intp(c);
|
||||
|
||||
// Now try the typemap library
|
||||
// Now it is no longer necessary to manufacture pointers.
|
||||
// Instead we use a single element array which in Java is modifiable.
|
||||
|
||||
System.out.println("Trying the typemap library");
|
||||
int[] r = {0};
|
||||
example.sub(37,42,r);
|
||||
System.out.println(" 37 - 42 = " + r[0]);
|
||||
|
||||
// Now try the version with return value
|
||||
|
||||
System.out.println("Testing return value");
|
||||
int q = example.divide(42,37,r);
|
||||
System.out.println(" 42/37 = " + q + " remainder " + r[0]);
|
||||
}
|
||||
}
|
||||
|
|
@ -121,7 +121,7 @@ Click <a href="example.i">here</a> to see a SWIG interface file with these addit
|
|||
|
||||
<h2>Sample Java program</h2>
|
||||
|
||||
Click <a href="main.java">here</a> to see a Java program that manipulates some C++ references.
|
||||
Click <a href="runme.java">here</a> to see a Java program that manipulates some C++ references.
|
||||
|
||||
<h2>Notes:</h2>
|
||||
|
||||
|
|
|
|||
79
Examples/java/reference/runme.java
Normal file
79
Examples/java/reference/runme.java
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// This example illustrates the manipulation of C++ references in Java.
|
||||
|
||||
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[])
|
||||
{
|
||||
System.out.println( "Creating some objects:" );
|
||||
Vector a = new Vector(3,4,5);
|
||||
Vector b = new Vector(10,11,12);
|
||||
|
||||
System.out.println( " Created " + a.print() );
|
||||
System.out.println( " Created " + b.print() );
|
||||
|
||||
// ----- Call an overloaded operator -----
|
||||
|
||||
// This calls the wrapper we placed around
|
||||
//
|
||||
// operator+(const Vector &a, const Vector &)
|
||||
//
|
||||
// It returns a new allocated object.
|
||||
|
||||
System.out.println( "Adding a+b" );
|
||||
Vector c = example.addv(a,b);
|
||||
System.out.println( " a+b = " + c.print() );
|
||||
|
||||
// Note: Unless we free the result, a memory leak will occur if the -noproxy commandline
|
||||
// is used as the proxy classes define finalizers which call the delete() method. When
|
||||
// -noproxy is not specified the memory management is controlled by the garbage collector.
|
||||
// You can still call delete(). It will free the c++ memory immediately, but not the
|
||||
// Java memory! You then must be careful not to call any member functions as it will
|
||||
// use a NULL c pointer on the underlying c++ object. We set the Java object to null
|
||||
// which will then throw a Java exception should we attempt to use it again.
|
||||
c.delete();
|
||||
c = null;
|
||||
|
||||
// ----- Create a vector array -----
|
||||
|
||||
System.out.println( "Creating an array of vectors" );
|
||||
VectorArray va = new VectorArray(10);
|
||||
System.out.println( " va = " + va.toString() );
|
||||
|
||||
// ----- Set some values in the array -----
|
||||
|
||||
// These operators copy the value of Vector a and Vector b to the vector array
|
||||
va.set(0,a);
|
||||
va.set(1,b);
|
||||
|
||||
// This works, but it would cause a memory leak if -noproxy was used!
|
||||
|
||||
va.set(2,example.addv(a,b));
|
||||
|
||||
|
||||
// Get some values from the array
|
||||
|
||||
System.out.println( "Getting some array values" );
|
||||
for (int i=0; i<5; i++)
|
||||
System.out.println( " va(" + i + ") = " + va.get(i).print() );
|
||||
|
||||
// Watch under resource meter to check on this
|
||||
System.out.println( "Making sure we don't leak memory." );
|
||||
for (int i=0; i<1000000; i++)
|
||||
c = va.get(i%10);
|
||||
|
||||
// ----- Clean up -----
|
||||
// This could be omitted. The garbage collector would then clean up for us.
|
||||
System.out.println( "Cleaning up" );
|
||||
va.delete();
|
||||
a.delete();
|
||||
b.delete();
|
||||
}
|
||||
}
|
||||
|
|
@ -65,15 +65,15 @@ to create the extension <tt>libexample.so (unix)</tt>.
|
|||
|
||||
<h2>Using the extension</h2>
|
||||
|
||||
Click <a href="main.java">here</a> to see a program that calls our C functions from Java.
|
||||
Click <a href="runme.java">here</a> to see a program that calls our C functions from Java.
|
||||
<p>
|
||||
Compile the java files <tt><a href="example.java">example.java</a></tt> and <tt><a href="main.java">main.java</a></tt>
|
||||
to create the class files example.class and main.class before running main in the JVM. Ensure that the libexample.so file is in your LD_LIBRARY_PATH before running. For example:
|
||||
Compile the java files <tt><a href="example.java">example.java</a></tt> and <tt><a href="runme.java">runme.java</a></tt>
|
||||
to create the class files example.class and runme.class before running runme in the JVM. Ensure that the libexample.so file is in your LD_LIBRARY_PATH before running. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
export LD_LIBRARY_PATH=. #ksh
|
||||
javac *.java
|
||||
java main
|
||||
java runme
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
|
|
|
|||
32
Examples/java/simple/runme.java
Normal file
32
Examples/java/simple/runme.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
|
||||
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[]) {
|
||||
// Call our gcd() function
|
||||
|
||||
int x = 42;
|
||||
int y = 105;
|
||||
int g = example.gcd(x,y);
|
||||
System.out.println("The gcd of " + x + " and " + y + " is " + g);
|
||||
|
||||
// Manipulate the Foo global variable
|
||||
|
||||
// Output its current value
|
||||
System.out.println("Foo = " + example.getFoo());
|
||||
|
||||
// Change its value
|
||||
example.setFoo(3.1415926);
|
||||
|
||||
// See if the change took effect
|
||||
System.out.println("Foo = " + example.getFoo());
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue