1 %module special_variable_attributes
2 
3 // Special variable expansion and special variable macros, aka embedded typemaps - expansion tests
4 // Tests these are expanded within typemap attributes.
5 // Also tests that these are expanded when used together, so that the special variables
6 // can be used as the type passed to the special variable macro.
7 // C# is used for testing as it is one of the few languages that uses a lot of typemap attributes.
8 // Attributes in both 'in' and 'out' typemaps are needed, that is,
9 // typemaps targeting both parameters and return values respectively).
10 
11 #ifdef SWIGCSHARP
12 // Check special variable expansion in typemap attributes.
13 // This changes return by reference into return by value.
14 // Takes advantage of the fact that 'int' is a valid type in both C and C#.
15 // This is not a realistic use, just a way to test the variable expansion in the 'out' attribute.
16 %typemap(ctype, out="$*1_ltype") int& getNumber1 "_not_used_"
17 %typemap(imtype, out="$*1_ltype") int& getNumber1 "_not_used_"
18 %typemap(cstype, out="$*1_ltype") int& getNumber1 "_not_used_"
19 %typemap(out) int& getNumber1 "$result = *$1;"
20 %typemap(csout, excode=SWIGEXCODE) int & getNumber1 {
21     int ret = $imcall;$excode
22     return ret;
23   }
24 #endif
25 
26 %inline %{
getNumber1()27 int& getNumber1() {
28   static int num = 111;
29   return num;
30 }
31 %}
32 
33 #ifdef SWIGCSHARP
34 // Check special variable macro expansion in typemap attributes.
35 // This changes return by reference into return by value.
36 %typemap(ctype, out="$typemap(ctype, int)") int& getNumber2 "_not_used_"
37 %typemap(imtype, out="$typemap(imtype, int)") int& getNumber2 "_not_used_"
38 %typemap(cstype, out="$typemap(cstype, int)") int& getNumber2 "_not_used_"
39 %typemap(out) int& getNumber2 "$result = *$1;"
40 %typemap(csout, excode=SWIGEXCODE) int & getNumber2 {
41     int ret = $imcall;$excode
42     return ret;
43   }
44 #endif
45 
46 %inline %{
getNumber2()47 int& getNumber2() {
48   static int num = 222;
49   return num;
50 }
51 %}
52 
53 
54 #ifdef SWIGCSHARP
55 // Check special variable macro expansion and special variable expansion in typemap attributes.
56 // This changes return by reference into return by value.
57 %typemap(ctype, out="$typemap(ctype, $*1_ltype)") int& getNumber3 "_not_used_"
58 %typemap(imtype, out="$typemap(imtype, $*1_ltype)") int& getNumber3 "_not_used_"
59 %typemap(cstype, out="$typemap(cstype, $*1_ltype)") int& getNumber3 "_not_used_"
60 %typemap(out) int& getNumber3 "$result = *$1;"
61 %typemap(csout, excode=SWIGEXCODE) int & getNumber3 {
62     int ret = $imcall;$excode
63     return ret;
64   }
65 #endif
66 
67 %inline %{
getNumber3()68 int& getNumber3() {
69   static int num = 333;
70   return num;
71 }
72 %}
73 
74 #ifdef SWIGCSHARP
75 // Check special variable macro expansion in typemap attributes.
76 %typemap(csin,
77          pre="    $typemap(cstype, int) $csinput_scaled = 11;"
78         ) int num1
79 %{$csinput * $csinput_scaled %}
80 #endif
81 
82 %inline %{
bounceNumber1(int num1)83 int bounceNumber1(int num1) {
84   return num1;
85 }
86 %}
87 
88 #ifdef SWIGCSHARP
89 // Check special variable expansion in typemap attributes.
90 %typemap(csin,
91          pre="    $1_type $csinput_scaled = 22;"
92         ) int num2
93 %{$csinput * $csinput_scaled %}
94 #endif
95 
96 %inline %{
bounceNumber2(int num2)97 int bounceNumber2(int num2) {
98   return num2;
99 }
100 %}
101 
102 #ifdef SWIGCSHARP
103 // Check special variable and special variable macro expansion in typemap attributes.
104 %typemap(csin,
105          pre="    $typemap(cstype, $1_type) $csinput_scaled = 33;"
106         ) int num3
107 %{$csinput * $csinput_scaled %}
108 #endif
109 
110 %inline %{
bounceNumber3(int num3)111 int bounceNumber3(int num3) {
112   return num3;
113 }
114 %}
115 
116 /////////////////////////////////
117 //// Multi-argument typemaps ////
118 /////////////////////////////////
119 
120 // Test expansion of special variables
121 #ifdef SWIGCSHARP
122 %typemap(ctype) (int intvar, char charvar) "double"
123 %typemap(imtype) (int intvar, char charvar) "double"
124 %typemap(cstype) (int intvar, char charvar) "double"
125 %typemap(in) (int intvar, char charvar)
126 %{
127   // split double value a.b into two numbers, a and b*100
128   $1 = (int)$input;
129   $2 = (char)(($input - $1 + 0.005) * 100);
130 %}
131 %typemap(csin,
132          pre="    $1_type $csinput_$1_type = 50;\n" // $1_type should expand to int
133              "    $2_type $csinput_$2_type = 'A';"  // $2_type should expand to char
134         ) (int intvar, char charvar)
135 %{$csinput + ($csinput_int - 50 + $csinput_char - 'A') + ($csinput_$1_type - 50 + $csinput_$2_type - 'A')%}
136 #endif
137 
138 %inline %{
multi1(int intvar,char charvar)139 int multi1(int intvar, char charvar) {
140   return intvar + charvar;
141 }
142 %}
143 
144 #ifdef SWIGCSHARP
145 %typemap(csin,
146          pre="    $typemap(cstype, int) $csinput_$typemap(cstype, int) = 50;\n" // also should expand to int
147              "    $typemap(cstype, char) $csinput_$typemap(cstype, char) = 'A';"  // also should expand to char
148         ) (int intvar, char charvar)
149 %{55 + $csinput + ($csinput_int - 50 + $csinput_char - 'A') + ($csinput_$typemap(cstype, int) - 50 + $csinput_$typemap(cstype, char) - 'A')%}
150 #endif
151 
152 %inline %{
multi2(int intvar,char charvar)153 int multi2(int intvar, char charvar) {
154   return intvar + charvar;
155 }
156 %}
157 
158 #ifdef SWIGCSHARP
159 %typemap(csin,
160          pre="    $typemap(cstype, $1_type) $csinput_$typemap(cstype, $1_type) = 50;\n" // also should expand to int
161              "    $typemap(cstype, $2_type) $csinput_$typemap(cstype, $2_type) = 'A';"  // also should expand to char
162         ) (int intvar, char charvar)
163 %{77 + $csinput + ($csinput_int - 50 + $csinput_char - 'A') + ($csinput_$typemap(cstype, $1_type) - 50 + $csinput_$typemap(cstype, $2_type) - 'A')%}
164 #endif
165 
166 %inline %{
multi3(int intvar,char charvar)167 int multi3(int intvar, char charvar) {
168   return intvar + charvar;
169 }
170 %}
171