/trunk/Examples/lua/arrays/example.i
Swig | 42 lines | 30 code | 6 blank | 6 comment | 0 complexity | a58523ee4a4854b80a20179290730890 MD5 | raw file
1/* File : example.i */ 2%module example 3 4/* in this file there are two sorting functions 5and three different ways to wrap them. 6 7See the lua code for how they are called 8*/ 9 10%include <carrays.i> // array helpers 11 12// this declares a batch of function for manipulating C integer arrays 13%array_functions(int,int) 14 15// this adds some lua code directly into the module 16// warning: you need the example. prefix if you want it added into the module 17// addmittedly this code is a bit tedious, but its a one off effort 18%luacode { 19function example.sort_int2(t) 20 local len=table.maxn(t) -- the len 21 local arr=example.new_int(len) 22 for i=1,len do 23 example.int_setitem(arr,i-1,t[i]) -- note: C index is one less then lua indea 24 end 25 example.sort_int(arr,len) -- call the fn 26 -- copy back 27 for i=1,len do 28 t[i]=example.int_getitem(arr,i-1) -- note: C index is one less then lua indea 29 end 30 example.delete_int(arr) -- must delete it 31end 32} 33 34// this way uses the SWIG-Lua typemaps to do the conversion for us 35// the %apply command states to apply this wherever the argument signature matches 36%include <typemaps.i> 37%apply (double *INOUT,int) {(double* arr,int len)}; 38 39%inline %{ 40extern void sort_int(int* arr, int len); 41extern void sort_double(double* arr, int len); 42%}