/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
7// const enum SWIGTYPE & typemaps
8%typemap(ctype) const enum SWIGTYPE & "int"
9%typemap(imtype) const enum SWIGTYPE & "int"
10%typemap(cstype) const enum SWIGTYPE & "$*csclassname"
11
12%typemap(in) const enum SWIGTYPE & ($*1_ltype temp)
13%{ temp = ($*1_ltype)$input;
14 $1 = &temp; %}
15%typemap(out) const enum SWIGTYPE & %{ $result = *$1; %}
16
17%typecheck(SWIG_TYPECHECK_POINTER) const enum SWIGTYPE & ""
18
19%typemap(throws, canthrow=1) const enum SWIGTYPE & %{
20 (void)$1;
21 SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "C++ $1_type exception thrown");
22 return $null;
23%}
24
25%typemap(csin) const enum SWIGTYPE & "$csinput.swigValue"
26%typemap(csout, excode=SWIGEXCODE) const enum SWIGTYPE & {
27 $*csclassname ret = $*csclassname.swigToEnum($imcall);$excode
28 return ret;
29 }
30
31%typemap(csvarout, excode=SWIGEXCODE2) const enum SWIGTYPE & %{
32 get {
33 $*csclassname ret = $*csclassname.swigToEnum($imcall);$excode
34 return ret;
35 } %}
36
37
38// enum SWIGTYPE typemaps
39%typemap(ctype) enum SWIGTYPE "int"
40%typemap(imtype) enum SWIGTYPE "int"
41%typemap(cstype) enum SWIGTYPE "$csclassname"
42
43%typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
44%typemap(out) enum SWIGTYPE %{ $result = $1; %}
45
46%typecheck(SWIG_TYPECHECK_POINTER) enum SWIGTYPE ""
47
48%typemap(throws, canthrow=1) enum SWIGTYPE %{
49 (void)$1;
50 SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "C++ $1_type exception thrown");
51 return $null;
52%}
53
54%typemap(csin) enum SWIGTYPE "$csinput.swigValue"
55%typemap(csout, excode=SWIGEXCODE) enum SWIGTYPE {
56 $csclassname ret = $csclassname.swigToEnum($imcall);$excode
57 return ret;
58 }
59
60%typemap(csvarout, excode=SWIGEXCODE2) enum SWIGTYPE %{
61 get {
62 $csclassname ret = $csclassname.swigToEnum($imcall);$excode
63 return ret;
64 } %}
65
66%typemap(csbase) enum SWIGTYPE ""
67%typemap(csclassmodifiers) enum SWIGTYPE "public sealed class"
68%typemap(cscode) enum SWIGTYPE ""
69%typemap(csimports) enum SWIGTYPE ""
70%typemap(csinterfaces) enum SWIGTYPE ""
71
72/*
73 * The swigToEnum method is used to find the C# enum from a C++ enum integer value. The default one here takes
74 * advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
75 * values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
76 * written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
77 * The special variable, $enumvalues, is replaced with a comma separated list of all the enum values.
78 */
79%typemap(csbody) enum SWIGTYPE %{
80 public readonly int swigValue;
81
82 public static $csclassname swigToEnum(int swigValue) {
83 if (swigValue < swigValues.Length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
84 return swigValues[swigValue];
85 for (int i = 0; i < swigValues.Length; i++)
86 if (swigValues[i].swigValue == swigValue)
87 return swigValues[i];
88 throw new System.ArgumentOutOfRangeException("No enum $csclassname with value " + swigValue);
89 }
90
91 public override string ToString() {
92 return swigName;
93 }
94
95 private $csclassname(string swigName) {
96 this.swigName = swigName;
97 this.swigValue = swigNext++;
98 }
99
100 private $csclassname(string swigName, int swigValue) {
101 this.swigName = swigName;
102 this.swigValue = swigValue;
103 swigNext = swigValue+1;
104 }
105
106 private static $csclassname[] swigValues = { $enumvalues };
107 private static int swigNext = 0;
108 private readonly string swigName;
109%}
110
111%csenum(typesafe);
112