Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/tolua/lua/variable.lua @ 3127

Last change on this file since 3127 was 3127, checked in by rgrieder, 15 years ago

Update to tolua 1.0.93

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1-- tolua: variable class
2-- Written by Waldemar Celes
3-- TeCGraf/PUC-Rio
4-- Jul 1998
5-- $Id: $
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-- Variable class
14-- Represents a extern variable or a public member of a class.
15-- Stores all fields present in a declaration.
16classVariable = {
17    _get = {},   -- mapped get functions
18    _set = {},   -- mapped set functions
19}
20classVariable.__index = classVariable
21setmetatable(classVariable,classDeclaration)
22
23-- Print method
24function 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.."',") end
31    print(ident.." def  = '"..self.def.."',")
32    print(ident.." ret  = '"..self.ret.."',")
33    print(ident.."}"..close)
34end
35
36-- Generates C function name
37function 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 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
65end
66
67-- check if it is a variable
68function classVariable:isvariable ()
69    return true
70end
71
72-- get variable value
73function classVariable:getvalue (class,static, prop_get)
74
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
90end
91
92-- get variable pointer value
93function classVariable:getpointervalue (class,static)
94    if class and static then
95        return class..'::p'
96    elseif class then
97        return 'self->p'
98    else
99        return 'p'
100    end
101end
102
103-- Write binding functions
104function classVariable:supcode ()
105
106 local class = self:inclass()
107
108    local prop_get,prop_set
109    if string.find(self.mod, 'tolua_property') then
110
111        local _,_,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        local to_func = get_to_function(self.parent.type)
134        output(to_func,'(tolua_S,1,0);')
135    elseif static then
136        _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
137    end
138
139
140    -- check self value
141    if class and static==nil then
142        output('#ifndef TOLUA_RELEASE\n')
143        output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in accessing variable \'%s\'", self.name)..'",NULL);');
144        output('#endif\n')
145    end
146
147    -- return value
148    if string.find(self.mod, 'tolua_inherits') then
149    local push_func = get_push_function(self.type)
150        output('#ifdef __cplusplus\n')
151        output('  ',push_func,'(tolua_S,(void*)static_cast<'..self.type..'*>(self), "',self.type,'");')
152        output('#else\n')
153        output('  ',push_func,'(tolua_S,(void*)(('..self.type..'*)self), "',self.type,'");')
154        output('#endif\n')
155    else
156        local t,ct = isbasic(self.type)
157        if t then
158            output('  tolua_push'..t..'(tolua_S,(',ct,')'..self:getvalue(class,static,prop_get)..');')
159        else
160            local push_func = get_push_function(self.type)
161            t = self.type
162            if self.ptr == '&' or self.ptr == '' then
163                output('  ',push_func,'(tolua_S,(void*)&'..self:getvalue(class,static,prop_get)..',"',t,'");')
164            else
165                output('  ',push_func,'(tolua_S,(void*)'..self:getvalue(class,static,prop_get)..',"',t,'");')
166            end
167        end
168    end
169    output(' return 1;')
170    output('}')
171    output('#endif //#ifndef TOLUA_DISABLE\n')
172    output('\n')
173
174    -- set function ------------------------------------------------
175    if not (strfind(self.type,'const%s+') or string.find(self.mod, 'tolua_readonly') or string.find(self.mod, 'tolua_inherits'))  then
176    if class then
177        output("/* set function:",self.name," of class ",class," */")
178    else
179        output("/* set function:",self.name," */")
180    end
181    self.csetname = self:cfuncname("tolua_set")
182    output("#ifndef TOLUA_DISABLE_"..self.csetname)
183    output("\nstatic int",self.csetname,"(lua_State* tolua_S)")
184    output("{")
185
186    -- declare self, if the case
187    if class and static==nil then
188        output(' ',self.parent.type,'*','self = ')
189        output('(',self.parent.type,'*) ')
190        local to_func = get_to_function(self.parent.type)
191        output(to_func,'(tolua_S,1,0);')
192        -- check self value
193    end
194    -- check types
195    output('#ifndef TOLUA_RELEASE\n')
196    output('  tolua_Error tolua_err;')
197    if class and static==nil then
198        output('  if (!self) tolua_error(tolua_S,"'..output_error_hook("invalid \'self\' in accessing variable \'%s\'", self.name)..'",NULL);');
199    elseif static then
200        _,_,self.mod = strfind(self.mod,'^%s*static%s%s*(.*)')
201    end
202
203    -- check variable type
204    output('  if ('..self:outchecktype(2)..')')
205    output('   tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);')
206    output('#endif\n')
207
208    -- assign value
209    local def = 0
210    if self.def ~= '' then def = self.def end
211    if self.type == 'char*' and self.dim ~= '' then -- is string
212        output(' strncpy((char*)')
213        if class and static then
214            output(self.parent.type..'::'..self.name)
215            elseif class then
216                output('self->'..self.name)
217            else
218                output(self.name)
219            end
220            output(',(const char*)tolua_tostring(tolua_S,2,',def,'),',self.dim,'-1);')
221        else
222            local ptr = ''
223            if self.ptr~='' then ptr = '*' end
224            output(' ')
225            local name = prop_set or self.name
226            if class and static then
227                output(self.parent.type..'::'..name)
228            elseif class then
229                output('self->'..name)
230            else
231                output(name)
232            end
233            local t = isbasic(self.type)
234            if prop_set then
235                output('(')
236            else
237                output(' = ')
238            end
239            if not t and ptr=='' then output('*') end
240            output('((',self.mod,self.type)
241            if not t then
242                output('*')
243            end
244            output(') ')
245            if t then
246                if isenum(self.type) then
247                    output('(int) ')
248                end
249                output('tolua_to'..t,'(tolua_S,2,',def,'))')
250            else
251                local to_func = get_to_function(self.type)
252                output(to_func,'(tolua_S,2,',def,'))')
253            end
254            if prop_set then
255                output(")")
256            end
257            output(";")
258        end
259        output(' return 0;')
260        output('}')
261        output('#endif //#ifndef TOLUA_DISABLE\n')
262        output('\n')
263    end
264
265end
266
267function classVariable:register (pre)
268
269    if not self:check_public_access() then
270        return
271    end
272    pre = pre or ''
273    local parent = self:inmodule() or self:innamespace() or self:inclass()
274    if not parent then
275        if classVariable._warning==nil then
276            warning("Mapping variable to global may degrade performance")
277            classVariable._warning = 1
278        end
279    end
280    if self.csetname then
281        output(pre..'tolua_variable(tolua_S,"'..self.lname..'",'..self.cgetname..','..self.csetname..');')
282    else
283        output(pre..'tolua_variable(tolua_S,"'..self.lname..'",'..self.cgetname..',NULL);')
284    end
285end
286
287-- Internal constructor
288function _Variable (t)
289    setmetatable(t,classVariable)
290    append(t)
291    return t
292end
293
294-- Constructor
295-- Expects a string representing the variable declaration.
296function Variable (s)
297    return _Variable (Declaration(s,'var'))
298end
299
300
Note: See TracBrowser for help on using the repository browser.