3 #include <msp/core/maputils.h>
4 #include <msp/io/print.h>
10 class Struct::Field::Loader: public Msp::DataFile::ObjectLoader<Struct::Field>
13 Loader(Struct::Field &);
16 void init_actions() override;
20 Struct::Struct(const string &n, Kind k):
25 void Struct::define_type(IO::Base &out) const
27 IO::print(out, "struct %s\n{\n", name);
28 IO::print(out, "\tclass Loader;\n\n");
30 for(const Field &f: fields)
32 IO::print(out, "\t%s %s", f.type->get_cpp_type(), f.name);
33 if(!f.default_value.empty())
35 if(const string &c = f.type->get_conversion(); !c.empty())
36 IO::print(out, " = %s(%s)", f.type->get_conversion(), f.default_value);
37 else if(f.type->get_kind() == Type::AGGREGATE)
38 IO::print(out, " = { %s }", f.default_value);
39 else if(f.type->get_kind() == Type::ENUM)
40 IO::print(out, " = %s::%s", f.type->get_name(), f.default_value);
42 IO::print(out, " = %s", f.default_value);
44 IO::print(out, ";\n");
47 IO::print(out, "};\n");
50 void Struct::define_loader(IO::Base &out) const
52 IO::print(out, "class %s::Loader: public Msp::DataFile::ObjectLoader<%s>\n{\n", name, name);
53 IO::print(out, "public:\n");
54 IO::print(out, "\tLoader(%s &);\n", name);
55 IO::print(out, "\nprivate:\n");
56 IO::print(out, "\tvoid init_actions() override;\n");
57 for(const Field &f: fields)
58 if(f.type->needs_loader_function())
59 IO::print(out, "\tvoid %s(%s);\n", f.name, f.type->create_loader_params(false));
60 IO::print(out, "};\n");
63 void Struct::define_functions(IO::Base &out) const
65 IO::print(out, "%s::Loader::Loader(%s &o):\n", name, name);
66 IO::print(out, "\tObjectLoader<%s>(o)\n{\n", name);
67 IO::print(out, "\tstatic ActionMap shared_actions;\n");
68 IO::print(out, "\tset_actions(shared_actions);\n}\n");
70 IO::print(out, "\nvoid %s::Loader::init_actions()\n{\n", name);
71 for(const Field &f: fields)
73 IO::print(out, "\tadd(\"%s\", ", f.name);
74 if(f.type->needs_loader_function())
75 IO::print(out, "&Loader::%s", f.name);
77 IO::print(out, "&%s::%s", name, f.name);
78 IO::print(out, ");\n");
80 IO::print(out, "}\n");
82 for(const Field &f: fields)
83 if(f.type->needs_loader_function())
85 IO::print(out, "\nvoid %s::Loader::%s(%s)\n{\n", name, f.name, f.type->create_loader_params(true));
86 IO::print(out, "\tauto &_v = obj.%s;\n", f.name);
87 IO::print(out, "\t%s\n", f.type->create_loader_statement());
88 IO::print(out, "}\n");
93 Struct::Loader::Loader(Struct &s, const SetupGen &g):
94 ObjectLoader<Struct>(s),
97 static ActionMap shared_actions;
98 set_actions(shared_actions);
101 void Struct::Loader::init_actions()
103 add("field", &Loader::field);
106 void Struct::Loader::field(const DataFile::Symbol &n, const DataFile::Symbol &t)
108 if(ranges::any_of(obj.fields, [&n](const Field &f){ return f.name==n.name; }))
109 throw key_error(n.name);
112 fld.type=&gen.get_type(t.name);
114 obj.fields.emplace_back(move(fld));
118 Struct::Field::Loader::Loader(Field &f):
119 ObjectLoader<Field>(f)
121 static ActionMap shared_actions;
122 set_actions(shared_actions);
125 void Struct::Field::Loader::init_actions()
127 add("default", &Field::default_value);