File: example.i

package info (click to toggle)
swig2.0 2.0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,260 kB
  • sloc: cpp: 49,839; ansic: 25,403; java: 8,412; python: 6,579; cs: 5,773; yacc: 5,158; makefile: 5,098; sh: 4,806; ruby: 3,673; perl: 2,384; lisp: 1,741; php: 1,701; tcl: 971; ml: 619; xml: 85
file content (42 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* File : example.i */
%module example

/* in this file there are two sorting functions
and three different ways to wrap them.

See the lua code for how they are called
*/

%include <carrays.i>    // array helpers

// this declares a batch of function for manipulating C integer arrays
%array_functions(int,int)

// this adds some lua code directly into the module
// warning: you need the example. prefix if you want it added into the module
// addmittedly this code is a bit tedious, but its a one off effort
%luacode {
function example.sort_int2(t)
 local len=table.maxn(t) -- the len
 local arr=example.new_int(len)
 for i=1,len do
  example.int_setitem(arr,i-1,t[i]) -- note: C index is one less then lua indea
 end
 example.sort_int(arr,len) -- call the fn
 -- copy back
 for i=1,len do
  t[i]=example.int_getitem(arr,i-1) -- note: C index is one less then lua indea
 end
 example.delete_int(arr) -- must delete it
end
}

// this way uses the SWIG-Lua typemaps to do the conversion for us
// the %apply command states to apply this wherever the argument signature matches
%include <typemaps.i>
%apply (double *INOUT,int) {(double* arr,int len)};

%inline %{
extern void sort_int(int* arr, int len);
extern void sort_double(double* arr, int len);
%}