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
|
2012-01-10 23:13:40 +01:00
|
|
|
print(' g_lua.registerStaticClass("' .. luaclassname .. '");')
|
2011-12-04 23:26:53 +01:00
|
|
|
else
|
|
|
|
baseclassname = line:match(': public ([%w_]+)')
|
2012-01-10 23:13:40 +01:00
|
|
|
bindline = ' g_lua.registerClass<' .. cppclassname
|
2011-12-04 23:26:53 +01:00
|
|
|
|
|
|
|
if baseclassname and baseclassname ~= 'LuaObject' then
|
|
|
|
bindline = bindline .. ', ' .. baseclassname
|
|
|
|
end
|
|
|
|
|
|
|
|
bindline = bindline .. '>();'
|
|
|
|
print(bindline)
|
|
|
|
|
2012-01-10 23:13:40 +01:00
|
|
|
bindline = ' g_lua.bindClassStaticFunction<' .. cppclassname .. '>("create", []{ return ' .. cppclassname .. 'Ptr(new ' .. cppclassname .. '); });'
|
2011-12-04 23:26:53 +01:00
|
|
|
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
|
2012-01-02 21:46:40 +01:00
|
|
|
funcname, args = line:match('^ *[%w <>&\*:_]* ([%w_]+)%(([^%)]*%))[%w ]*[;{=].*$')
|
2011-12-07 01:31:55 +01:00
|
|
|
if funcname then
|
|
|
|
if funcname ~= cppclassname and funcname ~= 'create' then
|
2011-12-04 23:26:53 +01:00
|
|
|
numargs = args:matchcount('[^,)]+[,)]')
|
|
|
|
|
|
|
|
if cppclassinstance then
|
2012-01-10 23:13:40 +01:00
|
|
|
bindline = ' g_lua.bindClassStaticFunction("' .. luaclassname .. '", "' .. funcname .. '", ' ..
|
2011-12-04 23:26:53 +01:00
|
|
|
'std::bind(&' .. cppclassname .. "::" .. funcname .. ', &' .. cppclassinstance
|
|
|
|
for i=1,numargs do
|
2012-04-14 16:19:58 +02:00
|
|
|
bindline = bindline .. ', std::placeholders::_' .. i
|
2011-12-04 23:26:53 +01:00
|
|
|
end
|
|
|
|
bindline = bindline .. '));'
|
|
|
|
else
|
2012-01-10 23:13:40 +01:00
|
|
|
bindline = ' g_lua.bindClassMemberFunction<' .. cppclassname .. '>("' .. funcname .. '", &' ..
|
2011-12-04 23:26:53 +01:00
|
|
|
cppclassname .. '::' .. funcname .. ');'
|
|
|
|
end
|
|
|
|
print(bindline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|