comparison lua/luadyncall/src/array.lua @ 0:0cfcc391201f

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:26:28 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0cfcc391201f
1 require"larray"
2 -- typesignature = require"typesignature"
3 local array_mt = {
4 __index = function(t,i)
5 if type(i) == "number" then
6 return larray.peek( t.pointer, t.typesize * (i-1), t.typeinfo )
7 else
8 local result = rawget(t,i)
9 if not result then
10 return getmetatable(t)[i]
11 end
12 end
13 end,
14 __newindex = function(t,i,v)
15 if type(i) == "number" then
16 return larray.poke( t.pointer, t.typesize * (i-1), t.typeinfo, v)
17 else
18 return rawset(t,i,v)
19 end
20 end,
21 copy = function(array,src,nelements)
22 return larray.copy( array.pointer, 0, src, 0, array.typesize * nelements)
23 end
24 }
25
26 --- Get type information from type signature.
27 --
28 -- @param signature string representing type informations. Fundamental type signatures are represented by characters such as 'c' for C char, 's' for C short, 'i' for C int, 'j' for C long
29 -- 'l' for C long long, 'f' for C float, 'd' for C double, 'p' for C pointer,
30 -- 'B' for ISO C99 _Bool_t/C++ bool, 'v' for C void. Upper-case characters 'CSIJL' refer to the corresponding unsigned C type.
31 -- Function signature syntax: 'name(argtypes..)resulttypes..;'
32 -- Structure signature syntax: 'name{fieldtypes..)fieldnames..;' the fieldnames are space separated text tokens.
33 -- Named objects can be refered using '<name>' syntax.
34 -- pointer types can be specified by prefixing multiple '*'.
35 -- function typesignature(signature)
36 -- end
37
38 --- Get size of fundamental C types.
39 -- @param typeinfo a simple C type signature string.
40 -- @see typesignature
41 function typesize(typesignature)
42 return typesizes[typesignature]
43 end
44
45 --- Create a lua-memory managed C array.
46 -- Arrays are represented as a table with fields.
47 -- @field pointer holds the address represented by a lua userdata object.
48 -- @return array object
49 function array(typeinfo, length)
50 local typesize = larray.sizeof(typeinfo)
51 local size = typesize * length
52 local pointer = larray.newudata(size)
53 local o = {
54 pointer = pointer,
55 size = size,
56 length = length,
57 typesize = typesize,
58 typeinfo = typeinfo,
59 }
60 setmetatable(o, array_mt)
61 return o
62 end
63
64