diff options
Diffstat (limited to 'Examples/java/enum')
-rw-r--r-- | Examples/java/enum/Makefile | 18 | ||||
-rw-r--r-- | Examples/java/enum/example.cxx | 37 | ||||
-rw-r--r-- | Examples/java/enum/example.h | 13 | ||||
-rw-r--r-- | Examples/java/enum/example.i | 14 | ||||
-rw-r--r-- | Examples/java/enum/index.html | 29 | ||||
-rw-r--r-- | Examples/java/enum/runme.java | 38 |
6 files changed, 149 insertions, 0 deletions
diff --git a/Examples/java/enum/Makefile b/Examples/java/enum/Makefile new file mode 100644 index 0000000..14c3017 --- /dev/null +++ b/Examples/java/enum/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/enum/example.cxx b/Examples/java/enum/example.cxx new file mode 100644 index 0000000..df7bb63 --- /dev/null +++ b/Examples/java/enum/example.cxx @@ -0,0 +1,37 @@ +/* File : example.cxx */ + +#include "example.h" +#include <stdio.h> + +void Foo::enum_test(speed s) { + if (s == IMPULSE) { + printf("IMPULSE speed\n"); + } else if (s == WARP) { + printf("WARP speed\n"); + } else if (s == LUDICROUS) { + printf("LUDICROUS speed\n"); + } else { + printf("Unknown speed\n"); + } +} + +void enum_test(color c, Foo::speed s) { + if (c == RED) { + printf("color = RED, "); + } else if (c == BLUE) { + printf("color = BLUE, "); + } else if (c == GREEN) { + printf("color = GREEN, "); + } else { + printf("color = Unknown color!, "); + } + if (s == Foo::IMPULSE) { + printf("speed = IMPULSE speed\n"); + } else if (s == Foo::WARP) { + printf("speed = WARP speed\n"); + } else if (s == Foo::LUDICROUS) { + printf("speed = LUDICROUS speed\n"); + } else { + printf("speed = Unknown speed!\n"); + } +} diff --git a/Examples/java/enum/example.h b/Examples/java/enum/example.h new file mode 100644 index 0000000..9119cd9 --- /dev/null +++ b/Examples/java/enum/example.h @@ -0,0 +1,13 @@ +/* File : example.h */ + +enum color { RED, BLUE, GREEN }; + +class Foo { + public: + Foo() { } + enum speed { IMPULSE=10, WARP=20, LUDICROUS=30 }; + void enum_test(speed s); +}; + +void enum_test(color c, Foo::speed s); + diff --git a/Examples/java/enum/example.i b/Examples/java/enum/example.i new file mode 100644 index 0000000..fb65689 --- /dev/null +++ b/Examples/java/enum/example.i @@ -0,0 +1,14 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Force the generated Java code to use the C enum values rather than making a JNI call */ +%javaconst(1); + +/* Let's just grab the original header file here */ + +%include "example.h" + diff --git a/Examples/java/enum/index.html b/Examples/java/enum/index.html new file mode 100644 index 0000000..20daf26 --- /dev/null +++ b/Examples/java/enum/index.html @@ -0,0 +1,29 @@ +<html> +<head> +<title>SWIG:Examples:java:enum</title> +</head> + +<body bgcolor="#ffffff"> + + +<tt>SWIG/Examples/java/enum/</tt> +<hr> + +<H2>Wrapping enumerations</H2> + +<p> +This example tests SWIG's ability to wrap enumerations. +SWIG wraps enums in numerous different ways. The default approach is to wrap +each enum with the typesafe enum pattern. Enums are handled as integers in the JNI layer. +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="runme.java">runme.java</a>. Sample Java program. +</ul> + +<hr> +</body> +</html> diff --git a/Examples/java/enum/runme.java b/Examples/java/enum/runme.java new file mode 100644 index 0000000..56e49af --- /dev/null +++ b/Examples/java/enum/runme.java @@ -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); + } +} |