PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Lib/lua/typemaps.i

#
Swig | 493 lines | 269 code | 42 blank | 182 comment | 0 complexity | 47f0a6cec0dd448da4393e2d6949c1e7 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. * typemaps.swg
  6. *
  7. * SWIG Library file containing the main typemap code to support Lua modules.
  8. * ----------------------------------------------------------------------------- */
  9. /* -----------------------------------------------------------------------------
  10. * Basic inout typemaps
  11. * ----------------------------------------------------------------------------- */
  12. /*
  13. These provide the basic ability for passing in & out of standard numeric data types
  14. (int,long,float,double, etc)
  15. The basic code looks like this:
  16. %typemap(in,checkfn="lua_isnumber") int *INPUT(int temp), int &INPUT(int temp)
  17. %{ temp = (int)lua_tonumber(L,$input);
  18. $1 = &temp; %}
  19. %typemap(in, numinputs=0) int *OUTPUT (int temp)
  20. %{ $1 = &temp; %}
  21. %typemap(argout) int *OUTPUT
  22. %{ lua_pushnumber(L, (double) *$1); SWIG_arg++;%}
  23. %typemap(in) int *INOUT = int *INPUT;
  24. %typemap(argout) int *INOUT = int *OUTPUT;
  25. However the code below is a mixture of #defines & such, so nowhere as easy to read
  26. To make you code work correctly its not just a matter of %including this file
  27. You also have to give SWIG the hints on which to use where
  28. eg
  29. extern int add_pointer(int* a1,int* a2); // a1 & a2 are pointer values to be added
  30. extern void swap(int* s1, int* s2); // does the swap
  31. You will need to either change the argument names
  32. extern int add_pointer(int* INPUT,int* INPUT);
  33. or provide a %apply statement
  34. %apply int* INOUT{ int *s1, int *s2 };
  35. // if SWIG sees int* s1, int* s2, assume they are inout params
  36. */
  37. %define SWIG_NUMBER_TYPEMAP(TYPE)
  38. %typemap(in,checkfn="lua_isnumber") TYPE *INPUT($*ltype temp), TYPE &INPUT($*ltype temp)
  39. %{ temp = ($*ltype)lua_tonumber(L,$input);
  40. $1 = &temp; %}
  41. %typemap(in, numinputs=0) TYPE *OUTPUT ($*ltype temp)
  42. %{ $1 = &temp; %}
  43. %typemap(argout) TYPE *OUTPUT
  44. %{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
  45. %typemap(in) TYPE *INOUT = TYPE *INPUT;
  46. %typemap(argout) TYPE *INOUT = TYPE *OUTPUT;
  47. %typemap(in) TYPE &INOUT = TYPE *INPUT;
  48. %typemap(argout) TYPE &INOUT = TYPE *OUTPUT;
  49. // const version (the $*ltype is the basic number without ptr or const's)
  50. %typemap(in,checkfn="lua_isnumber") const TYPE *INPUT($*ltype temp)
  51. %{ temp = ($*ltype)lua_tonumber(L,$input);
  52. $1 = &temp; %}
  53. %enddef
  54. // now the code
  55. SWIG_NUMBER_TYPEMAP(int); SWIG_NUMBER_TYPEMAP(unsigned int);
  56. SWIG_NUMBER_TYPEMAP(short); SWIG_NUMBER_TYPEMAP(unsigned short);
  57. SWIG_NUMBER_TYPEMAP(long); SWIG_NUMBER_TYPEMAP(unsigned long);
  58. SWIG_NUMBER_TYPEMAP(float);
  59. SWIG_NUMBER_TYPEMAP(double);
  60. SWIG_NUMBER_TYPEMAP(enum SWIGTYPE);
  61. // note we dont do char, as a char* is probably a string not a ptr to a single char
  62. /* -----------------------------------------------------------------------------
  63. * Basic Array typemaps
  64. * ----------------------------------------------------------------------------- */
  65. /*
  66. I have no idea why this kind of code does not exist in SWIG as standard,
  67. but here is it.
  68. This code will convert to/from 1D numeric arrays.
  69. In order to reduce code bloat, there are a few macros
  70. and quite a few functions defined
  71. (unfortunately this makes it a lot less clear)
  72. assuming we have functions
  73. void process_array(int arr[3]); // nice fixed size array
  74. void process_var_array(float arr[],int len); // variable sized array
  75. void process_var_array_inout(double arr*,int len); // variable sized array
  76. // data passed in & out
  77. void process_enum_inout_array_var(enum Days *arrinout, int len); // using enums
  78. void return_array_5(int arrout[5]); // out array only
  79. in order to wrap them correctly requires a typemap
  80. // inform SWIG of the correct typemap
  81. // For fixed length, you must specify it as <type> INPUT[ANY]
  82. %apply (int INPUT[ANY]) {(int arr[3])};
  83. // variable length arrays are just the same
  84. %apply (float INPUT[],int) {(float arr[],int len)};
  85. // it is also ok, to map the TYPE* instead of a TYPE[]
  86. %apply (double *INOUT,int) {(double arr*,int len)};
  87. // for the enum's you must use enum SWIGTYPE
  88. %apply (enum SWIGTYPE *INOUT,int) {(enum Days *arrinout, int len)};
  89. // fixed length out if also fine
  90. %apply (int OUTPUT[ANY]) {(int arrout[5])};
  91. Generally, you could use %typemap(...)=...
  92. but the %apply is neater & easier
  93. a few things of note:
  94. * all Lua tables are indexed from 1, all C/C++ arrays are indexed from 0
  95. therefore t={6,5,3} -- t[1]==6, t[2]==5, t[3]==3
  96. when passed to process_array(int arr[3]) becomes
  97. arr[0]==6, arr[1]==5, arr[2]==3
  98. * for OUTPUT arrays, no array need be passed in, the fn will return a Lua table
  99. so for the above mentioned return_array_5() would look like
  100. arr=return_array_5() -- no parameters passed in
  101. * for INOUT arrays, a table must be passed in, and a new table will be returned
  102. (this is consistant with the way that numbers are processed
  103. if you want just use
  104. arr={...}
  105. arr=process_var_array_inout(arr) -- arr is replaced by the new version
  106. The following are not yet supported:
  107. * variable length output only array (inout's work ok)
  108. * multidimentional arrays
  109. * arrays of objects/structs
  110. * arrays of pointers
  111. */
  112. /*
  113. The internals of the array managment stuff
  114. helper fns/macros
  115. SWIG_ALLOC_ARRAY(TYPE,LEN) // returns a typed array TYPE[LEN]
  116. SWIG_FREE_ARRAY(PTR) // delete the ptr (if not zero)
  117. // counts the specified table & gets the size
  118. // integer version
  119. int SWIG_itable_size(lua_State* L, int index);
  120. // other version
  121. int SWIG_table_size(lua_State* L, int index);
  122. SWIG_DECLARE_TYPEMAP_ARR_FN(NAME,TYPE)
  123. // this fn declares up 4 functions for helping to read/write tables
  124. // these can then be called by the macros ...
  125. // all assume the table is an integer indexes from 1
  126. // but the C array is a indexed from 0
  127. // created a fixed size array, reads the specified table
  128. // and then fills the array with numbers
  129. // returns ptr to the array if ok, or 0 for error
  130. // (also pushes a error message to the stack)
  131. TYPE* SWIG_get_NAME_num_array_fixed(lua_State* L, int index, int size);
  132. // as per SWIG_get_NAME_num_array_fixed()
  133. // but reads the entire table & creates an array of the correct size
  134. // (if the table is empty, it returns an error rather than a zero length array)
  135. TYPE* SWIG_get_NAME_num_array_var(lua_State* L, int index, int* size);
  136. // writes a table to Lua with all the specified numbers
  137. void SWIG_write_NAME_num_array(lua_State* L,TYPE *array,int size);
  138. // read the specified table, and fills the array with numbers
  139. // returns 1 of ok (only fails if it doesnt find numbers)
  140. // helper fn (called by SWIG_get_NAME_num_array_*() fns)
  141. int SWIG_read_NAME_num_array(lua_State* L,int index,TYPE *array,int size);
  142. */
  143. %{
  144. #ifdef __cplusplus /* generic alloc/dealloc fns*/
  145. #define SWIG_ALLOC_ARRAY(TYPE,LEN) new (TYPE)[LEN]
  146. #define SWIG_FREE_ARRAY(PTR) if(PTR){delete[] PTR;}
  147. #else
  148. #define SWIG_ALLOC_ARRAY(TYPE,LEN) (TYPE *)malloc(LEN*sizeof(TYPE))
  149. #define SWIG_FREE_ARRAY(PTR) if(PTR){free(PTR);}
  150. #endif
  151. /* counting the size of arrays:*/
  152. int SWIG_itable_size(lua_State* L, int index)
  153. {
  154. int n=0;
  155. while(1){
  156. lua_rawgeti(L,index,n+1);
  157. if (lua_isnil(L,-1))break;
  158. ++n;
  159. lua_pop(L,1);
  160. }
  161. lua_pop(L,1);
  162. return n;
  163. }
  164. int SWIG_table_size(lua_State* L, int index)
  165. {
  166. int n=0;
  167. lua_pushnil(L); /* first key*/
  168. while (lua_next(L, index) != 0) {
  169. ++n;
  170. lua_pop(L, 1); /* removes `value'; keeps `key' for next iteration*/
  171. }
  172. return n;
  173. }
  174. /* super macro to declare array typemap helper fns */
  175. #define SWIG_DECLARE_TYPEMAP_ARR_FN(NAME,TYPE)\
  176. int SWIG_read_##NAME##_num_array(lua_State* L,int index,TYPE *array,int size){\
  177. int i;\
  178. for (i = 0; i < size; i++) {\
  179. lua_rawgeti(L,index,i+1);\
  180. if (lua_isnumber(L,-1)){\
  181. array[i] = (TYPE)lua_tonumber(L,-1);\
  182. } else {\
  183. lua_pop(L,1);\
  184. return 0;\
  185. }\
  186. lua_pop(L,1);\
  187. }\
  188. return 1;\
  189. }\
  190. static TYPE* SWIG_get_##NAME##_num_array_fixed(lua_State* L, int index, int size){\
  191. TYPE *array;\
  192. if (!lua_istable(L,index) || SWIG_itable_size(L,index) != size) {\
  193. lua_pushfstring(L,"expected a table of size %d",size);\
  194. return 0;\
  195. }\
  196. array=SWIG_ALLOC_ARRAY(TYPE,size);\
  197. if (!SWIG_read_##NAME##_num_array(L,index,array,size)){\
  198. lua_pushstring(L,"table must contain numbers");\
  199. SWIG_FREE_ARRAY(array);\
  200. return 0;\
  201. }\
  202. return array;\
  203. }\
  204. static TYPE* SWIG_get_##NAME##_num_array_var(lua_State* L, int index, int* size)\
  205. {\
  206. TYPE *array;\
  207. if (!lua_istable(L,index)) {\
  208. lua_pushstring(L,"expected a table");\
  209. return 0;\
  210. }\
  211. *size=SWIG_itable_size(L,index);\
  212. if (*size<1){\
  213. lua_pushstring(L,"table appears to be empty");\
  214. return 0;\
  215. }\
  216. array=SWIG_ALLOC_ARRAY(TYPE,*size);\
  217. if (!SWIG_read_##NAME##_num_array(L,index,array,*size)){\
  218. lua_pushstring(L,"table must contain numbers");\
  219. SWIG_FREE_ARRAY(array);\
  220. return 0;\
  221. }\
  222. return array;\
  223. }\
  224. void SWIG_write_##NAME##_num_array(lua_State* L,TYPE *array,int size){\
  225. int i;\
  226. lua_newtable(L);\
  227. for (i = 0; i < size; i++){\
  228. lua_pushnumber(L,(lua_Number)array[i]);\
  229. lua_rawseti(L,-2,i+1);/* -1 is the number, -2 is the table*/ \
  230. }\
  231. }
  232. %}
  233. /*
  234. This is one giant macro to define the typemaps & the helpers
  235. for array handling
  236. */
  237. %define SWIG_TYPEMAP_NUM_ARR(NAME,TYPE)
  238. %{SWIG_DECLARE_TYPEMAP_ARR_FN(NAME,TYPE);%}
  239. // fixed size array's
  240. %typemap(in) TYPE INPUT[ANY]
  241. %{ $1 = SWIG_get_##NAME##_num_array_fixed(L,$input,$1_dim0);
  242. if (!$1) SWIG_fail;%}
  243. %typemap(freearg) TYPE INPUT[ANY]
  244. %{ SWIG_FREE_ARRAY($1);%}
  245. // variable size array's
  246. %typemap(in) (TYPE *INPUT,int)
  247. %{ $1 = SWIG_get_##NAME##_num_array_var(L,$input,&$2);
  248. if (!$1) SWIG_fail;%}
  249. %typemap(freearg) (TYPE *INPUT,int)
  250. %{ SWIG_FREE_ARRAY($1);%}
  251. // out fixed arrays
  252. %typemap(in,numinputs=0) TYPE OUTPUT[ANY]
  253. %{ $1 = SWIG_ALLOC_ARRAY(TYPE,$1_dim0); %}
  254. %typemap(argout) TYPE OUTPUT[ANY]
  255. %{ SWIG_write_##NAME##_num_array(L,$1,$1_dim0); SWIG_arg++; %}
  256. %typemap(freearg) TYPE OUTPUT[ANY]
  257. %{ SWIG_FREE_ARRAY($1); %}
  258. // inout fixed arrays
  259. %typemap(in) TYPE INOUT[ANY]=TYPE INPUT[ANY];
  260. %typemap(argout) TYPE INOUT[ANY]=TYPE OUTPUT[ANY];
  261. %typemap(freearg) TYPE INOUT[ANY]=TYPE INPUT[ANY];
  262. // inout variable arrays
  263. %typemap(in) (TYPE *INOUT,int)=(TYPE *INPUT,int);
  264. %typemap(argout) (TYPE *INOUT,int)
  265. %{ SWIG_write_##NAME##_num_array(L,$1,$2); SWIG_arg++; %}
  266. %typemap(freearg) (TYPE *INOUT,int)=(TYPE *INPUT,int);
  267. // TODO out variable arrays (is there a standard form for such things?)
  268. %enddef
  269. // the following line of code
  270. // declares the C helper fns for the array typemaps
  271. // as well as defining typemaps for
  272. // fixed len arrays in & out, & variable length arrays in
  273. SWIG_TYPEMAP_NUM_ARR(int,int);
  274. SWIG_TYPEMAP_NUM_ARR(uint,unsigned int);
  275. SWIG_TYPEMAP_NUM_ARR(short,short);
  276. SWIG_TYPEMAP_NUM_ARR(ushort,unsigned short);
  277. SWIG_TYPEMAP_NUM_ARR(long,long);
  278. SWIG_TYPEMAP_NUM_ARR(ulong,unsigned long);
  279. SWIG_TYPEMAP_NUM_ARR(float,float);
  280. SWIG_TYPEMAP_NUM_ARR(double,double);
  281. // again enums are a problem so they need their own type
  282. // we use the int conversion routine & recast it
  283. %typemap(in) enum SWIGTYPE INPUT[ANY]
  284. %{ $1 = ($ltype)SWIG_get_int_num_array_fixed(L,$input,$1_dim0);
  285. if (!$1) SWIG_fail;%}
  286. %typemap(freearg) enum SWIGTYPE INPUT[ANY]
  287. %{ SWIG_FREE_ARRAY($1);%}
  288. // variable size array's
  289. %typemap(in) (enum SWIGTYPE *INPUT,int)
  290. %{ $1 = ($ltype)SWIG_get_int_num_array_var(L,$input,&$2);
  291. if (!$1) SWIG_fail;%}
  292. %typemap(freearg) (enum SWIGTYPE *INPUT,int)
  293. %{ SWIG_FREE_ARRAY($1);%}
  294. // out fixed arrays
  295. %typemap(in,numinputs=0) enum SWIGTYPE OUTPUT[ANY]
  296. %{ $1 = SWIG_ALLOC_ARRAY(enum SWIGTYPE,$1_dim0); %}
  297. %typemap(argout) enum SWIGTYPE OUTPUT[ANY]
  298. %{ SWIG_write_int_num_array(L,(int*)$1,$1_dim0); SWIG_arg++; %}
  299. %typemap(freearg) enum SWIGTYPE OUTPUT[ANY]
  300. %{ SWIG_FREE_ARRAY($1); %}
  301. // inout fixed arrays
  302. %typemap(in) enum SWIGTYPE INOUT[ANY]=enum SWIGTYPE INPUT[ANY];
  303. %typemap(argout) enum SWIGTYPE INOUT[ANY]=enum SWIGTYPE OUTPUT[ANY];
  304. %typemap(freearg) enum SWIGTYPE INOUT[ANY]=enum SWIGTYPE INPUT[ANY];
  305. // inout variable arrays
  306. %typemap(in) (enum SWIGTYPE *INOUT,int)=(enum SWIGTYPE *INPUT,int);
  307. %typemap(argout) (enum SWIGTYPE *INOUT,int)
  308. %{ SWIG_write_int_num_array(L,(int*)$1,$2); SWIG_arg++; %}
  309. %typemap(freearg) (enum SWIGTYPE *INOUT,int)=(enum SWIGTYPE *INPUT,int);
  310. /* Surprisingly pointer arrays are easier:
  311. this is because all ptr arrays become void**
  312. so only a few fns are needed & a few casts
  313. The function defined are
  314. // created a fixed size array, reads the specified table
  315. // and then fills the array with pointers (checking the type)
  316. // returns ptr to the array if ok, or 0 for error
  317. // (also pushes a error message to the stack)
  318. void** SWIG_get_ptr_array_fixed(lua_State* L, int index, int size,swig_type_info *type);
  319. // as per SWIG_get_ptr_array_fixed()
  320. // but reads the entire table & creates an array of the correct size
  321. // (if the table is empty, it returns an error rather than a zero length array)
  322. void** SWIG_get_ptr_array_var(lua_State* L, int index, int* size,swig_type_info *type);
  323. // writes a table to Lua with all the specified pointers
  324. // all pointers have the ownership value 'own' (normally 0)
  325. void SWIG_write_ptr_array(lua_State* L,void **array,int size,int own);
  326. // read the specified table, and fills the array with ptrs
  327. // returns 1 of ok (only fails if it doesnt find correct type of ptrs)
  328. // helper fn (called by SWIG_get_ptr_array_*() fns)
  329. int SWIG_read_ptr_array(lua_State* L,int index,void **array,int size,swig_type_info *type);
  330. The key thing to remember is that it is assumed that there is no
  331. modification of pointers ownership in the arrays
  332. eg A fn:
  333. void pointers_in(TYPE* arr[],int len);
  334. will make copies of the pointer into a temp array and then pass it into the fn
  335. Lua does not remeber that this fn held the pointers, so it is not safe to keep
  336. these pointers until later
  337. eg A fn:
  338. void pointers_out(TYPE* arr[3]);
  339. will return a table containing three pointers
  340. however these pointers are NOT owned by Lua, merely borrowed
  341. so if the C/C++ frees then Lua is not aware
  342. */
  343. %{
  344. int SWIG_read_ptr_array(lua_State* L,int index,void **array,int size,swig_type_info *type){
  345. int i;
  346. for (i = 0; i < size; i++) {
  347. lua_rawgeti(L,index,i+1);
  348. if (!lua_isuserdata(L,-1) || SWIG_ConvertPtr(L,-1,&array[i],type,0)==-1){
  349. lua_pop(L,1);
  350. return 0;
  351. }
  352. lua_pop(L,1);
  353. }
  354. return 1;
  355. }
  356. static void** SWIG_get_ptr_array_fixed(lua_State* L, int index, int size,swig_type_info *type){
  357. void **array;
  358. if (!lua_istable(L,index) || SWIG_itable_size(L,index) != size) {
  359. lua_pushfstring(L,"expected a table of size %d",size);
  360. return 0;
  361. }
  362. array=SWIG_ALLOC_ARRAY(void*,size);
  363. if (!SWIG_read_ptr_array(L,index,array,size,type)){
  364. lua_pushfstring(L,"table must contain pointers of type %s",type->name);
  365. SWIG_FREE_ARRAY(array);
  366. return 0;
  367. }
  368. return array;
  369. }
  370. static void** SWIG_get_ptr_array_var(lua_State* L, int index, int* size,swig_type_info *type){
  371. void **array;
  372. if (!lua_istable(L,index)) {
  373. lua_pushstring(L,"expected a table");
  374. return 0;
  375. }
  376. *size=SWIG_itable_size(L,index);
  377. if (*size<1){
  378. lua_pushstring(L,"table appears to be empty");
  379. return 0;
  380. }
  381. array=SWIG_ALLOC_ARRAY(void*,*size);
  382. if (!SWIG_read_ptr_array(L,index,array,*size,type)){
  383. lua_pushfstring(L,"table must contain pointers of type %s",type->name);
  384. SWIG_FREE_ARRAY(array);
  385. return 0;
  386. }
  387. return array;
  388. }
  389. void SWIG_write_ptr_array(lua_State* L,void **array,int size,swig_type_info *type,int own){
  390. int i;
  391. lua_newtable(L);
  392. for (i = 0; i < size; i++){
  393. SWIG_NewPointerObj(L,array[i],type,own);
  394. lua_rawseti(L,-2,i+1);/* -1 is the number, -2 is the table*/
  395. }
  396. }
  397. %}
  398. // fixed size array's
  399. %typemap(in) SWIGTYPE* INPUT[ANY]
  400. %{ $1 = ($ltype)SWIG_get_ptr_array_fixed(L,$input,$1_dim0,$*1_descriptor);
  401. if (!$1) SWIG_fail;%}
  402. %typemap(freearg) SWIGTYPE* INPUT[ANY]
  403. %{ SWIG_FREE_ARRAY($1);%}
  404. // variable size array's
  405. %typemap(in) (SWIGTYPE **INPUT,int)
  406. %{ $1 = ($ltype)SWIG_get_ptr_array_var(L,$input,&$2,$*1_descriptor);
  407. if (!$1) SWIG_fail;%}
  408. %typemap(freearg) (SWIGTYPE **INPUT,int)
  409. %{ SWIG_FREE_ARRAY($1);%}
  410. // out fixed arrays
  411. %typemap(in,numinputs=0) SWIGTYPE* OUTPUT[ANY]
  412. %{ $1 = SWIG_ALLOC_ARRAY($*1_type,$1_dim0); %}
  413. %typemap(argout) SWIGTYPE* OUTPUT[ANY]
  414. %{ SWIG_write_ptr_array(L,(void**)$1,$1_dim0,$*1_descriptor,0); SWIG_arg++; %}
  415. %typemap(freearg) SWIGTYPE* OUTPUT[ANY]
  416. %{ SWIG_FREE_ARRAY($1); %}
  417. // inout fixed arrays
  418. %typemap(in) SWIGTYPE* INOUT[ANY]=SWIGTYPE* INPUT[ANY];
  419. %typemap(argout) SWIGTYPE* INOUT[ANY]=SWIGTYPE* OUTPUT[ANY];
  420. %typemap(freearg) SWIGTYPE* INOUT[ANY]=SWIGTYPE* INPUT[ANY];
  421. // inout variable arrays
  422. %typemap(in) (SWIGTYPE** INOUT,int)=(SWIGTYPE** INPUT,int);
  423. %typemap(argout) (SWIGTYPE** INOUT,int)
  424. %{ SWIG_write_ptr_array(L,(void**)$1,$2,$*1_descriptor,0); SWIG_arg++; %}
  425. %typemap(freearg) (SWIGTYPE**INOUT,int)=(SWIGTYPE**INPUT,int);