Home | History | Annotate | Download | only in java
      1 /* -----------------------------------------------------------------------------
      2  * enums.swg
      3  *
      4  * Include this file in order for C/C++ enums to be wrapped by proper Java enums.
      5  * Note that the JNI layer handles the enum as an int. The Java enum has extra
      6  * code generated to store the C++ int value. This is required for C++ enums that
      7  * specify a value for the enum item, as native Java enums do not support this.
      8  * ----------------------------------------------------------------------------- */
      9 
     10 // const enum SWIGTYPE & typemaps
     11 %typemap(jni) const enum SWIGTYPE & "jint"
     12 %typemap(jtype) const enum SWIGTYPE & "int"
     13 %typemap(jstype) const enum SWIGTYPE & "$*javaclassname"
     14 
     15 %typemap(in) const enum SWIGTYPE & ($*1_ltype temp)
     16 %{ temp = ($*1_ltype)$input;
     17    $1 = &temp; %}
     18 %typemap(out) const enum SWIGTYPE &  %{ $result = (jint)*$1; %}
     19 
     20 %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const enum SWIGTYPE &
     21 %{ static $*1_ltype temp = ($*1_ltype)$input;
     22    $result = &temp; %}
     23 %typemap(directorin, descriptor="L$packagepath/$*javaclassname;") const enum SWIGTYPE & "$input = (jint)$1;"
     24 %typemap(javadirectorin) const enum SWIGTYPE & "$*javaclassname.swigToEnum($jniinput)"
     25 %typemap(javadirectorout) const enum SWIGTYPE & "($javacall).swigValue()"
     26 
     27 %typecheck(SWIG_TYPECHECK_POINTER) const enum SWIGTYPE & ""
     28 
     29 %typemap(throws) const enum SWIGTYPE &
     30 %{ (void)$1;
     31    SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown"); %}
     32 
     33 %typemap(javain) const enum SWIGTYPE & "$javainput.swigValue()"
     34 %typemap(javaout) const enum SWIGTYPE & {
     35     return $*javaclassname.swigToEnum($jnicall);
     36   }
     37 
     38 
     39 // enum SWIGTYPE typemaps
     40 %typemap(jni) enum SWIGTYPE "jint"
     41 %typemap(jtype) enum SWIGTYPE "int"
     42 %typemap(jstype) enum SWIGTYPE "$javaclassname"
     43 
     44 %typemap(in) enum SWIGTYPE  %{ $1 = ($1_ltype)$input; %}
     45 %typemap(out) enum SWIGTYPE  %{ $result = (jint)$1; %}
     46 
     47 %typemap(directorout) enum SWIGTYPE  %{ $result = ($1_ltype)$input; %}
     48 %typemap(directorin, descriptor="L$packagepath/$javaclassname;") enum SWIGTYPE "$input = (jint) $1;"
     49 %typemap(javadirectorin) enum SWIGTYPE "$javaclassname.swigToEnum($jniinput)"
     50 %typemap(javadirectorout) enum SWIGTYPE "($javacall).swigValue()"
     51 
     52 %typecheck(SWIG_TYPECHECK_POINTER) enum SWIGTYPE ""
     53 
     54 %typemap(throws) enum SWIGTYPE
     55 %{ (void)$1;
     56    SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "C++ $1_type exception thrown"); %}
     57 
     58 %typemap(javain) enum SWIGTYPE "$javainput.swigValue()"
     59 %typemap(javaout) enum SWIGTYPE {
     60     return $javaclassname.swigToEnum($jnicall);
     61   }
     62 
     63 %typemap(javaclassmodifiers) enum SWIGTYPE "public enum"
     64 %typemap(javabase)           enum SWIGTYPE ""
     65 %typemap(javacode)           enum SWIGTYPE ""
     66 %typemap(javaimports)        enum SWIGTYPE ""
     67 %typemap(javainterfaces)     enum SWIGTYPE ""
     68 %typemap(javabody)           enum SWIGTYPE ""
     69 
     70 /*
     71  * SwigNext static inner class used instead of a static int as static fields cannot be accessed from enum initialisers.
     72  * The swigToEnum method is used to find the Java enum from a C++ enum integer value. The default one here takes
     73  * advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
     74  * values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
     75  * written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
     76  */
     77 %typemap(javabody) enum SWIGTYPE %{
     78   public final int swigValue() {
     79     return swigValue;
     80   }
     81 
     82   public static $javaclassname swigToEnum(int swigValue) {
     83     $javaclassname[] swigValues = $javaclassname.class.getEnumConstants();
     84     if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
     85       return swigValues[swigValue];
     86     for ($javaclassname swigEnum : swigValues)
     87       if (swigEnum.swigValue == swigValue)
     88         return swigEnum;
     89     throw new IllegalArgumentException("No enum " + $javaclassname.class + " with value " + swigValue);
     90   }
     91 
     92   @SuppressWarnings("unused")
     93   private $javaclassname() {
     94     this.swigValue = SwigNext.next++;
     95   }
     96 
     97   @SuppressWarnings("unused")
     98   private $javaclassname(int swigValue) {
     99     this.swigValue = swigValue;
    100     SwigNext.next = swigValue+1;
    101   }
    102 
    103   @SuppressWarnings("unused")
    104   private $javaclassname($javaclassname swigEnum) {
    105     this.swigValue = swigEnum.swigValue;
    106     SwigNext.next = this.swigValue+1;
    107   }
    108 
    109   private final int swigValue;
    110 
    111   private static class SwigNext {
    112     private static int next = 0;
    113   }
    114 %}
    115 
    116 %javaenum(proper);
    117 
    118