4 Copyright (c) 2007-2008 Mauro Iazzi
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation
8 files (the "Software"), to deal in the Software without
9 restriction, including without limitation the rights to use,
10 copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
31 B
= dofile'binder.lua'
33 function NOINSTANCE(b
, name
)
34 b
.types_from_stack
[name
] = function(i
) error('cannot copy '..name
) end
35 b
.types_test
[name
] = function(i
) error('cannot copy '..name
) end
36 b
.types_to_stack
[name
] = function(i
) error('cannot copy '..name
) end
38 b
.types_from_stack
[(name
..' *')] = function(i
) return '*static_cast<'..name
..'**>(lqtL_toudata(L, '..tostring(i
)..', "'..name
..'"))' end
39 b
.types_test
[(name
..' *')] = function(i
) return 'lqtL_testudata(L, '..tostring(i
)..', "'..name
..'*")' end
40 b
.types_to_stack
[(name
..' *')] = function(i
) return 'lqtL_pushudata(L, '..tostring(i
)..', "'..name
..'*")' end
43 function cp_file(src
, dst
)
45 local src
= (type(src
)=='string') and io
.open(src
, 'r') or src
46 local check
= (type(dst
)=='string') and io
.open(dst
, 'r') or nil
47 if type(check
)=='userdata' then
48 local a
, b
= src
:read'*a', check
:read'*a'
49 if a
==b
then check
:close() src
:close() return end
53 src
= (type(src
)=='string') and io
.open(src
, 'r') or src
54 dst
= (type(dst
)=='string') and io
.open(dst
, 'w') or dst
55 local content
= src
:read('*a')
63 local h
, c
= B
:make_namespace(n
, n
, 'QtCore', 'QtGui')
64 print(n
..': writing definition file')
65 f
= io
.open('src/lqt_bind_'..n
..'.cpp', 'w')
69 print(n
..': writing prototypes file')
70 f
= io
.open('src/lqt_bind_'..n
..'.hpp', 'w')
76 B
.filter
= function (m
)
77 local n
= type(m
)=='table' and type(m
.attr
)=='table' and m
.attr
.name
78 if n
and string.match(n
, "[_%w]*[xX]11[_%w]*$") then
79 return true, 'it is X11 specific'
81 if n
and string.match(n
, "[_%w]*_[_%w]*$") then
82 return true, 'it is meant to be internal'
88 NOINSTANCE(B
, 'QCoreApplication')
91 B
.types_from_stack
['const QString&'] = function(i
) return 'QString::fromAscii(lua_tostring(L, '..tostring(i
)..'), lua_objlen(L, '..tostring(i
)..'))' end
92 B
.types_test
['const QString&'] = function(i
) return '(lua_type(L, ' .. tostring(i
) .. ')==LUA_TSTRING)' end
93 B
.types_to_stack
['const QString&'] = function(i
) return 'lua_pushlstring(L, '..tostring(i
)..'.toAscii().data(), '..tostring(i
)..'.toAscii().size())' end
94 B
.types_from_stack
['QString'] = function(i
) return 'QString::fromAscii(lua_tostring(L, '..tostring(i
)..'), lua_objlen(L, '..tostring(i
)..'))' end
95 B
.types_test
['QString'] = function(i
) return '(lua_type(L, ' .. tostring(i
) .. ')==LUA_TSTRING)' end
96 B
.types_to_stack
['QString'] = function(i
) return 'lua_pushlstring(L, '..tostring(i
)..'.toAscii().data(), '..tostring(i
)..'.toAscii().size())' end
98 B
.types_from_stack
['QByteArray'] = function(i
) return 'QByteArray(lua_tostring(L, '..tostring(i
)..'), lua_objlen(L, '..tostring(i
)..'))' end
99 B
.types_test
['QByteArray'] = function(i
) return '(lua_type(L, ' .. tostring(i
) .. ')==LUA_TSTRING)' end
100 B
.types_to_stack
['QByteArray'] = function(i
) return 'lua_pushlstring(L, '..tostring(i
)..'.data(), '..tostring(i
)..'.size())' end
103 function make_tree (cl
, tf
)
104 f
= io
.open(tf
..'.cpp', 'w')
105 for n
in pairs(cl
) do
106 f
:write('#include <'..n
..'>\n')
108 f
:write'\nmain() {\n'
109 for n
in pairs(cl
) do
110 if not ({Qt
=true})[n
] then f
:write(' '..n
..' *'..string.lower(n
)..';\n') end
114 os
.execute('gccxml `pkg-config QtGui QtCore --cflags` -fxml='..tf
..'.xml '..tf
..'.cpp')
115 --os.execute'gccxml -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -fxml=auto.xml auto.cpp'
116 --os.remove(tf..'.cpp')
119 function make_standard_qt(B
, classlist
)
120 print'copying common files'
121 cp_file('lqt_qt_utils.hpp', 'src/lqt_qt_utils.hpp')
122 cp_file('lqt_qt_utils.cpp', 'src/lqt_qt_utils.cpp')
123 cp_file('lqt_common.hpp', 'src/lqt_common.hpp')
124 cp_file('lqt_common.cpp', 'src/lqt_common.cpp')
128 for s
in string.gmatch(classlist
, '([%u%l%d]+)') do
134 local tmpfile
='tmp/auto'
136 make_tree(classlist
, tmpfile
)
138 B
:init(tmpfile
..'.xml')
143 for n
in pairs(classlist
) do
144 local c
= B
:find_name(n
)
145 clist
= B
.set_union(clist
, B
:tree_of_bases(c
))
147 classlist
= B
.set_union(classlist
, clist
)
150 for n
in pairs(classlist
) do
155 function make_single_qt(B
, class
)
156 local classlist
= { class
}
158 local tmpfile
='tmp/auto'
160 make_tree(classlist
, tmpfile
)
161 B
:init(tmpfile
..'.xml')
167 function B
:enum_push_body_plus_qt(id
, c
)
168 local enum
= (type(id
)=='string') and self
:find_id(id
) or id
169 local e_static
= (self
:find_id(enum
.attr
.context
).tag == 'Class') and 'static ' or ''
170 local e_context
= self
:context_name(enum
)
171 local e_name
= 'lqt_pushenum_' .. enum
.attr
.name
172 local e_proto
, e_def
= '', ''
174 e_proto
= e_proto
.. ' ' .. e_static
.. self
.lua_proto(e_name
) .. ';\n'
175 e_def
= e_def
.. self
.lua_proto(c
.. e_name
) .. ' '
176 e_def
= e_def
.. '{\n'
177 e_def
= e_def
.. ' int enum_table = 0;\n'
178 e_def
= e_def
.. ' lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);\n'
179 e_def
= e_def
.. ' if (!lua_istable(L, -1)) {\n'
180 e_def
= e_def
.. ' lua_pop(L, 1);\n'
181 e_def
= e_def
.. ' lua_newtable(L);\n'
182 e_def
= e_def
.. ' lua_pushvalue(L, -1);\n'
183 e_def
= e_def
.. ' lua_setfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);\n'
184 e_def
= e_def
.. ' }\n'
186 e_def
= e_def
.. ' lua_newtable(L);\n'
187 e_def
= e_def
.. ' enum_table = lua_gettop(L);\n'
188 for i
, e
in ipairs(enum
) do
189 if (type(e
)=='table') and (e
.tag=='EnumValue') then
190 e_def
= e_def
.. ' lua_pushstring(L, "' .. e
.attr
.name
.. '");\n'
191 e_def
= e_def
.. ' lua_rawseti(L, enum_table, ' .. e
.attr
.init
.. ');\n'
192 e_def
= e_def
.. ' lua_pushinteger(L, ' .. e
.attr
.init
.. ');\n'
193 e_def
= e_def
.. ' lua_setfield(L, enum_table, "' .. e
.attr
.name
.. '");\n'
196 e_def
= e_def
.. ' lua_pushcfunction(L, ' .. c
.. e_name
.. '_QFLAGS_CREATOR' .. ');\n'
197 e_def
= e_def
.. ' lua_setfield(L, enum_table, "QFlags");\n'
198 e_def
= e_def
.. ' lua_pushvalue(L, -1);\n'
199 e_def
= e_def
.. ' lua_setfield(L, -3, "' .. e_context
.. enum
.attr
.name
.. '");\n'
200 e_def
= e_def
.. ' lua_remove(L, -2);\n'
201 e_def
= e_def
.. ' return 1;\n'
202 e_def
= e_def
.. '}\n'
203 -- ######## QFLAGS SPECIFIC
204 e_proto
= e_proto
.. ' ' .. e_static
.. self
.lua_proto(e_name
..'_QFLAGS_CREATOR') .. ';\n'
205 e_def
= e_def
.. self
.lua_proto(c
.. e_name
.. '_QFLAGS_CREATOR') .. [[ {
206 int argn = lua_gettop(L);
208 void *p = lua_newuserdata(L, sizeof(QFlags<]]..e_context
..enum
.attr
.name
..[[>*) + sizeof(QFlags<]]..e_context
..enum
.attr
.name
..[[>));
209 QFlags<]]..e_context
..enum
.attr
.name
..[[> *fl = static_cast<QFlags<]]..e_context
..enum
.attr
.name
..[[>*>( static_cast<void*>(&static_cast<QFlags<]]..e_context
..enum
.attr
.name
..[[>**>(p)[1]) );
211 for (i=1;i<=argn;i++) {
212 *fl |= static_cast<]]..e_context
..enum
.attr
.name
..[[>(lqtL_toenum(L, i, "]]..e_context
..enum
.attr
.name
..[["));
214 if (luaL_newmetatable(L, "QFlags<]]..e_context
..enum
.attr
.name
..[[>*")) {
215 lua_pushstring(L, "QFlags<]]..e_context
..enum
.attr
.name
..[[>*");
216 lua_setfield(L, -2, "__qtype");
218 lua_setmetatable(L, -2);
223 return e_proto
, e_def
, e_name
226 B
.enum_push_body
= B
.enum_push_body_plus_qt