import source from 1.3.40
[external/swig.git] / Lib / csharp / enumtypesafe.swg
1 /* -----------------------------------------------------------------------------
2  * See the LICENSE file for information on copyright, usage and redistribution
3  * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4  *
5  * enumtypesafe.swg
6  *
7  * Include this file in order for C/C++ enums to be wrapped by the so called
8  * typesafe enum pattern. Each enum has an equivalent C# class named after the
9  * enum and each enum item is a static instance of this class.
10  * ----------------------------------------------------------------------------- */
11
12 // const enum SWIGTYPE & typemaps
13 %typemap(ctype) const enum SWIGTYPE & "int"
14 %typemap(imtype) const enum SWIGTYPE & "int"
15 %typemap(cstype) const enum SWIGTYPE & "$*csclassname"
16
17 %typemap(in) const enum SWIGTYPE & ($*1_ltype temp)
18 %{ temp = ($*1_ltype)$input; 
19    $1 = &temp; %}
20 %typemap(out) const enum SWIGTYPE & %{ $result = *$1; %}
21
22 %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const enum SWIGTYPE &
23 %{ static $*1_ltype temp = ($*1_ltype)$input; 
24    $result = &temp; %}
25 %typemap(directorin) const enum SWIGTYPE &    "$input = $1_name;"
26 %typemap(csdirectorin) const enum SWIGTYPE & "$*csclassname.swigToEnum($iminput)"
27 %typemap(csdirectorout) const enum SWIGTYPE & "$cscall.swigValue"
28
29 %typecheck(SWIG_TYPECHECK_POINTER) const enum SWIGTYPE & ""
30
31 %typemap(throws, canthrow=1) const enum SWIGTYPE &
32 %{ (void)$1;
33    SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "C++ $1_type exception thrown");
34    return $null; %}
35
36 %typemap(csin) const enum SWIGTYPE & "$csinput.swigValue"
37 %typemap(csout, excode=SWIGEXCODE) const enum SWIGTYPE & {
38     $*csclassname ret = $*csclassname.swigToEnum($imcall);$excode
39     return ret;
40   }
41
42 %typemap(csvarout, excode=SWIGEXCODE2) const enum SWIGTYPE & %{
43     get {
44       $*csclassname ret = $*csclassname.swigToEnum($imcall);$excode
45       return ret;
46     } %}
47
48
49 // enum SWIGTYPE typemaps
50 %typemap(ctype) enum SWIGTYPE "int"
51 %typemap(imtype) enum SWIGTYPE "int"
52 %typemap(cstype) enum SWIGTYPE "$csclassname"
53
54 %typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %}
55 %typemap(out) enum SWIGTYPE %{ $result = $1; %}
56
57 %typemap(directorout) enum SWIGTYPE  %{ $result = ($1_ltype)$input; %}
58 %typemap(directorin) enum SWIGTYPE    "$input = $1;"
59 %typemap(csdirectorin) enum SWIGTYPE "$csclassname.swigToEnum($iminput)"
60 %typemap(csdirectorout) enum SWIGTYPE "$cscall.swigValue"
61
62 %typecheck(SWIG_TYPECHECK_POINTER) enum SWIGTYPE ""
63
64 %typemap(throws, canthrow=1) enum SWIGTYPE
65 %{ (void)$1;
66    SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "C++ $1_type exception thrown");
67    return $null; %}
68
69 %typemap(csin) enum SWIGTYPE "$csinput.swigValue"
70 %typemap(csout, excode=SWIGEXCODE) enum SWIGTYPE {
71     $csclassname ret = $csclassname.swigToEnum($imcall);$excode
72     return ret;
73   }
74
75 %typemap(csvarout, excode=SWIGEXCODE2) enum SWIGTYPE %{
76     get {
77       $csclassname ret = $csclassname.swigToEnum($imcall);$excode
78       return ret;
79     } %}
80
81 %typemap(csbase)           enum SWIGTYPE ""
82 %typemap(csclassmodifiers) enum SWIGTYPE "public sealed class"
83 %typemap(cscode)           enum SWIGTYPE ""
84 %typemap(csimports)        enum SWIGTYPE ""
85 %typemap(csinterfaces)     enum SWIGTYPE ""
86
87 /*
88  * The swigToEnum method is used to find the C# enum from a C++ enum integer value. The default one here takes 
89  * advantage of the fact that most enums do not have initial values specified, so the lookup is fast. If initial
90  * values are specified then a lengthy linear search through all possible enums might occur. Specific typemaps could be
91  * written to possibly optimise this lookup by taking advantage of characteristics peculiar to the targeted enum.
92  * The special variable, $enumvalues, is replaced with a comma separated list of all the enum values.
93  */
94 %typemap(csbody) enum SWIGTYPE %{
95   public readonly int swigValue;
96
97   public static $csclassname swigToEnum(int swigValue) {
98     if (swigValue < swigValues.Length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue)
99       return swigValues[swigValue];
100     for (int i = 0; i < swigValues.Length; i++)
101       if (swigValues[i].swigValue == swigValue)
102         return swigValues[i];
103     throw new System.ArgumentOutOfRangeException("No enum $csclassname with value " + swigValue);
104   }
105
106   public override string ToString() {
107     return swigName;
108   }
109
110   private $csclassname(string swigName) {
111     this.swigName = swigName;
112     this.swigValue = swigNext++;
113   }
114
115   private $csclassname(string swigName, int swigValue) {
116     this.swigName = swigName;
117     this.swigValue = swigValue;
118     swigNext = swigValue+1;
119   }
120
121   private $csclassname(string swigName, $csclassname swigEnum) {
122     this.swigName = swigName;
123     this.swigValue = swigEnum.swigValue;
124     swigNext = this.swigValue+1;
125   }
126
127   private static $csclassname[] swigValues = { $enumvalues };
128   private static int swigNext = 0;
129   private readonly string swigName;
130 %}
131
132 %csenum(typesafe);
133