1 #include "TypeDescription.h"
11 using std::invalid_argument;
15 using std::stringstream;
20 void TypeDescription::AddField(const std::string &n, const FieldDescription &f) {
22 throw std::invalid_argument("duplicate definition of field " + n + " of type " + name);
24 fields.insert(std::make_pair(n, f));
28 bool TypeDescription::HasField(const std::string &name) const {
29 return fields.count(name);
32 const FieldDescription &TypeDescription::GetField(const std::string &n) const {
33 map<string, FieldDescription>::const_iterator result(fields.find(n));
34 if (result != fields.end()) {
35 return result->second;
37 throw std::invalid_argument("undefined field " + n + " of type " + name);
41 void TypeDescription::Construct(void *data) const {
45 std::memset(data, 0, Size());
49 void TypeDescription::Load(void *data) const {
56 void TypeDescription::AddSupertype(int id, std::ptrdiff_t offset) {
57 supertypes[id] = offset;
60 bool TypeDescription::IsSubtypeOf(int id) const {
61 return supertypes.count(id);
64 std::ptrdiff_t TypeDescription::SupertypeOffset(int id) const {
65 return supertypes.at(id);
69 map<int, TypeDescription> TypeDescription::typeDescriptions;
70 map<string, int> TypeDescription::typeName2ID;
72 TypeDescription &TypeDescription::Create(int id, const std::string &name) {
73 if (typeDescriptions.count(id)) {
74 std::stringstream msg;
75 msg << "duplicate type ID " << id
76 << " (have " << Get(id).TypeName() << ", got " << name << ")";
77 throw std::invalid_argument(msg.str());
79 if (typeName2ID.count(name)) {
80 std::stringstream msg;
81 msg << "duplicate type name " << name
82 << " (have " << GetTypeId(name) << ", got " << id << ")";
83 throw std::invalid_argument(msg.str());
85 typeName2ID[name] = id;
86 return typeDescriptions.insert(make_pair(id, TypeDescription(id, name))).first->second;
89 int TypeDescription::GetTypeId(const std::string &name) {
90 map<string, int>::const_iterator i(typeName2ID.find(name));
91 if (i != typeName2ID.end()) {
94 throw invalid_argument("unknown type name " + name);
98 const TypeDescription &TypeDescription::Get(int id) {
99 map<int, TypeDescription>::const_iterator i(typeDescriptions.find(id));
100 if (i != typeDescriptions.end()) {
103 std::stringstream msg;
104 msg << "invalid type ID " << id;
105 throw invalid_argument(msg.str());
110 void TypeDescription::WriteSourceWiki(std::ostream &out) {
111 vector<string> types;
112 for (map<int, TypeDescription>::const_iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) {
113 if (i->second.name != "Animation") {
114 types.push_back(i->second.name);
117 std::sort(types.begin(), types.end());
119 out << "h2. Data types" << endl << endl;
121 for (vector<string>::const_iterator i(types.begin()), end(types.end()); i != end; ++i) {
122 out << "* [[LoaderSource#" << *i << "|" << *i << "]]" << endl;
126 for (vector<string>::const_iterator i(types.begin()), end(types.end()); i != end; ++i) {
127 const TypeDescription &td(Get(GetTypeId(*i)));
128 out << "h3. " << td.TypeName() << endl << endl;
130 out << "Type ID: @" << td.TypeId() << "@" << endl << endl;
132 if (td.Description()) {
133 out << td.Description() << endl << endl;
136 if (td.FieldsBegin() == td.FieldsEnd()) {
137 out << "No properties." << endl << endl;
139 out << "| *Property* | *Type* | *Description* |" << endl;
140 for (FieldIterator field(td.FieldsBegin()); field != td.FieldsEnd(); ++field) {
141 const FieldDescription &fd(field->second);
142 out << "| " << field->first << " | ";
143 if (fd.IsAggregate()) {
144 out << "Array<" << Get(fd.TypeId()).TypeName() << ">";
146 out << Get(fd.TypeId()).TypeName();
149 if (fd.Description()) {
150 out << fd.Description();