2 #include <msp/core/getopt.h>
3 #include <msp/core/maputils.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/console.h>
6 #include <msp/io/print.h>
7 #include <msp/strings/utils.h>
12 SetupGen::SetupGen(int argc, char **argv)
15 getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
16 getopt.add_argument("input_file", in_fn, GetOpt::REQUIRED_ARG);
22 create_standard_types();
25 const Module &main_mod = modules.front();
26 collect_headers(main_mod);
30 generate_header(main_mod, IO::cout);
31 generate_code(main_mod, IO::cout);
35 string out_base = FS::basename(out_fn);
36 string out_ext = FS::extpart(out_base);
37 if(out_ext==".cpp" || out_ext==".h")
39 FS::Path out_dir = FS::dirname(out_fn);
40 out_base = FS::basepart(out_base);
41 IO::BufferedFile header_out((out_dir/(out_base+".h")).str(), IO::M_WRITE);
42 generate_header(main_mod, header_out);
43 IO::BufferedFile code_out((out_dir/(out_base+".cpp")).str(), IO::M_WRITE);
44 IO::print(code_out, "#include \"%s.h\"\n", out_base);
45 if(!main_mod.enums.empty())
46 IO::print(code_out, "#include <msp/strings/format.h>\n");
47 generate_code(main_mod, code_out);
51 IO::BufferedFile out(out_fn, IO::M_WRITE);
52 generate_header(main_mod, out);
54 generate_code(main_mod, out);
61 void SetupGen::create_standard_types()
63 add_type("bool", Type::VALUE);
64 add_type("int", Type::VALUE);
65 add_type("uint", Type::VALUE).set_cpp_type("unsigned");
66 const Type &float_type = add_type("float", Type::VALUE);
67 add_type("string", Type::VALUE).set_cpp_type("std::string", "string");
68 add_type("angle", Type::VALUE).set_cpp_type("Msp::Geometry::Angle<float>", "msp/geometry/angle.h")
69 .set_load("float", "Msp::Geometry::Angle<float>::from_degrees");
70 add_type("vector2", Type::AGGREGATE).set_cpp_type("Msp::LinAl::Vector<float, 2>", "msp/linal/vector.h")
71 .set_elements(float_type, 2);
72 add_type("vector3", Type::AGGREGATE).set_cpp_type("Msp::LinAl::Vector<float, 3>", "msp/linal/vector.h")
73 .set_elements(float_type, 3);
76 Type &SetupGen::add_type(const string &n, Type::Kind k)
78 auto result = types.try_emplace(n, n, k);
81 return result.first->second;
84 const Type &SetupGen::get_type(const string &n) const
86 return get_item(types, n);
89 void SetupGen::load(const FS::Path &fn)
91 Module &mod = modules.emplace_back();
92 IO::BufferedFile in_file(fn.str());
93 DataFile::Parser parser(in_file, fn.str());
94 Loader ldr(*this, mod);
98 void SetupGen::collect_headers(const Module &mod)
100 headers.insert("msp/datafile/objectloader.h");
101 headers.insert("msp/strings/lexicalcast.h");
102 for(const unique_ptr<Struct> &s: mod.structs)
103 for(const Struct::Field &f: s->get_fields())
104 if(const string &h = f.type->get_header(); !h.empty())
108 void SetupGen::generate_header(const Module &mod, IO::Base &out) const
110 string guard = toupper(FS::basepart(FS::basename(out_fn.empty() ? in_fn : out_fn)))+"_H_";
111 if(!mod.name_space.empty())
113 vector<string> parts = split(mod.name_space, "::");
114 parts.emplace_back(move(guard));
115 guard = join(parts.begin(), parts.end(), "_");
117 IO::print(out, "#ifndef %s\n", guard);
118 IO::print(out, "#define %s\n", guard);
123 for(const string &h: headers)
124 IO::print(out, "#include <%s>\n", h);
127 if(!mod.name_space.empty())
128 IO::print(out, "\nnamespace %s {\n", mod.name_space);
130 for(const unique_ptr<Enum> &e: mod.enums)
136 for(const unique_ptr<Struct> &s: mod.structs)
142 for(const unique_ptr<Struct> &s: mod.structs)
145 s->define_loader(out);
148 for(const unique_ptr<Enum> &e: mod.enums)
151 e->declare_conversions(out);
154 if(!mod.name_space.empty())
155 IO::print(out, "\n} // namespace %s\n", mod.name_space);
157 IO::print(out, "\n#endif\n");
160 void SetupGen::generate_code(const Module &mod, IO::Base &out) const
162 if(!mod.name_space.empty())
163 IO::print(out, "\nnamespace %s {\n", mod.name_space);
165 for(const unique_ptr<Struct> &s: mod.structs)
168 s->define_functions(out);
171 for(const unique_ptr<Enum> &e: mod.enums)
174 e->define_conversions(out);
177 if(!mod.name_space.empty())
178 IO::print(out, "\n} // namespace %s\n", mod.name_space);
182 SetupGen::Loader::Loader(SetupGen &s, Module &m):
183 ObjectLoader<SetupGen>(s),
186 static ActionMap shared_actions;
187 set_actions(shared_actions);
190 void SetupGen::Loader::init_actions()
192 add("component", &Loader::struct_def, Struct::COMPONENT);
193 add("entity", &Loader::struct_def, Struct::ENTITY);
194 add("enum", &Loader::enum_def);
195 add("namespace", &Loader::name_space);
198 void SetupGen::Loader::enum_def(const DataFile::Symbol &n)
202 Type &type = obj.add_type(n.name, Type::ENUM);
203 type.set_enum(*mod.enums.emplace_back(make_unique<Enum>(move(en))));
206 void SetupGen::Loader::name_space(const string &ns)
211 void SetupGen::Loader::struct_def(Struct::Kind kind, const DataFile::Symbol &n)
213 Struct sct(n.name+"Setup", kind);
215 Type &type = obj.add_type(n.name, Type::STRUCT);
216 type.set_struct(*mod.structs.emplace_back(make_unique<Struct>(move(sct))));