Upstream version 1.3.40
[profile/ivi/swig.git] / Lib / lua / luatypemaps.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  * luatypemaps.swg
6  *
7  * basic typemaps for Lua.
8  * ----------------------------------------------------------------------------- */
9
10 /* -----------------------------------------------------------------------------
11  *                          standard typemaps
12  * ----------------------------------------------------------------------------- */
13 /* NEW LANGUAGE NOTE:
14    the 'checkfn' param is something that I added for typemap(in)
15    it is an optional fn call to check the type of the lua object
16    the fn call must be of the form
17      int checkfn(lua_State *L, int index);
18    and return 1/0 depending upon if this is the correct type
19    For the typemap(out), an additional SWIG_arg parmeter must be incremented
20    to reflect the number of values returned (normally SWIG_arg++; will do)
21 */
22 // numbers
23 %typemap(in,checkfn="lua_isnumber") int, short, long,
24              signed char, float, double
25 %{$1 = ($type)lua_tonumber(L, $input);%}
26  
27 // additional check for unsigned numbers, to not permit negative input
28 %typemap(in,checkfn="lua_isnumber") unsigned int,
29              unsigned short, unsigned long, unsigned char
30 %{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
31 $1 = ($type)lua_tonumber(L, $input);%}
32
33 %typemap(out) int,short,long,
34              unsigned int,unsigned short,unsigned long,
35              signed char,unsigned char,
36              float,double
37 %{  lua_pushnumber(L, (lua_Number) $1); SWIG_arg++;%}
38
39 // we must also provide typemaps for privitives by const reference:
40 // given a function:
41 //      int intbyref(const int& i);
42 // SWIG assumes that this code will need a pointer to int to be passed in
43 // (this might be ok for objects by const ref, but not for numeric primitives)
44 // therefore we add a set of typemaps to fix this (for both in & out)
45 %typemap(in,checkfn="lua_isnumber") const int&($basetype temp)
46 %{ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
47
48 %typemap(in,checkfn="lua_isnumber") const unsigned int&($basetype temp)
49 %{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
50 temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
51
52 %typemap(out) const int&, const unsigned int&
53 %{  lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
54
55 // for the other numbers we can just use an apply statement to cover them
56 %apply const int & {const short&,const long&,const signed char&,
57              const float&,const double&};
58
59 %apply const unsigned int & {const unsigned short&,const unsigned long&,
60              const unsigned char&};
61
62 /* enums have to be handled slightly differently
63         VC++ .net will not allow a cast from lua_Number(double) to enum directly.
64 */
65 %typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
66 %{$1 = ($type)(int)lua_tonumber(L, $input);%}
67
68 %typemap(out) enum SWIGTYPE
69 %{  lua_pushnumber(L, (lua_Number)(int)($1)); SWIG_arg++;%}
70
71 // and const refs
72 %typemap(in,checkfn="lua_isnumber") const enum SWIGTYPE &($basetype temp)
73 %{ temp=($basetype)(int)lua_tonumber(L,$input); $1=&temp;%}
74 %typemap(out) const enum SWIGTYPE &
75 %{  lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
76
77
78 // boolean (which is a special type in lua)
79 // note: lua_toboolean() returns 1 or 0
80 // note: 1 & 0 are not booleans in lua, only true & false
81 %typemap(in,checkfn="lua_isboolean") bool
82 %{$1 = (lua_toboolean(L, $input)!=0);%}
83
84 %typemap(out) bool
85 %{  lua_pushboolean(L,(int)($1!=0)); SWIG_arg++;%}
86
87 // for const bool&, SWIG treats this as a const bool* so we must dereference it
88 %typemap(in,checkfn="lua_isboolean") const bool& (bool temp)
89 %{temp=(lua_toboolean(L, $input)!=0);
90   $1=&temp;%}
91
92 %typemap(out) const bool&
93 %{  lua_pushboolean(L,(int)((*$1)!=0)); SWIG_arg++;%}
94
95 // strings (char* and char[])
96 %typemap(in,checkfn="lua_isstring") const char*, char*
97 %{$1 = ($ltype)lua_tostring(L, $input);%}
98
99 %typemap(in,checkfn="lua_isstring") const char[ANY], char[ANY]
100 %{$1 = ($ltype)lua_tostring(L, $input);%}
101
102 %typemap(out) const char*, char*
103 %{  lua_pushstring(L,(const char*)$1); SWIG_arg++;%}
104
105 %typemap(out) const char[ANY], char[ANY]
106 %{  lua_pushstring(L,(const char*)$1); SWIG_arg++;%}
107
108 // char's
109 // currently treating chars as small strings, not as numbers
110 // (however signed & unsigned char's are numbers...)
111 %typemap(in,checkfn="lua_isstring") char
112 %{$1 = (lua_tostring(L, $input))[0];%}
113
114 %typemap(out) char
115 %{  lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
116
117 // by const ref
118 %typemap(in,checkfn="lua_isstring") const char& (char temp)
119 %{temp = (lua_tostring(L, $input))[0]; $1=&temp;%}
120
121 %typemap(out) const char&
122 %{  lua_pushfstring(L,"%c",*$1); SWIG_arg++;%}
123
124 // pointers and references
125 // under SWIG rules, it is ok, to have a pass in a lua nil,
126 // it should be converted to a SWIG NULL.
127 // This will only be allowed for pointers & arrays, not refs or by value
128 // the checkfn lua_isuserdata will only work for userdata
129 // the checkfn SWIG_isptrtype will work for both userdata and nil's
130 %typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE*,SWIGTYPE[]
131 %{
132   if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,$disown))){
133     SWIG_fail_ptr("$symname",$argnum,$descriptor);
134   }
135 %}
136
137 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE&
138 %{
139   if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,$disown))){
140     SWIG_fail_ptr("$symname",$argnum,$descriptor);
141   }
142 %}
143
144 // out is simple
145 %typemap(out) SWIGTYPE*,SWIGTYPE&
146 %{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
147
148 // dynamic casts
149 // this uses the SWIG_TypeDynamicCast() which relies on RTTI to find out what the pointer really is
150 // the we return it as the correct type
151 %typemap(out) SWIGTYPE *DYNAMIC,
152               SWIGTYPE &DYNAMIC
153 {
154   swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1);
155   SWIG_NewPointerObj(L,(void*)$1,ty,$owner); SWIG_arg++; 
156 }
157
158
159 // passing objects by value
160 // SWIG_ConvertPtr wants an object pointer (the $&ltype argp)
161 // then dereferences it to get the object
162 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE ($&ltype argp)
163 %{
164    if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&argp,$&descriptor,0))){
165      SWIG_fail_ptr("$symname",$argnum,$&descriptor);
166    }
167    $1 = *argp;
168 %}
169
170 // Also needed for object ptrs by const ref
171 // eg A* const& ref_pointer(A* const& a);
172 // found in mixed_types.i
173 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE *&($*ltype temp)
174 %{temp=($*ltype)SWIG_MustGetPtr(L,$input,$*descriptor,0,$argnum,"$symname");
175 $1=&temp;%}
176
177 %typemap(out) SWIGTYPE *&
178 %{SWIG_NewPointerObj(L,*$1,$*descriptor,$owner); SWIG_arg++; %}
179
180
181 // DISOWN-ing typemaps
182 // if you have an object pointer which must be disowned, use this typemap
183 // eg. for void destroy_foo(Foo* toDie);
184 // use %apply SWIGTYPE* DISOWN {Foo* toDie};
185 // you could just use %delobject, but this is more flexible
186 %typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE* DISOWN,SWIGTYPE DISOWN[]
187 %{  if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,SWIG_POINTER_DISOWN))){
188     SWIG_fail_ptr("$symname",$argnum,$descriptor);
189   }
190 %}
191
192
193 // Primitive types--return by value
194 // must make a new object, copy the data & return the new object
195 // Note: the brackets are {...} and not %{..%}, because we want them to be included in the wrapper
196 // this is because typemap(out) does not support local variables, like in typemap(in) does
197 // and we need the $&1_ltype resultptr; to be declared
198 #ifdef __cplusplus
199 %typemap(out) SWIGTYPE
200 {
201   $&1_ltype resultptr = new $1_ltype((const $1_ltype &) $1);
202   SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
203 }
204 #else
205 %typemap(out) SWIGTYPE
206 {
207   $&1_ltype resultptr;
208   resultptr = ($&1_ltype) malloc(sizeof($1_type));
209   memmove(resultptr, &$1, sizeof($1_type));
210   SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
211 }
212 #endif
213
214 // member function pointer
215 // a member fn ptr is not 4 bytes like a normal pointer, but 8 bytes (at least on mingw)
216 // so the standard wrappering cannot be done
217 // nor can you cast a member function pointer to a void* (obviously)
218 // therefore a special wrappering functions SWIG_ConvertMember() & SWIG_NewMemberObj() were written
219 #ifdef __cplusplus
220 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE (CLASS::*)
221 %{
222   if (!SWIG_IsOK(SWIG_ConvertMember(L,$input,(void*)(&$1),sizeof($type),$descriptor)))
223     SWIG_fail_ptr("$symname",$argnum,$descriptor);
224 %}
225
226 %typemap(out) SWIGTYPE (CLASS::*)
227 %{ 
228   SWIG_NewMemberObj(L,(void*)(&$1),sizeof($type),$descriptor); SWIG_arg++; 
229 %}
230 #endif
231
232
233 // void (must be empty without the SWIG_arg++)
234 %typemap(out) void "";
235
236 /* void* is a special case
237 A function void fn(void*) should take any kind of pointer as a parameter (just like C/C++ does)
238 but if its an output, then it should be wrappered like any other SWIG object (using default typemap)
239 */
240 %typemap(in,checkfn="SWIG_isptrtype") void*
241 %{$1=($1_ltype)SWIG_MustGetPtr(L,$input,0,0,$argnum,"$symname");%}
242
243 /* long long is another special case:
244 as lua only supports one numeric type (lua_Number), we will just
245 cast it to that & accept the loss of precision.
246 An alternative solution would be a long long struct or class
247 with the relevant operators.
248 */
249 %apply long {long long, signed long long, unsigned long long};
250 %apply const long& {const long long&, const signed long long&, const unsigned long long&};
251
252 /* It is possible to also pass a lua_State* into a function, so
253 void fn(int a, float b, lua_State* s) is wrappable as
254 > fn(1,4.3) -- note: the state is implicitly passed in
255 */
256 %typemap(in, numinputs=0) lua_State* 
257 %{$1 = L;%}
258
259
260
261 /* -----------------------------------------------------------------------------
262  *                          typecheck rules
263  * ----------------------------------------------------------------------------- */
264 /* These are needed for the overloaded functions
265 These define the detection routines which will spot what
266 parmeters match which function
267 */
268
269 // unfortunately lua only considers one type of number
270 // so all numbers (int,float,double) match
271 // you could add an advanced fn to get type & check if its integral
272 %typecheck(SWIG_TYPECHECK_INTEGER)
273          int, short, long,
274          unsigned int, unsigned short, unsigned long,
275          signed char, unsigned char,
276          long long, unsigned long long, signed long long,
277          const int &, const short &, const long &,
278          const unsigned int &, const unsigned short &, const unsigned long &,
279          const signed char&, const unsigned char&,
280          const long long &, const unsigned long long &,
281          enum SWIGTYPE, const enum SWIGTYPE&,
282          float, double, const float &, const double&
283 {
284   $1 = lua_isnumber(L,$input);
285 }
286
287 %typecheck(SWIG_TYPECHECK_BOOL)
288     bool, const bool &
289 {
290   $1 = lua_isboolean(L,$input);
291 }
292
293 // special check for a char (string of length 1)
294 %typecheck(SWIG_TYPECHECK_CHAR) char, const char& {
295   $1 = lua_isstring(L,$input) && (lua_strlen(L,$input)==1);
296 }
297
298 %typecheck(SWIG_TYPECHECK_STRING) char *, char[] {
299   $1 = lua_isstring(L,$input);
300 }
301
302 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE [] {
303   void *ptr;
304   if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
305     $1 = 0;
306   } else {
307     $1 = 1;
308   }
309 }
310
311 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE & {
312   void *ptr;
313   if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
314     $1 = 0;
315   } else {
316     $1 = 1;
317   }
318 }
319
320 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE {
321   void *ptr;
322   if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $&1_descriptor, 0)) {
323     $1 = 0;
324   } else {
325     $1 = 1;
326   }
327 }
328
329 %typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
330   void *ptr;
331   if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)) {
332     $1 = 0;
333   } else {
334     $1 = 1;
335   }
336 }
337
338 // Also needed for object ptrs by const ref
339 // eg const A* ref_pointer(A* const& a);
340 // found in mixed_types.i
341 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE* const &
342 {
343   void *ptr;
344   if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $*descriptor, 0)) {
345     $1 = 0;
346   } else {
347     $1 = 1;
348   }
349 }
350
351 /* -----------------------------------------------------------------------------
352  *                          Others
353  * ----------------------------------------------------------------------------- */
354
355 // Array reference typemaps
356 %apply SWIGTYPE & { SWIGTYPE ((&)[ANY]) }
357
358 // size_t (which is just a unsigned long)
359 %apply unsigned long { size_t };
360 %apply const unsigned long & { const size_t & };
361
362
363 /* -----------------------------------------------------------------------------
364  *                          Specials
365  * ----------------------------------------------------------------------------- */
366 // swig::LANGUAGE_OBJ was added to allow containers of native objects
367 // however its rather difficult to do this in lua, as you cannot hold pointers
368 // to native objects (they are held in the interpreter)
369 // therefore for now: just ignoring this feature
370 #ifdef __cplusplus
371 %ignore swig::LANGUAGE_OBJ;
372
373 //%inline %{
374 %{
375 namespace swig {
376 typedef struct{} LANGUAGE_OBJ;
377 }
378 %}
379
380 #endif // __cplusplus