From 9f352d64f920f46a2d5b4fe67408154629933293 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 7 Oct 2012 21:00:48 +0200 Subject: [PATCH] added loading callback for type descriptions this can be used if additional setup is needed after raw data loading is complete --- src/loader/Interpreter.cpp | 16 ++++++---------- src/loader/TypeDescription.cpp | 6 ++++++ src/loader/TypeDescription.h | 5 ++++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 28e9b39..0c17bd5 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -47,7 +47,6 @@ using graphics::SimpleAnimation; using graphics::Sprite; using geometry::Vector; using std::make_pair; -using std::map; using std::set; using std::string; using std::vector; @@ -58,11 +57,11 @@ Interpreter::~Interpreter() { for (vector::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) { delete i->identifier; } - for (map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { + for (std::map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { SDL_FreeSurface(i->second); } // TODO: maybe need to reverse the array deletion check if most objects turn out to be arrays (of char) - for (map >::const_iterator i(values.begin()), end(values.end()); i != end; ++i) { + for (std::map >::const_iterator i(values.begin()), end(values.end()); i != end; ++i) { for (vector::const_iterator j(i->second.begin()), end(i->second.end()); j != end; ++j) { delete[] reinterpret_cast(*j); } @@ -71,16 +70,12 @@ Interpreter::~Interpreter() { const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const { - try { - return parsedDefinitions.at(identifier); - } catch (...) { - throw std::runtime_error("cannot find definition for " + identifier); - } + return parsedDefinitions.at(identifier); } void *Interpreter::GetObject(int typeId, const std::string &name) { - map::const_iterator i(parsedDefinitions.find(name)); + std::map::const_iterator i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { const TypeDescription &requested(TypeDescription::Get(typeId)); const TypeDescription &actual(TypeDescription::Get(i->second.type)); @@ -324,11 +319,12 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis Postpone(typeId, id, fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced()); } } + td.Load(object); } SDL_Surface *Interpreter::GetImage(const string &path) { - map::const_iterator result(imageCache.find(path)); + std::map::const_iterator result(imageCache.find(path)); if (result != imageCache.end()) { return result->second; } else { diff --git a/src/loader/TypeDescription.cpp b/src/loader/TypeDescription.cpp index ed88e8e..8fd95d7 100644 --- a/src/loader/TypeDescription.cpp +++ b/src/loader/TypeDescription.cpp @@ -46,6 +46,12 @@ void TypeDescription::Construct(void *data) const { } } +void TypeDescription::Load(void *data) const { + if (loader) { + (*loader)(data); + } +} + void TypeDescription::AddSupertype(int id, std::ptrdiff_t offset) { supertypes[id] = offset; diff --git a/src/loader/TypeDescription.h b/src/loader/TypeDescription.h index b7f4ba9..edd0177 100644 --- a/src/loader/TypeDescription.h +++ b/src/loader/TypeDescription.h @@ -40,8 +40,10 @@ public: bool HasField(const std::string &name) const; const FieldDescription &GetField(const std::string &name) const; void Construct(void *) const; + void Load(void *) const; void SetConstructor(void (*ctor)(void *)) { constructor = ctor; } + void SetLoader(void (*ld)(void *)) { loader = ld; } void AddSupertype(int id, std::ptrdiff_t offset); bool IsSubtypeOf(int id) const; bool IsSubtypeOf(const TypeDescription &other) const { return IsSubtypeOf(other.TypeId()); } @@ -63,10 +65,11 @@ public: static const TypeDescription &Get(int id); private: - TypeDescription(int id, const std::string &name) : constructor(0), name(name), id(id), size(0) { } + TypeDescription(int id, const std::string &name) : constructor(0), loader(0), name(name), id(id), size(0) { } private: void (*constructor)(void *); + void (*loader)(void *); std::string name; std::map fields; std::map supertypes; -- 2.39.2