From cc7b5e1a8c07bbc070480a32f43d7878e67a23ab Mon Sep 17 00:00:00 2001 From: Michal Kottman Date: Thu, 5 Aug 2010 16:27:50 +0200 Subject: [PATCH] make templates configurable from a file The templates are now configurable via a Lua file returning the translation table. It is 'qtemplates.lua' by default, but a custom file can be specified on the command line using the '-c filepath' switch. The keys in the table can be either the names of template classes, or the module name (qtcore, qtgui...). See generator/qtemplates.lua for examples of usage. --- generator/generator.lua | 4 ++++ generator/qtemplates.lua | 14 ++++++++++++++ generator/templates.lua | 17 ++++------------- 3 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 generator/qtemplates.lua diff --git a/generator/generator.lua b/generator/generator.lua index 4a08223..33c465b 100644 --- a/generator/generator.lua +++ b/generator/generator.lua @@ -40,6 +40,7 @@ end local filename = nil local dirname = nil module_name = nil +template_file = path .. 'qtemplates.lua' local typefiles = {} local filterfiles = {} output_includes = { @@ -62,6 +63,9 @@ do elseif argi=='-f' then i = i + 1 table.insert(filterfiles, (select(i, ...))) + elseif argi=='-c' then + i = i + 1 + template_file = select(i, ...) else filename = filename and error'duplicate filename' or argi end diff --git a/generator/qtemplates.lua b/generator/qtemplates.lua new file mode 100644 index 0000000..cfa4d12 --- /dev/null +++ b/generator/qtemplates.lua @@ -0,0 +1,14 @@ +return { + -- example usage: + -- + -- ["ex"] = { "ex", "ex" } + -- module = { ["ex2"] = { "example" } } + + qtcore = { + ["QList"] = { "QList", "QList" }, + }, + qtgui = { + ["QList"] = { "QList" }, + ["QVector"] = { "QVector" }, + }, +} diff --git a/generator/templates.lua b/generator/templates.lua index 4257785..d83012d 100644 --- a/generator/templates.lua +++ b/generator/templates.lua @@ -1,15 +1,7 @@ module('templates', package.seeall) -- TODO: maybe automate this? -local translate = { - qtcore = { - ["QList"] = { "QList", "QList" }, - }, - qtgui = { - ["QList"] = { "QList" }, - ["QVector"] = { "QVector" } - } -} +local translate = dofile(template_file) --- Creates a deep copy of an object. local function deepcopy(object) @@ -40,17 +32,16 @@ local idindex_add = {} --- Return true if an instance of templated class should be created. --- The decision is based on the contents of the 'translate' table and the --- name of current module function should_copy(class) - return translate[module_name] and translate[module_name][class.xarg.fullname] + return translate[class.xarg.fullname] or + (translate[module_name] and translate[module_name][class.xarg.fullname]) end --- Creates instantiated copies of template class. -- New classes are created according to the 'translate' table as deep copies, and -- are inserted into the 'ret' table. function create(class, ret) - local temps = translate[module_name][class.xarg.fullname] + local temps = should_copy(class) local replace_in = {name=true, context=true, fullname=true, member_of=true, member_of_class=true, scope=true, type_base=true, type_name=true, return_type=true } -- 2.11.4.GIT