diff options
Diffstat (limited to 'src/sp-factory.cpp')
| -rw-r--r-- | src/sp-factory.cpp | 39 |
1 files changed, 8 insertions, 31 deletions
diff --git a/src/sp-factory.cpp b/src/sp-factory.cpp index 272584ca6..389589d09 100644 --- a/src/sp-factory.cpp +++ b/src/sp-factory.cpp @@ -10,7 +10,7 @@ SPFactory::TypeNotRegistered::TypeNotRegistered(const std::string& type) : std::exception(), type(type) { } -const char* SPFactory::TypeNotRegistered::what() const noexcept { +const char* SPFactory::TypeNotRegistered::what() const throw() { return type.c_str(); } @@ -19,39 +19,16 @@ SPFactory& SPFactory::instance() { return factory; } -bool SPFactory::registerObject(const std::string& id, std::function<SPObject* ()> createFunction) { +bool SPFactory::registerObject(const std::string& id, CreateFunction* createFunction) { return this->objectMap.insert(std::make_pair(id, createFunction)).second; - - // replace when gcc supports this - //return this->objectMap.emplace(id, createFunction).second; } -SPObject* SPFactory::createObject(const Inkscape::XML::Node& id) const { - std::string name; - - switch (id.type()) { - case Inkscape::XML::TEXT_NODE: - name = "string"; - break; - - case Inkscape::XML::ELEMENT_NODE: { - gchar const* const sptype = id.attribute("sodipodi:type"); - - if (sptype) { - name = sptype; - } else { - name = id.name(); - } - break; - } - default: - break; - } +SPObject* SPFactory::createObject(const std::string& id) const { + std::map<const std::string, CreateFunction*>::const_iterator it = this->objectMap.find(id); - try { - std::function<SPObject* ()> createFunction = this->objectMap.at(name); - return createFunction(); - } catch (const std::out_of_range& ex) { - std::throw_with_nested(TypeNotRegistered(name)); + if (it == this->objectMap.end()) { + throw TypeNotRegistered(id); } + + return (*it).second(); } |
