PageRenderTime 303ms CodeModel.GetById 44ms RepoModel.GetById 7ms app.codeStats 0ms

/tags/rel-1-3-27/SWIG/Lib/csharp/enumtypesafe.swg

#
Unknown | 112 lines | 90 code | 22 blank | 0 comment | 0 complexity | 1b7cd0af650e65682367604c5a37d94b MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * Include this file in order for C/C++ enums to be wrapped by the so called
  3. * typesafe enum pattern. Each enum has an equivalent C# class named after the
  4. * enum and each enum item is a static instance of this class.
  5. * ----------------------------------------------------------------------------- */
  6. // const enum SWIGTYPE & typemaps
  7. %typemap(ctype) const enum SWIGTYPE & "int"
  8. %typemap(imtype) const enum SWIGTYPE & "int"
  9. %typemap(cstype) const enum SWIGTYPE & "$*csclassname"
  10. %typemap(in) const enum SWIGTYPE & ($*1_ltype temp)
  11. %{ temp = ($*1_ltype)$input;
  12. $1 = &temp; %}
  13. %typemap(out) const enum SWIGTYPE & %{ $result = *$1; %}
  14. %typecheck(SWIG_TYPECHECK_POINTER) const enum SWIGTYPE & ""
  15. %typemap(throws, canthrow=1) const enum SWIGTYPE & %{
  16. (void)$1;
  17. SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "C++ $1_type exception thrown");
  18. return $null;
  19. %}
  20. %typemap(csin) const enum SWIGTYPE & "$csinput.swigValue"
  21. %typemap(csout, excode=SWIGEXCODE) const enum SWIGTYPE & {
  22. $*csclassname ret = $*csclassname.swigToEnum($imcall);$excode
  23. return ret;
  24. }
  25. %typemap(csvarout, excode=SWIGEXCODE2) const enum SWIGTYPE & %{
  26. get {
  27. $*csclassname ret = $*csclassname.swigToEnum($imcall);$excode
  28. return ret;
  29. } %}
  30. // enum SWIGTYPE typemaps
  31. %typemap(ctype) enum SWIGTYPE "int"
  32. %typemap(imtype) enum SWIGTYPE "int"
  33. %typemap(cstype) enum SWIGTYPE "$csclassname"
  34. %typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
  35. %typemap(out) enum SWIGTYPE %{ $result = $1; %}
  36. %typecheck(SWIG_TYPECHECK_POINTER) enum SWIGTYPE ""
  37. %typemap(throws, canthrow=1) enum SWIGTYPE %{
  38. (void)$1;
  39. SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "C++ $1_type exception thrown");
  40. return $null;
  41. %}
  42. %typemap(csin) enum SWIGTYPE "$csinput.swigValue"
  43. %typemap(csout, excode=SWIGEXCODE) enum SWIGTYPE {
  44. $csclassname ret = $csclassname.swigToEnum($imcall);$excode
  45. return ret;
  46. }
  47. %typemap(csvarout, excode=SWIGEXCODE2) enum SWIGTYPE %{
  48. get {
  49. $csclassname ret = $csclassname.swigToEnum($imcall);$excode
  50. return ret;
  51. } %}
  52. %typemap(csbase) enum SWIGTYPE ""
  53. %typemap(csclassmodifiers) enum SWIGTYPE "public sealed class"
  54. %typemap(cscode) enum SWIGTYPE ""
  55. %typemap(csimports) enum SWIGTYPE ""
  56. %typemap(csinterfaces) enum SWIGTYPE ""
  57. /*
  58. * The swigToEnum method is used to find the C# enum from a C++ enum integer value. The default one here takes
  59. * advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
  60. * values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
  61. * written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
  62. * The special variable, $enumvalues, is replaced with a comma separated list of all the enum values.
  63. */
  64. %typemap(csbody) enum SWIGTYPE %{
  65. public readonly int swigValue;
  66. public static $csclassname swigToEnum(int swigValue) {
  67. if (swigValue < swigValues.Length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
  68. return swigValues[swigValue];
  69. for (int i = 0; i < swigValues.Length; i++)
  70. if (swigValues[i].swigValue == swigValue)
  71. return swigValues[i];
  72. throw new System.ArgumentOutOfRangeException("No enum $csclassname with value " + swigValue);
  73. }
  74. public override string ToString() {
  75. return swigName;
  76. }
  77. private $csclassname(string swigName) {
  78. this.swigName = swigName;
  79. this.swigValue = swigNext++;
  80. }
  81. private $csclassname(string swigName, int swigValue) {
  82. this.swigName = swigName;
  83. this.swigValue = swigValue;
  84. swigNext = swigValue+1;
  85. }
  86. private static $csclassname[] swigValues = { $enumvalues };
  87. private static int swigNext = 0;
  88. private readonly string swigName;
  89. %}
  90. %csenum(typesafe);