Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/tolua/lua/array.lua @ 1955

Last change on this file since 1955 was 1755, checked in by rgrieder, 16 years ago

merged gui back to trunk.
update the media repository!

File size: 6.3 KB
Line 
1-- tolua: array class
2-- Written by Waldemar Celes
3-- TeCGraf/PUC-Rio
4-- Jul 1999
5-- $Id: array.lua,v 1.1 2000/11/06 22:03:57 celes Exp $
6
7-- This code is free software; you can redistribute it and/or modify it.
8-- The software provided hereunder is on an "as is" basis, and
9-- the author has no obligation to provide maintenance, support, updates,
10-- enhancements, or modifications.
11
12
13-- Array class
14-- Represents a extern array variable or a public member of a class.
15-- Stores all fields present in a declaration.
16classArray = {
17}
18classArray.__index = classArray
19setmetatable(classArray,classDeclaration)
20
21-- Print method
22function classArray:print (ident,close)
23 print(ident.."Array{")
24 print(ident.." mod  = '"..self.mod.."',")
25 print(ident.." type = '"..self.type.."',")
26 print(ident.." ptr  = '"..self.ptr.."',")
27 print(ident.." name = '"..self.name.."',")
28 print(ident.." def  = '"..self.def.."',")
29 print(ident.." dim  = '"..self.dim.."',")
30 print(ident.." ret  = '"..self.ret.."',")
31 print(ident.."}"..close)
32end
33
34-- check if it is a variable
35function classArray:isvariable ()
36 return true
37end
38
39
40-- get variable value
41function classArray:getvalue (class,static)
42 if class and static then
43  return class..'::'..self.name..'[tolua_index]'
44 elseif class then
45  return 'self->'..self.name..'[tolua_index]'
46 else
47  return self.name..'[tolua_index]'
48 end
49end
50
51-- Write binding functions
52function classArray:supcode ()
53 local class = self:inclass()
54
55 -- get function ------------------------------------------------
56 if class then
57  output("/* get function:",self.name," of class ",class," */")
58 else
59  output("/* get function:",self.name," */")
60 end
61 self.cgetname = self:cfuncname("tolua_get")
62 output("#ifndef TOLUA_DISABLE_"..self.cgetname)
63 output("\nstatic int",self.cgetname,"(lua_State* tolua_S)")
64 output("{")
65 output(" int tolua_index;")
66
67 -- declare self, if the case
68 local _,_,static = strfind(self.mod,'^%s*(static)')
69 if class and static==nil then
70  output(' ',self.parent.type,'*','self;')
71  output(' lua_pushstring(tolua_S,".self");')
72  output(' lua_rawget(tolua_S,1);')
73  output(' self = ')
74  output('(',self.parent.type,'*) ')
75  output('lua_touserdata(tolua_S,-1);')
76 elseif static then
77  _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
78 end
79
80 -- check index
81        output('#ifndef TOLUA_RELEASE\n')
82        output(' {')
83        output('  tolua_Error tolua_err;')
84 output('  if (!tolua_isnumber(tolua_S,2,0,&tolua_err))')
85 output('   tolua_error(tolua_S,"#vinvalid type in array indexing.",&tolua_err);')
86        output(' }')
87        output('#endif\n')
88        if flags['1'] then -- for compatibility with tolua5 ?
89                output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0)-1;')
90        else
91                output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0);')
92        end
93        output('#ifndef TOLUA_RELEASE\n')
94        if self.dim and self.dim ~= '' then
95          output(' if (tolua_index<0 || tolua_index>='..self.dim..')')
96        else
97          output(' if (tolua_index<0)')
98        end
99 output('  tolua_error(tolua_S,"array indexing out of range.",NULL);')
100        output('#endif\n')
101
102 -- return value
103 local t,ct = isbasic(self.type)
104 if t then
105  output(' tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static)..');')
106 else
107                t = self.type
108  if self.ptr == '&' or self.ptr == '' then
109   output(' tolua_pushusertype(tolua_S,(void*)&'..self:getvalue(class,static)..',"',t,'");')
110  else
111   output(' tolua_pushusertype(tolua_S,(void*)'..self:getvalue(class,static)..',"',t,'");')
112  end
113 end
114 output(' return 1;')
115 output('}')
116 output('#endif //#ifndef TOLUA_DISABLE\n')
117 output('\n')
118
119 -- set function ------------------------------------------------
120 if not strfind(self.type,'const') then
121  if class then
122   output("/* set function:",self.name," of class ",class," */")
123  else
124   output("/* set function:",self.name," */")
125  end
126  self.csetname = self:cfuncname("tolua_set")
127  output("#ifndef TOLUA_DISABLE_"..self.csetname)
128  output("\nstatic int",self.csetname,"(lua_State* tolua_S)")
129  output("{")
130
131  -- declare index
132  output(' int tolua_index;')
133
134  -- declare self, if the case
135  local _,_,static = strfind(self.mod,'^%s*(static)')
136  if class and static==nil then
137   output(' ',self.parent.type,'*','self;')
138   output(' lua_pushstring(tolua_S,".self");')
139   output(' lua_rawget(tolua_S,1);')
140   output(' self = ')
141   output('(',self.parent.type,'*) ')
142   output('lua_touserdata(tolua_S,-1);')
143  elseif static then
144   _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
145  end
146
147  -- check index
148         output('#ifndef TOLUA_RELEASE\n')
149         output(' {')
150         output('  tolua_Error tolua_err;')
151  output('  if (!tolua_isnumber(tolua_S,2,0,&tolua_err))')
152  output('   tolua_error(tolua_S,"#vinvalid type in array indexing.",&tolua_err);')
153                output(' }')
154                output('#endif\n')
155
156        if flags['1'] then -- for compatibility with tolua5 ?
157                output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0)-1;')
158        else
159                output(' tolua_index = (int)tolua_tonumber(tolua_S,2,0);')
160        end
161
162         output('#ifndef TOLUA_RELEASE\n')
163        if self.dim and self.dim ~= '' then
164          output(' if (tolua_index<0 || tolua_index>='..self.dim..')')
165        else
166          output(' if (tolua_index<0)')
167        end
168  output('  tolua_error(tolua_S,"array indexing out of range.",NULL);')
169                output('#endif\n')
170
171  -- assign value
172  local ptr = ''
173  if self.ptr~='' then ptr = '*' end
174  output(' ')
175  if class and static then
176   output(class..'::'..self.name..'[tolua_index]')
177  elseif class then
178   output('self->'..self.name..'[tolua_index]')
179  else
180   output(self.name..'[tolua_index]')
181  end
182  local t = isbasic(self.type)
183  output(' = ')
184  if not t and ptr=='' then output('*') end
185  output('((',self.mod,self.type)
186  if not t then
187   output('*')
188  end
189  output(') ')
190  local def = 0
191  if self.def ~= '' then def = self.def end
192  if t then
193   output('tolua_to'..t,'(tolua_S,3,',def,'));')
194  else
195   output('tolua_tousertype(tolua_S,3,',def,'));')
196  end
197  output(' return 0;')
198  output('}')
199  output('#endif //#ifndef TOLUA_DISABLE\n')
200  output('\n')
201 end
202
203end
204
205function classArray:register (pre)
206 pre = pre or ''
207 if self.csetname then
208  output(pre..'tolua_array(tolua_S,"'..self.lname..'",'..self.cgetname..','..self.csetname..');')
209 else
210  output(pre..'tolua_array(tolua_S,"'..self.lname..'",'..self.cgetname..',NULL);')
211 end
212end
213
214-- Internal constructor
215function _Array (t)
216 setmetatable(t,classArray)
217 append(t)
218 return t
219end
220
221-- Constructor
222-- Expects a string representing the variable declaration.
223function Array (s)
224 return _Array (Declaration(s,'var'))
225end
226
227
Note: See TracBrowser for help on using the repository browser.