Changeset 2509 for code/branches/buildsystem2/src/tolua/lua/variable.lua
- Timestamp:
- Dec 17, 2008, 8:59:48 PM (16 years ago)
- Location:
- code/branches/buildsystem2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/buildsystem2
- Property svn:ignore deleted
- Property svn:mergeinfo changed
/code/branches/buildsystem (added) merged: 1875,1882-1886,1975-1982,1991,1999,2054,2061,2135,2137-2139,2197-2199,2204,2214-2220,2223-2224,2229,2233-2244,2248-2249,2252-2253,2260,2275
-
code/branches/buildsystem2/src/tolua/lua/variable.lua
r2087 r2509 15 15 -- Stores all fields present in a declaration. 16 16 classVariable = { 17 _get = {}, -- mapped get functions18 _set = {}, -- mapped set functions17 _get = {}, -- mapped get functions 18 _set = {}, -- mapped set functions 19 19 } 20 20 classVariable.__index = classVariable … … 23 23 -- Print method 24 24 function classVariable:print (ident,close) 25 print(ident.."Variable{")26 print(ident.." mod = '"..self.mod.."',")27 print(ident.." type = '"..self.type.."',")28 print(ident.." ptr = '"..self.ptr.."',")29 print(ident.." name = '"..self.name.."',")30 if self.dim then print(ident.." dim = '"..self.dim.."',") end31 print(ident.." def = '"..self.def.."',")32 print(ident.." ret = '"..self.ret.."',")33 print(ident.."}"..close)25 print(ident.."Variable{") 26 print(ident.." mod = '"..self.mod.."',") 27 print(ident.." type = '"..self.type.."',") 28 print(ident.." ptr = '"..self.ptr.."',") 29 print(ident.." name = '"..self.name.."',") 30 if self.dim then print(ident.." dim = '"..self.dim.."',") end 31 print(ident.." def = '"..self.def.."',") 32 print(ident.." ret = '"..self.ret.."',") 33 print(ident.."}"..close) 34 34 end 35 35 36 36 -- Generates C function name 37 37 function classVariable:cfuncname (prefix) 38 local parent = ""39 local unsigned = ""40 local ptr = ""41 42 local p = self:inmodule() or self:innamespace() or self:inclass()43 44 if p then45 46 47 48 49 50 end51 52 if strfind(self.mod,"(unsigned)") then53 unsigned = "_unsigned"54 end55 56 if self.ptr == "*" then ptr = "_ptr"57 elseif self.ptr == "&" then ptr = "_ref"58 end59 60 local name = prefix .. parent .. unsigned .. "_" .. gsub(self.lname or self.name,".*::","") .. ptr61 62 63 return name38 local parent = "" 39 local unsigned = "" 40 local ptr = "" 41 42 local p = self:inmodule() or self:innamespace() or self:inclass() 43 44 if p then 45 if self.parent.classtype == 'class' then 46 parent = "_" .. self.parent.type 47 else 48 parent = "_" .. p 49 end 50 end 51 52 if strfind(self.mod,"(unsigned)") then 53 unsigned = "_unsigned" 54 end 55 56 if self.ptr == "*" then ptr = "_ptr" 57 elseif self.ptr == "&" then ptr = "_ref" 58 end 59 60 local name = prefix .. parent .. unsigned .. "_" .. gsub(self.lname or self.name,".*::","") .. ptr 61 62 name = clean_template(name) 63 return name 64 64 65 65 end … … 67 67 -- check if it is a variable 68 68 function classVariable:isvariable () 69 return true69 return true 70 70 end 71 71 … … 73 73 function classVariable:getvalue (class,static, prop_get) 74 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 75 local name 76 if prop_get then 77 78 name = prop_get.."()" 79 else 80 name = self.name 81 end 82 83 if class and static then 84 return self.parent.type..'::'..name 85 elseif class then 86 return 'self->'..name 87 else 88 return name 89 end 90 90 end 91 91 92 92 -- get variable pointer value 93 93 function classVariable:getpointervalue (class,static) 94 if class and static then95 return class..'::p'96 elseif class then97 return 'self->p'98 else99 return 'p'100 end94 if class and static then 95 return class..'::p' 96 elseif class then 97 return 'self->p' 98 else 99 return 'p' 100 end 101 101 end 102 102 … … 106 106 local class = self:inclass() 107 107 108 109 110 111 112 113 114 115 116 117 -- get function ------------------------------------------------118 if class then119 output("/* get function:",self.name," of class ",class," */")120 else121 output("/* get function:",self.name," */")122 end123 self.cgetname = self:cfuncname("tolua_get")124 output("#ifndef TOLUA_DISABLE_"..self.cgetname)125 output("\nstatic int",self.cgetname,"(lua_State* tolua_S)")126 output("{")127 128 -- declare self, if the case129 local _,_,static = strfind(self.mod,'^%s*(static)')130 if class and static==nil then131 output(' ',self.parent.type,'*','self = ')132 output('(',self.parent.type,'*) ')133 output('tolua_tousertype(tolua_S,1,0);')134 elseif static then135 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')136 end137 138 139 -- check self value140 if class and static==nil then141 142 output(' if (!self) tolua_error(tolua_S,"invalid \'self\' in accessing variable \''..self.name..'\'",NULL);');143 144 end145 146 -- return value147 if string.find(self.mod, 'tolua_inherits') then148 149 150 151 152 153 else154 155 156 157 158 159 160 161 162 163 164 165 end166 output(' return 1;')167 output('}')168 output('#endif //#ifndef TOLUA_DISABLE\n')169 output('\n')170 171 -- set function ------------------------------------------------172 if not (strfind(self.type,'const%s+') or string.find(self.mod, 'tolua_readonly') or string.find(self.mod, 'tolua_inherits')) then173 if class then174 output("/* set function:",self.name," of class ",class," */")175 else176 output("/* set function:",self.name," */")177 end178 self.csetname = self:cfuncname("tolua_set")179 output("#ifndef TOLUA_DISABLE_"..self.csetname)180 output("\nstatic int",self.csetname,"(lua_State* tolua_S)")181 output("{")182 183 -- declare self, if the case184 if class and static==nil then185 output(' ',self.parent.type,'*','self = ')186 output('(',self.parent.type,'*) ')187 output('tolua_tousertype(tolua_S,1,0);')188 -- check self value189 190 -- check types191 192 193 if class and static==nil then194 output(' if (!self) tolua_error(tolua_S,"invalid \'self\' in accessing variable \''..self.name..'\'",NULL);');195 elseif static then196 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')197 end198 199 -- check variable type200 output(' if (!'..self:outchecktype(2)..')')201 output(' tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);')202 203 204 -- assign value205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 output(' return 0;')255 output('}')256 output('#endif //#ifndef TOLUA_DISABLE\n')257 output('\n')258 end108 local prop_get,prop_set 109 if string.find(self.mod, 'tolua_property') then 110 111 _,_,type = string.find(self.mod, "tolua_property__([^%s]*)") 112 type = type or "default" 113 prop_get,prop_set = get_property_methods(type, self.name) 114 self.mod = string.gsub(self.mod, "tolua_property[^%s]*", "") 115 end 116 117 -- get function ------------------------------------------------ 118 if class then 119 output("/* get function:",self.name," of class ",class," */") 120 else 121 output("/* get function:",self.name," */") 122 end 123 self.cgetname = self:cfuncname("tolua_get") 124 output("#ifndef TOLUA_DISABLE_"..self.cgetname) 125 output("\nstatic int",self.cgetname,"(lua_State* tolua_S)") 126 output("{") 127 128 -- declare self, if the case 129 local _,_,static = strfind(self.mod,'^%s*(static)') 130 if class and static==nil then 131 output(' ',self.parent.type,'*','self = ') 132 output('(',self.parent.type,'*) ') 133 output('tolua_tousertype(tolua_S,1,0);') 134 elseif static then 135 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') 136 end 137 138 139 -- check self value 140 if class and static==nil then 141 output('#ifndef TOLUA_RELEASE\n') 142 output(' if (!self) tolua_error(tolua_S,"invalid \'self\' in accessing variable \''..self.name..'\'",NULL);'); 143 output('#endif\n') 144 end 145 146 -- return value 147 if string.find(self.mod, 'tolua_inherits') then 148 output('#ifdef __cplusplus\n') 149 output(' tolua_pushusertype(tolua_S,(void*)static_cast<'..self.type..'*>(self), "',self.type,'");') 150 output('#else\n') 151 output(' tolua_pushusertype(tolua_S,(void*)(('..self.type..'*)self), "',self.type,'");') 152 output('#endif\n') 153 else 154 local t,ct = isbasic(self.type) 155 if t then 156 output(' tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static,prop_get)..');') 157 else 158 t = self.type 159 if self.ptr == '&' or self.ptr == '' then 160 output(' tolua_pushusertype(tolua_S,(void*)&'..self:getvalue(class,static,prop_get)..',"',t,'");') 161 else 162 output(' tolua_pushusertype(tolua_S,(void*)'..self:getvalue(class,static,prop_get)..',"',t,'");') 163 end 164 end 165 end 166 output(' return 1;') 167 output('}') 168 output('#endif //#ifndef TOLUA_DISABLE\n') 169 output('\n') 170 171 -- set function ------------------------------------------------ 172 if not (strfind(self.type,'const%s+') or string.find(self.mod, 'tolua_readonly') or string.find(self.mod, 'tolua_inherits')) then 173 if class then 174 output("/* set function:",self.name," of class ",class," */") 175 else 176 output("/* set function:",self.name," */") 177 end 178 self.csetname = self:cfuncname("tolua_set") 179 output("#ifndef TOLUA_DISABLE_"..self.csetname) 180 output("\nstatic int",self.csetname,"(lua_State* tolua_S)") 181 output("{") 182 183 -- declare self, if the case 184 if class and static==nil then 185 output(' ',self.parent.type,'*','self = ') 186 output('(',self.parent.type,'*) ') 187 output('tolua_tousertype(tolua_S,1,0);') 188 -- check self value 189 end 190 -- check types 191 output('#ifndef TOLUA_RELEASE\n') 192 output(' tolua_Error tolua_err;') 193 if class and static==nil then 194 output(' if (!self) tolua_error(tolua_S,"invalid \'self\' in accessing variable \''..self.name..'\'",NULL);'); 195 elseif static then 196 _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)') 197 end 198 199 -- check variable type 200 output(' if (!'..self:outchecktype(2)..')') 201 output(' tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);') 202 output('#endif\n') 203 204 -- assign value 205 local def = 0 206 if self.def ~= '' then def = self.def end 207 if self.type == 'char*' and self.dim ~= '' then -- is string 208 output(' strncpy(') 209 if class and static then 210 output(self.parent.type..'::'..self.name) 211 elseif class then 212 output('self->'..self.name) 213 else 214 output(self.name) 215 end 216 output(',tolua_tostring(tolua_S,2,',def,'),',self.dim,'-1);') 217 else 218 local ptr = '' 219 if self.ptr~='' then ptr = '*' end 220 output(' ') 221 local name = prop_set or self.name 222 if class and static then 223 output(self.parent.type..'::'..name) 224 elseif class then 225 output('self->'..name) 226 else 227 output(name) 228 end 229 local t = isbasic(self.type) 230 if prop_set then 231 output('(') 232 else 233 output(' = ') 234 end 235 if not t and ptr=='' then output('*') end 236 output('((',self.mod,self.type) 237 if not t then 238 output('*') 239 end 240 output(') ') 241 if t then 242 if isenum(self.type) then 243 output('(int) ') 244 end 245 output('tolua_to'..t,'(tolua_S,2,',def,'))') 246 else 247 output('tolua_tousertype(tolua_S,2,',def,'))') 248 end 249 if prop_set then 250 output(")") 251 end 252 output(";") 253 end 254 output(' return 0;') 255 output('}') 256 output('#endif //#ifndef TOLUA_DISABLE\n') 257 output('\n') 258 end 259 259 260 260 end … … 262 262 function classVariable:register (pre) 263 263 264 265 266 267 pre = pre or ''268 local parent = self:inmodule() or self:innamespace() or self:inclass()269 if not parent then270 if classVariable._warning==nil then271 warning("Mapping variable to global may degrade performance")272 classVariable._warning = 1273 end274 end275 if self.csetname then276 output(pre..'tolua_variable(tolua_S,"'..self.lname..'",'..self.cgetname..','..self.csetname..');')277 else278 output(pre..'tolua_variable(tolua_S,"'..self.lname..'",'..self.cgetname..',NULL);')279 end264 if not self:check_public_access() then 265 return 266 end 267 pre = pre or '' 268 local parent = self:inmodule() or self:innamespace() or self:inclass() 269 if not parent then 270 if classVariable._warning==nil then 271 warning("Mapping variable to global may degrade performance") 272 classVariable._warning = 1 273 end 274 end 275 if self.csetname then 276 output(pre..'tolua_variable(tolua_S,"'..self.lname..'",'..self.cgetname..','..self.csetname..');') 277 else 278 output(pre..'tolua_variable(tolua_S,"'..self.lname..'",'..self.cgetname..',NULL);') 279 end 280 280 end 281 281 282 282 -- Internal constructor 283 283 function _Variable (t) 284 setmetatable(t,classVariable)285 append(t)286 return t284 setmetatable(t,classVariable) 285 append(t) 286 return t 287 287 end 288 288 … … 290 290 -- Expects a string representing the variable declaration. 291 291 function Variable (s) 292 return _Variable (Declaration(s,'var'))293 end 294 295 292 return _Variable (Declaration(s,'var')) 293 end 294 295
Note: See TracChangeset
for help on using the changeset viewer.