blob: a1df552779c35764efe9ae18294f314745a093fa (
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/** @file
* Generic Factory
*//*
* Authors:
* Markus Engel
*
* Copyright (C) 2013 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifndef FACTORY_H_SEEN
#define FACTORY_H_SEEN
#include <exception>
#include <map>
#include <string>
#include "xml/node.h"
/**
* A simple singleton implementation.
*/
template <class T>
struct Singleton {
static T &instance() {
static T inst;
return inst;
}
};
namespace FactoryExceptions {
class TypeNotRegistered : public std::exception {
public:
TypeNotRegistered(std::string const &type)
: std::exception()
, _type_string(type) {
}
virtual ~TypeNotRegistered() throw() {
}
char const *what() const throw() {
return _type_string.c_str();
}
private:
std::string const _type_string;
};
} // namespace FactoryExceptions
/**
* A Factory for creating objects which can be identified by strings.
*/
template <class BaseObject>
class Factory {
public:
typedef BaseObject *CreateFunction();
bool registerObject(std::string const &id, CreateFunction *creator) {
return this->_object_map.insert(std::make_pair(id, creator)).second;
}
BaseObject *createObject(std::string const &id) const {
typename std::map<std::string const, CreateFunction *>::const_iterator it = this->_object_map.find(id);
if (it == this->_object_map.end()) {
throw FactoryExceptions::TypeNotRegistered(id);
}
return it->second();
}
private:
std::map<std::string const, CreateFunction *> _object_map;
};
struct NodeTraits {
static std::string get_type_string(Inkscape::XML::Node const &node) {
std::string name;
switch (node.type()) {
case Inkscape::XML::TEXT_NODE:
name = "string";
break;
case Inkscape::XML::ELEMENT_NODE: {
gchar const *const sptype = node.attribute("sodipodi:type");
if (sptype) {
name = sptype;
} else {
name = node.name();
}
break;
}
default:
name = "";
break;
}
return name;
}
};
#endif
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|