PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Lib/java/enumtypesafe.swg

#
Unknown | 118 lines | 96 code | 22 blank | 0 comment | 0 complexity | e4825ee367ba7124f08dc5a5d39eefa1 MD5 | raw file
  1/* -----------------------------------------------------------------------------
  2 * enumtypesafe.swg
  3 *
  4 * Include this file in order for C/C++ enums to be wrapped by the so called
  5 * typesafe enum pattern. Each enum has an equivalent Java class named after the
  6 * enum and each enum item is a static instance of this class.
  7 * ----------------------------------------------------------------------------- */
  8
  9// const enum SWIGTYPE & typemaps
 10%typemap(jni) const enum SWIGTYPE & "jint"
 11%typemap(jtype) const enum SWIGTYPE & "int"
 12%typemap(jstype) const enum SWIGTYPE & "$*javaclassname"
 13
 14%typemap(in) const enum SWIGTYPE & ($*1_ltype temp)
 15%{ temp = ($*1_ltype)$input; 
 16   $1 = &temp; %}
 17%typemap(out) const enum SWIGTYPE &  %{ $result = (jint)*$1; %}
 18
 19%typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const enum SWIGTYPE &
 20%{ static $*1_ltype temp = ($*1_ltype)$input; 
 21   $result = &temp; %}
 22%typemap(directorin, descriptor="L$packagepath/$*javaclassname;") const enum SWIGTYPE & "$input = (jint)$1;"
 23%typemap(javadirectorin) const enum SWIGTYPE & "$*javaclassname.swigToEnum($jniinput)"
 24%typemap(javadirectorout) const enum SWIGTYPE & "($javacall).swigValue()"
 25
 26%typecheck(SWIG_TYPECHECK_POINTER) const enum SWIGTYPE & ""
 27
 28%typemap(throws) const enum SWIGTYPE &
 29%{ (void)$1;
 30   SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown"); %}
 31
 32%typemap(javain) const enum SWIGTYPE & "$javainput.swigValue()"
 33%typemap(javaout) const enum SWIGTYPE & {
 34    return $*javaclassname.swigToEnum($jnicall);
 35  }
 36
 37// enum SWIGTYPE typemaps
 38%typemap(jni) enum SWIGTYPE "jint"
 39%typemap(jtype) enum SWIGTYPE "int"
 40%typemap(jstype) enum SWIGTYPE "$javaclassname"
 41
 42%typemap(in) enum SWIGTYPE  %{ $1 = ($1_ltype)$input; %}
 43%typemap(out) enum SWIGTYPE  %{ $result = (jint)$1; %}
 44
 45%typemap(directorout) enum SWIGTYPE  %{ $result = ($1_ltype)$input; %}
 46%typemap(directorin, descriptor="L$packagepath/$javaclassname;") enum SWIGTYPE "$input = (jint) $1;"
 47%typemap(javadirectorin) enum SWIGTYPE "$javaclassname.swigToEnum($jniinput)"
 48%typemap(javadirectorout) enum SWIGTYPE "($javacall).swigValue()"
 49
 50%typecheck(SWIG_TYPECHECK_POINTER) enum SWIGTYPE ""
 51
 52%typemap(throws) enum SWIGTYPE
 53%{ (void)$1;
 54   SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown"); %}
 55
 56%typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
 57%typemap(javaout) enum SWIGTYPE {
 58    return $javaclassname.swigToEnum($jnicall);
 59  }
 60
 61// '$static' will be replaced with either 'static' or nothing depending on whether the enum is an inner Java class or not
 62%typemap(javaclassmodifiers) enum SWIGTYPE "public final $static class"
 63%typemap(javabase)           enum SWIGTYPE ""
 64%typemap(javacode)           enum SWIGTYPE ""
 65%typemap(javaimports)        enum SWIGTYPE ""
 66%typemap(javainterfaces)     enum SWIGTYPE ""
 67%typemap(javabody)           enum SWIGTYPE ""
 68
 69/*
 70 * The swigToEnum method is used to find the Java enum from a C++ enum integer value. The default one here takes 
 71 * advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
 72 * values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
 73 * written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
 74 * The special variable, $enumvalues, is replaced with a comma separated list of all the enum values.
 75 */
 76%typemap(javabody) enum SWIGTYPE %{
 77  public final int swigValue() {
 78    return swigValue;
 79  }
 80
 81  public String toString() {
 82    return swigName;
 83  }
 84
 85  public static $javaclassname swigToEnum(int swigValue) {
 86    if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
 87      return swigValues[swigValue];
 88    for (int i = 0; i < swigValues.length; i++)
 89      if (swigValues[i].swigValue == swigValue)
 90        return swigValues[i];
 91    throw new IllegalArgumentException("No enum " + $javaclassname.class + " with value " + swigValue);
 92  }
 93
 94  private $javaclassname(String swigName) {
 95    this.swigName = swigName;
 96    this.swigValue = swigNext++;
 97  }
 98
 99  private $javaclassname(String swigName, int swigValue) {
100    this.swigName = swigName;
101    this.swigValue = swigValue;
102    swigNext = swigValue+1;
103  }
104
105  private $javaclassname(String swigName, $javaclassname swigEnum) {
106    this.swigName = swigName;
107    this.swigValue = swigEnum.swigValue;
108    swigNext = this.swigValue+1;
109  }
110
111  private static $javaclassname[] swigValues = { $enumvalues };
112  private static int swigNext = 0;
113  private final int swigValue;
114  private final String swigName;
115%}
116
117%javaenum(typesafe);
118