PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/java/enumtypesafe.swg

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