From f4ca190e869b9d03bf4f8b7b002c65af9f56c0ad Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 8 Jan 2023 14:50:45 +0200 Subject: [PATCH] Refactor the setup generator to store types in modules --- tools/setupgen/setupgen.cpp | 79 +++++++++++++++++++------------------ tools/setupgen/setupgen.h | 24 ++++++++--- 2 files changed, 59 insertions(+), 44 deletions(-) diff --git a/tools/setupgen/setupgen.cpp b/tools/setupgen/setupgen.cpp index 9506677..083fc8a 100644 --- a/tools/setupgen/setupgen.cpp +++ b/tools/setupgen/setupgen.cpp @@ -20,14 +20,15 @@ SetupGen::SetupGen(int argc, char **argv) int SetupGen::main() { create_standard_types(); - load(); + load(in_fn); - collect_headers(); + const Module &main_mod = modules.front(); + collect_headers(main_mod); if(out_fn.empty()) { - generate_header(IO::cout); - generate_code(IO::cout); + generate_header(main_mod, IO::cout); + generate_code(main_mod, IO::cout); } else { @@ -38,19 +39,19 @@ int SetupGen::main() FS::Path out_dir = FS::dirname(out_fn); out_base = FS::basepart(out_base); IO::BufferedFile header_out((out_dir/(out_base+".h")).str(), IO::M_WRITE); - generate_header(header_out); + generate_header(main_mod, header_out); IO::BufferedFile code_out((out_dir/(out_base+".cpp")).str(), IO::M_WRITE); IO::print(code_out, "#include \"%s.h\"\n", out_base); - if(!enums.empty()) + if(!main_mod.enums.empty()) IO::print(code_out, "#include \n"); - generate_code(code_out); + generate_code(main_mod, code_out); } else { IO::BufferedFile out(out_fn, IO::M_WRITE); - generate_header(out); + generate_header(main_mod, out); out.put('\n'); - generate_code(out); + generate_code(main_mod, out); } } @@ -85,30 +86,31 @@ const Type &SetupGen::get_type(const string &n) const return get_item(types, n); } -void SetupGen::load() +void SetupGen::load(const FS::Path &fn) { - IO::BufferedFile in_file(in_fn); - DataFile::Parser parser(in_file, in_fn); - Loader ldr(*this); + Module &mod = modules.emplace_back(); + IO::BufferedFile in_file(fn.str()); + DataFile::Parser parser(in_file, fn.str()); + Loader ldr(*this, mod); ldr.load(parser); } -void SetupGen::collect_headers() +void SetupGen::collect_headers(const Module &mod) { headers.insert("msp/datafile/objectloader.h"); headers.insert("msp/strings/lexicalcast.h"); - for(const unique_ptr &s: structs) + for(const unique_ptr &s: mod.structs) for(const Struct::Field &f: s->get_fields()) if(const string &h = f.type->get_header(); !h.empty()) headers.insert(h); } -void SetupGen::generate_header(IO::Base &out) const +void SetupGen::generate_header(const Module &mod, IO::Base &out) const { string guard = toupper(FS::basepart(FS::basename(out_fn.empty() ? in_fn : out_fn)))+"_H_"; - if(!name_space.empty()) + if(!mod.name_space.empty()) { - vector parts = split(name_space, "::"); + vector parts = split(mod.name_space, "::"); parts.emplace_back(move(guard)); guard = join(parts.begin(), parts.end(), "_"); } @@ -122,63 +124,64 @@ void SetupGen::generate_header(IO::Base &out) const IO::print(out, "#include <%s>\n", h); } - if(!name_space.empty()) - IO::print(out, "\nnamespace %s {\n", name_space); + if(!mod.name_space.empty()) + IO::print(out, "\nnamespace %s {\n", mod.name_space); - for(const unique_ptr &e: enums) + for(const unique_ptr &e: mod.enums) { out.put('\n'); e->define_type(out); } - for(const unique_ptr &s: structs) + for(const unique_ptr &s: mod.structs) { out.put('\n'); s->define_type(out); } - for(const unique_ptr &s: structs) + for(const unique_ptr &s: mod.structs) { out.put('\n'); s->define_loader(out); } - for(const unique_ptr &e: enums) + for(const unique_ptr &e: mod.enums) { out.put('\n'); e->declare_conversions(out); } - if(!name_space.empty()) - IO::print(out, "\n} // namespace %s\n", name_space); + if(!mod.name_space.empty()) + IO::print(out, "\n} // namespace %s\n", mod.name_space); IO::print(out, "\n#endif\n"); } -void SetupGen::generate_code(IO::Base &out) const +void SetupGen::generate_code(const Module &mod, IO::Base &out) const { - if(!name_space.empty()) - IO::print(out, "\nnamespace %s {\n", name_space); + if(!mod.name_space.empty()) + IO::print(out, "\nnamespace %s {\n", mod.name_space); - for(const unique_ptr &s: structs) + for(const unique_ptr &s: mod.structs) { out.put('\n'); s->define_functions(out); } - for(const unique_ptr &e: enums) + for(const unique_ptr &e: mod.enums) { out.put('\n'); e->define_conversions(out); } - if(!name_space.empty()) - IO::print(out, "\n} // namespace %s\n", name_space); + if(!mod.name_space.empty()) + IO::print(out, "\n} // namespace %s\n", mod.name_space); } -SetupGen::Loader::Loader(SetupGen &s): - ObjectLoader(s) +SetupGen::Loader::Loader(SetupGen &s, Module &m): + ObjectLoader(s), + mod(m) { static ActionMap shared_actions; set_actions(shared_actions); @@ -197,12 +200,12 @@ void SetupGen::Loader::enum_def(const DataFile::Symbol &n) Enum en(n.name); load_sub(en); Type &type = obj.add_type(n.name, Type::ENUM); - type.set_enum(*obj.enums.emplace_back(make_unique(move(en)))); + type.set_enum(*mod.enums.emplace_back(make_unique(move(en)))); } void SetupGen::Loader::name_space(const string &ns) { - obj.name_space = ns; + mod.name_space = ns; } void SetupGen::Loader::struct_def(Struct::Kind kind, const DataFile::Symbol &n) @@ -210,5 +213,5 @@ void SetupGen::Loader::struct_def(Struct::Kind kind, const DataFile::Symbol &n) Struct sct(n.name+"Setup", kind); load_sub(sct, obj); Type &type = obj.add_type(n.name, Type::STRUCT); - type.set_struct(*obj.structs.emplace_back(make_unique(move(sct)))); + type.set_struct(*mod.structs.emplace_back(make_unique(move(sct)))); } diff --git a/tools/setupgen/setupgen.h b/tools/setupgen/setupgen.h index 98d1ab8..e4c1b33 100644 --- a/tools/setupgen/setupgen.h +++ b/tools/setupgen/setupgen.h @@ -1,12 +1,14 @@ #ifndef SETUPGEN_H_ #define SETUPGEN_H_ +#include #include #include #include #include #include #include +#include #include "enum.h" #include "struct.h" #include "type.h" @@ -14,10 +16,20 @@ class SetupGen: public Msp::RegisteredApplication { private: + struct Module + { + std::string name_space; + std::vector> enums; + std::vector> structs; + }; + class Loader: public Msp::DataFile::ObjectLoader { + private: + Module &mod; + public: - Loader(SetupGen &); + Loader(SetupGen &, Module &); private: void init_actions() override; @@ -29,7 +41,7 @@ private: std::string in_fn; std::string out_fn; - std::string name_space; + std::list modules; std::map types; std::vector> enums; std::vector> structs; @@ -46,10 +58,10 @@ public: const Type &get_type(const std::string &) const; private: - void load(); - void collect_headers(); - void generate_header(Msp::IO::Base &) const; - void generate_code(Msp::IO::Base &) const; + void load(const Msp::FS::Path &); + void collect_headers(const Module &); + void generate_header(const Module &, Msp::IO::Base &) const; + void generate_code(const Module &, Msp::IO::Base &) const; }; #endif -- 2.45.2