X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FTypeDescription.cpp;h=bd63d70137b5f0a27c2f6939a6bc14e11d05d692;hb=0e7b9eca67383e45e04aa419cb783c92722f7801;hp=27906fab758b486c247edcc0d86be6b6d03f2591;hpb=4bc70f5311dcbcca4e6b9e852bbcb19602f50eeb;p=l2e.git diff --git a/src/loader/TypeDescription.cpp b/src/loader/TypeDescription.cpp index 27906fa..bd63d70 100644 --- a/src/loader/TypeDescription.cpp +++ b/src/loader/TypeDescription.cpp @@ -1,21 +1,18 @@ -/* - * TypeDescription.cpp - * - * Created on: Sep 4, 2012 - * Author: holy - */ - #include "TypeDescription.h" #include #include #include #include +#include #include using std::endl; +using std::invalid_argument; +using std::make_pair; using std::map; using std::string; +using std::stringstream; using std::vector; namespace loader { @@ -49,6 +46,12 @@ void TypeDescription::Construct(void *data) const { } } +void TypeDescription::Init(void *data) const { + if (initializer) { + (*initializer)(data); + } +} + void TypeDescription::Load(void *data) const { if (loader) { (*loader)(data); @@ -69,39 +72,66 @@ std::ptrdiff_t TypeDescription::SupertypeOffset(int id) const { } -vector TypeDescription::typeDescriptions; +map TypeDescription::typeDescriptions; +map TypeDescription::typeName2ID; -TypeDescription &TypeDescription::CreateOrGet(const std::string &name) { - for (vector::iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) { - if (i->name == name) { - return *i; - } +TypeDescription &TypeDescription::Create(int id, const std::string &name) { + if (typeDescriptions.count(id)) { + std::stringstream msg; + msg << "duplicate type ID " << id + << " (have " << Get(id).TypeName() << ", got " << name << ")"; + throw std::invalid_argument(msg.str()); + } + if (typeName2ID.count(name)) { + std::stringstream msg; + msg << "duplicate type name " << name + << " (have " << GetTypeId(name) << ", got " << id << ")"; + throw std::invalid_argument(msg.str()); } - typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name)); - return typeDescriptions.back(); + typeName2ID[name] = id; + return typeDescriptions.insert(make_pair(id, TypeDescription(id, name))).first->second; } int TypeDescription::GetTypeId(const std::string &name) { - for (vector::size_type i(0), end(typeDescriptions.size()); i < end; ++i) { - if (typeDescriptions[i].name == name) { - return i; - } + map::const_iterator i(typeName2ID.find(name)); + if (i != typeName2ID.end()) { + return i->second; + } else { + throw invalid_argument("unknown type name " + name); } - typeDescriptions.push_back(TypeDescription(typeDescriptions.size(), name)); - return typeDescriptions.size() - 1; } const TypeDescription &TypeDescription::Get(int id) { - assert(id >= 0 && id < int(typeDescriptions.size())); - return typeDescriptions[id]; + map::const_iterator i(typeDescriptions.find(id)); + if (i != typeDescriptions.end()) { + return i->second; + } else { + std::stringstream msg; + msg << "invalid type ID " << id; + throw invalid_argument(msg.str()); + } +} + + +int TypeDescription::GetMaxSize() { + int max = 0; + for (map::const_iterator + i(typeDescriptions.begin()), + end(typeDescriptions.end()); + i != end; ++i) { + if (i->second.Size() > max) { + max = i->second.Size(); + } + } + return max; } void TypeDescription::WriteSourceWiki(std::ostream &out) { vector types; - for (vector::const_iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) { - if (i->name != "Animation") { - types.push_back(i->name); + for (map::const_iterator i(typeDescriptions.begin()), end(typeDescriptions.end()); i != end; ++i) { + if (i->second.name != "Animation") { + types.push_back(i->second.name); } } std::sort(types.begin(), types.end()); @@ -117,6 +147,8 @@ void TypeDescription::WriteSourceWiki(std::ostream &out) { const TypeDescription &td(Get(GetTypeId(*i))); out << "h3. " << td.TypeName() << endl << endl; + out << "Type ID: @" << td.TypeId() << "@" << endl << endl; + if (td.Description()) { out << td.Description() << endl << endl; }