annotate lua/luadyncall/src/array.lua @ 19:5b51738793c6

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