summaryrefslogtreecommitdiffstats
path: root/src/sp-factory.cpp
diff options
context:
space:
mode:
authorMarkus Engel <markus.engel@tum.de>2013-04-07 23:31:41 +0000
committerMarkus Engel <markus.engel@tum.de>2013-04-07 23:31:41 +0000
commite9d86314ef5d8aafc2ec7677fe4825a346276ce4 (patch)
tree59ebe8b86e8b25336a336e5d7465aa20435f00ca /src/sp-factory.cpp
parentMerge Object and subclasses. Merging of SP- and C-classes complete. (diff)
downloadinkscape-e9d86314ef5d8aafc2ec7677fe4825a346276ce4.tar.gz
inkscape-e9d86314ef5d8aafc2ec7677fe4825a346276ce4.zip
Added exception to SPFactory / basic handling to SPObject.
(bzr r11608.1.87)
Diffstat (limited to 'src/sp-factory.cpp')
-rw-r--r--src/sp-factory.cpp40
1 files changed, 25 insertions, 15 deletions
diff --git a/src/sp-factory.cpp b/src/sp-factory.cpp
index 29b308111..272584ca6 100644
--- a/src/sp-factory.cpp
+++ b/src/sp-factory.cpp
@@ -5,43 +5,53 @@
#include "sp-object.h"
#include "xml/node.h"
+
+SPFactory::TypeNotRegistered::TypeNotRegistered(const std::string& type)
+ : std::exception(), type(type) {
+}
+
+const char* SPFactory::TypeNotRegistered::what() const noexcept {
+ return type.c_str();
+}
+
SPFactory& SPFactory::instance() {
static SPFactory factory;
return factory;
}
-bool SPFactory::registerObject(std::string id, std::function<SPObject*()> createFunction) {
+bool SPFactory::registerObject(const std::string& id, std::function<SPObject* ()> 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::map<std::string, std::function<SPObject*()>>::const_iterator entry;
+ std::string name;
switch (id.type()) {
case Inkscape::XML::TEXT_NODE:
- entry = this->objectMap.find("string");
+ name = "string";
break;
case Inkscape::XML::ELEMENT_NODE: {
- gchar const* const type_name = id.attribute("sodipodi:type");
+ gchar const* const sptype = id.attribute("sodipodi:type");
- if (type_name) {
- entry = this->objectMap.find(type_name);
+ if (sptype) {
+ name = sptype;
} else {
- entry = this->objectMap.find(id.name());
+ name = id.name();
}
-
break;
}
default:
- entry = this->objectMap.end();
+ break;
}
- if (entry == this->objectMap.end()) {
- g_warning("Factory: Type \"%s\" not registered!", id.name());
-
- return 0;
+ try {
+ std::function<SPObject* ()> createFunction = this->objectMap.at(name);
+ return createFunction();
+ } catch (const std::out_of_range& ex) {
+ std::throw_with_nested(TypeNotRegistered(name));
}
-
- return (entry->second)();
}