2011-12-04 23:26:53 +01:00
|
|
|
#!/usr/bin/lua
|
|
|
|
|
|
|
|
if not (#arg >= 1 and #arg <= 4) then
|
|
|
|
print('usage: ' .. arg[0] .. ' <cpp class header> [class name] [cpp class instance] [lua class instance]')
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
cppclassheader = arg[1]
|
|
|
|
cppclassname = arg[2]
|
|
|
|
cppclassinstance = arg[3]
|
|
|
|
luaclassname = arg[3] or luaclassname
|
|
|
|
|
|
|
|
if not io.open(cppclassheader, 'r') then
|
|
|
|
print('could not open ' .. cppclassheader)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
function string:matchcount(pattern)
|
2011-12-04 23:26:53 +01:00
|
|
|
local count = 0
|
2011-12-07 01:31:55 +01:00
|
|
|
for w in self:gmatch(pattern) do count = count + 1 end
|
2011-12-04 23:26:53 +01:00
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
function string:splitlines()
|
2011-12-04 23:26:53 +01:00
|
|
|
local t = {}
|
|
|
|
local function helper(line) table.insert(t, line) return "" end
|
2011-12-07 01:31:55 +01:00
|
|
|
helper((self:gsub("(.-)\r?\n", helper)))
|
2011-12-04 23:26:53 +01:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
classfound = false
|
|
|
|
publicmethods = false
|
|
|
|
for line in io.lines(cppclassheader) do
|
|
|
|
foundclassname = line:match('^class ([%w_]+)')
|
|
|
|
if foundclassname then
|
|
|
|
if not cppclassname then
|
2011-12-07 01:31:55 +01:00
|
|
|
guessedclassname = cppclassheader:match('([%w_]+)\.h$'):lower()
|
2011-12-04 23:26:53 +01:00
|
|
|
if foundclassname:lower() == guessedclassname then
|
|
|
|
cppclassname = foundclassname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if foundclassname == cppclassname then
|
|
|
|
classfound = true
|
|
|
|
publicmethods = false
|
|
|
|
|
|
|
|
if cppclassinstance then
|
|
|
|
print('g_lua.registerStaticClass("' .. luaclassname .. '");')
|
|
|
|
else
|
|
|
|
baseclassname = line:match(': public ([%w_]+)')
|
|
|
|
bindline = 'g_lua.registerClass<' .. cppclassname
|
|
|
|
|
|
|
|
if baseclassname and baseclassname ~= 'LuaObject' then
|
|
|
|
bindline = bindline .. ', ' .. baseclassname
|
|
|
|
end
|
|
|
|
|
|
|
|
bindline = bindline .. '>();'
|
|
|
|
print(bindline)
|
|
|
|
|
|
|
|
bindline = 'g_lua.bindClassStaticFunction<' .. cppclassname .. '>("create", &' .. cppclassname .. '::create);'
|
|
|
|
print(bindline)
|
|
|
|
end
|
|
|
|
elseif classfound then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if classfound then
|
|
|
|
if line:match('public:') then
|
|
|
|
publicmethods = true
|
|
|
|
elseif line:match('private:') or line:match('protected:') then
|
|
|
|
publicmethods = false
|
|
|
|
elseif publicmethods then
|
2011-12-07 01:31:55 +01:00
|
|
|
funcname, args = line:match('^ *[%w <>&\*:_]* ([%w_]+)%(([^%)]*%))[%w ]*[;{].*$')
|
|
|
|
if funcname then
|
|
|
|
if funcname ~= cppclassname and funcname ~= 'create' then
|
2011-12-04 23:26:53 +01:00
|
|
|
numargs = args:matchcount('[^,)]+[,)]')
|
|
|
|
|
|
|
|
if cppclassinstance then
|
|
|
|
bindline = 'g_lua.bindClassStaticFunction("' .. luaclassname .. '", "' .. funcname .. '", ' ..
|
|
|
|
'std::bind(&' .. cppclassname .. "::" .. funcname .. ', &' .. cppclassinstance
|
|
|
|
for i=1,numargs do
|
|
|
|
bindline = bindline .. ', _' .. i
|
|
|
|
end
|
|
|
|
bindline = bindline .. '));'
|
|
|
|
else
|
|
|
|
bindline = 'g_lua.bindClassMemberFunction<' .. cppclassname .. '>("' .. funcname .. '", &' ..
|
|
|
|
cppclassname .. '::' .. funcname .. ');'
|
|
|
|
end
|
|
|
|
print(bindline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|