Enum typemaps taken out of java.swg

New typemap files for the different C/C++ to Java enum wrapping approaches.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5921 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2004-05-24 20:09:17 +00:00
commit e7777f2adb
5 changed files with 234 additions and 19 deletions

76
SWIG/Lib/java/enums.swg Normal file
View file

@ -0,0 +1,76 @@
/* -----------------------------------------------------------------------------
* Include this file in order for C/C++ enums to be wrapped by proper Java enums.
* Note that the JNI layer handles the enum as an int. The Java enum has extra
* code generated to store the C++ int value. This is required for C++ enums that
* specify a value for the enum item, as native Java enums do not support this.
* ----------------------------------------------------------------------------- */
%typemap(jni) enum SWIGTYPE "jint"
%typemap(jtype) enum SWIGTYPE "int"
%typemap(jstype) enum SWIGTYPE "$javaclassname"
%typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
%typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %}
%typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;"
%typemap(javadirectorin) enum SWIGTYPE "$jniinput"
%typemap(javadirectorout) enum SWIGTYPE "$javacall"
%typecheck(SWIG_TYPECHECK_INT32) enum SWIGTYPE ""
%typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
%typemap(javaout) enum SWIGTYPE {
return $javaclassname.swigToEnum($jnicall);
}
%typemap(throws) enum SWIGTYPE {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown");
}
%typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
%typemap(javaout) enum SWIGTYPE {
return $javaclassname.swigToEnum($jnicall);
}
%typemap(javaclassmodifiers) enum SWIGTYPE "public enum"
/*
* SwigNext static inner class used instead of a static int as static fields cannot be accessed from enum initialisers.
* The swigToEnum method is used to find the Java enum from a C++ enum integer value. The default one here takes
* advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
* values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
* written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
*/
%typemap(javagetcptr) enum SWIGTYPE %{
public final int swigValue() {
return swigValue;
}
public static $javaclassname swigToEnum(int swigValue) {
$javaclassname[] swigValues = $javaclassname.class.getEnumConstants();
if (swigValue < swigValues.length && swigValues[swigValue].swigValue == swigValue)
return swigValues[swigValue];
for ($javaclassname swigEnum : swigValues)
if (swigEnum.swigValue == swigValue)
return swigEnum;
throw new IllegalArgumentException("No enum " + $javaclassname.class + " with value " + swigValue);
}
private $javaclassname() {
this.swigValue = SwigNext.next++;
}
private $javaclassname(int swigValue) {
this.swigValue = swigValue;
SwigNext.next = swigValue+1;
}
private final int swigValue;
private static class SwigNext {
private static int next = 0;
}
%}
%javaenum(proper);

View file

@ -0,0 +1,39 @@
/* -----------------------------------------------------------------------------
* This file provides backwards compatible enum wrapping. SWIG versions 1.3.21
* and earlier wrapped global enums with constant integers in the module class
* or Constants interface. Enums declared within a C++ class were wrapped by
* constant integers in the Java proxy class.
* ----------------------------------------------------------------------------- */
%typemap(jni) enum SWIGTYPE "jint"
%typemap(jtype) enum SWIGTYPE "int"
%typemap(jstype) enum SWIGTYPE "int"
%typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
%typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %}
%typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;"
%typemap(javadirectorin) enum SWIGTYPE "$jniinput"
%typemap(javadirectorout) enum SWIGTYPE "$javacall"
%typecheck(SWIG_TYPECHECK_INT32) enum SWIGTYPE ""
%typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
%typemap(javaout) enum SWIGTYPE {
return $javaclassname.swigToEnum($jnicall);
}
%typemap(throws) enum SWIGTYPE {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown");
}
%typemap(javain) enum SWIGTYPE "$javainput"
%typemap(javaout) enum SWIGTYPE {
return $jnicall;
}
%typemap(javaclassmodifiers) enum SWIGTYPE ""
%typemap(javagetcptr) enum SWIGTYPE ""
%javaenum(simple);

View file

@ -0,0 +1,68 @@
/* -----------------------------------------------------------------------------
* Include this file in order for C/C++ enums to be wrapped by the so called
* typesafe enum pattern. Each enum has an equivalent Java class named after the
* enum and each enum item is a static instance of this class.
* ----------------------------------------------------------------------------- */
%typemap(jni) enum SWIGTYPE "jint"
%typemap(jtype) enum SWIGTYPE "int"
%typemap(jstype) enum SWIGTYPE "$javaclassname"
%typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
%typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %}
%typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;"
%typemap(javadirectorin) enum SWIGTYPE "$jniinput"
%typemap(javadirectorout) enum SWIGTYPE "$javacall"
%typecheck(SWIG_TYPECHECK_INT32) enum SWIGTYPE ""
%typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
%typemap(javaout) enum SWIGTYPE {
return $javaclassname.swigToEnum($jnicall);
}
%typemap(throws) enum SWIGTYPE {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown");
}
// '$static' will be replaced with either 'static' or nothing depending on whether the enum is an inner Java class or not
%typemap(javaclassmodifiers) enum SWIGTYPE "public $static class"
/*
* The swigToEnum method is used to find the Java enum from a C++ enum integer value. The default one here takes
* advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
* values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
* written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
* The special variable, $enumvalues, is replaced with a comma separated list of all the enum values.
*/
%typemap(javagetcptr) enum SWIGTYPE %{
public final int swigValue() {
return swigValue;
}
public static $javaclassname swigToEnum(int swigValue) {
if (swigValue < swigValues.length && swigValues[swigValue].swigValue == swigValue)
return swigValues[swigValue];
for (int i = 0; i<swigValues.length; i++)
if (swigValues[i].swigValue == swigValue)
return swigValues[i];
throw new IllegalArgumentException("No enum " + $javaclassname.class + " with value " + swigValue);
}
private $javaclassname() {
this.swigValue = next++;
}
private $javaclassname(int swigValue) {
this.swigValue = swigValue;
next = swigValue+1;
}
private static $javaclassname[] swigValues = { $enumvalues };
private static int next = 0;
private final int swigValue;
%}
%javaenum(typesafe);

View file

@ -0,0 +1,40 @@
/* -----------------------------------------------------------------------------
* Include this file in order for C/C++ enums to be wrapped by integers values.
* Each enum has an equivalent class named after the enum and the enum items are
* wrapped by constant integers within this class. The enum items are not
* typesafe as they are all integers.
* ----------------------------------------------------------------------------- */
%typemap(jni) enum SWIGTYPE "jint"
%typemap(jtype) enum SWIGTYPE "int"
%typemap(jstype) enum SWIGTYPE "int"
%typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
%typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %}
%typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;"
%typemap(javadirectorin) enum SWIGTYPE "$jniinput"
%typemap(javadirectorout) enum SWIGTYPE "$javacall"
%typecheck(SWIG_TYPECHECK_INT32) enum SWIGTYPE ""
%typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
%typemap(javaout) enum SWIGTYPE {
return $javaclassname.swigToEnum($jnicall);
}
%typemap(throws) enum SWIGTYPE {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown");
}
%typemap(javain) enum SWIGTYPE "$javainput"
%typemap(javaout) enum SWIGTYPE {
return $jnicall;
}
// '$static' will be replaced with either 'static' or nothing depending on whether the enum is an inner Java class or not
%typemap(javaclassmodifiers) enum SWIGTYPE "public $static class"
%typemap(javagetcptr) enum SWIGTYPE ""
%javaenum(typeunsafe);

View file

@ -146,10 +146,6 @@
%typemap(jtype) SWIGTYPE & "long"
%typemap(jstype) SWIGTYPE & "$javaclassname"
%typemap(jni) enum SWIGTYPE "jint"
%typemap(jtype) enum SWIGTYPE "int"
%typemap(jstype) enum SWIGTYPE "int"
/* pointer to a class member */
%typemap(jni) SWIGTYPE (CLASS::*) "jlong"
%typemap(jtype) SWIGTYPE (CLASS::*) "long"
@ -175,8 +171,7 @@
unsigned long,
long long,
float,
double,
enum SWIGTYPE
double
%{ $1 = ($1_ltype)$input; %}
%typemap(directorin, descriptor="Z") bool "$input = (jboolean) $1;"
@ -192,7 +187,6 @@
%typemap(directorin, descriptor="J") long long "$input = (jlong) $1;"
%typemap(directorin, descriptor="F") float "$input = (jfloat) $1;"
%typemap(directorin, descriptor="D") double "$input = (jdouble) $1;"
%typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;"
%typemap(javadirectorin) char,
signed char,
@ -205,8 +199,7 @@
unsigned long,
long long,
float,
double,
enum SWIGTYPE
double
"$jniinput"
%typemap(javadirectorout) char,
@ -220,8 +213,7 @@
unsigned long,
long long,
float,
double,
enum SWIGTYPE
double
"$javacall"
%typemap(out) bool %{ $result = (jboolean)$1; %}
@ -237,7 +229,6 @@
%typemap(out) long long %{ $result = (jlong)$1; %}
%typemap(out) float %{ $result = (jfloat)$1; %}
%typemap(out) double %{ $result = (jdouble)$1; %}
%typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %}
/* unsigned long long */
/* Convert from BigInteger using the toByteArray member function */
@ -681,8 +672,7 @@
long,
const unsigned short &,
const int &,
const long &,
enum SWIGTYPE
const long &
""
%typecheck(SWIG_TYPECHECK_INT64) /* Java long */
@ -806,8 +796,7 @@
float, const float &,
double, const double &,
char *,
char[ANY],
enum SWIGTYPE
char[ANY]
"$javainput"
%typemap(javain) jboolean,
jchar,
@ -849,8 +838,7 @@
float, const float &,
double, const double &,
char *,
char[ANY],
enum SWIGTYPE {
char[ANY] {
return $jnicall;
}
%typemap(javaout) jboolean,
@ -927,9 +915,9 @@
/* Java specific directives */
#define %javaconst(flag) %feature("java:const","flag")
#define %javaenum(wrapapproach) %feature("java:enum","wrapapproach")
#define %javamethodmodifiers %feature("java:methodmodifiers")
/* Some ANSI C typemaps */
%apply long { size_t };
@ -937,3 +925,7 @@
/* java keywords */
/* please test and activate */
//%include "javakw.swg"
// Default enum handling
%include "enumtypesafe.swg"