PageRenderTime 86ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/lua/arrays/example.i

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