summaryrefslogtreecommitdiffstats
path: root/src/sp-factory.cpp
blob: 29b308111ce8ebb93aff47458097032d19922fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "sp-factory.h"

#include <stdexcept>

#include "sp-object.h"
#include "xml/node.h"

SPFactory& SPFactory::instance() {
	static SPFactory factory;
	return factory;
}

bool SPFactory::registerObject(std::string id, std::function<SPObject*()> createFunction) {
	return this->objectMap.insert(std::make_pair(id, createFunction)).second;
}

SPObject* SPFactory::createObject(const Inkscape::XML::Node& id) const {
	std::map<std::string, std::function<SPObject*()>>::const_iterator entry;

	switch (id.type()) {
		case Inkscape::XML::TEXT_NODE:
			entry = this->objectMap.find("string");
			break;

		case Inkscape::XML::ELEMENT_NODE: {
			gchar const* const type_name = id.attribute("sodipodi:type");

			if (type_name) {
				entry = this->objectMap.find(type_name);
			} else {
				entry = this->objectMap.find(id.name());
			}

			break;
		}
		default:
			entry = this->objectMap.end();
	}

	if (entry == this->objectMap.end()) {
		g_warning("Factory: Type \"%s\" not registered!", id.name());

		return 0;
	}

	return (entry->second)();
}