diff options
| author | Markus Engel <markus.engel@tum.de> | 2013-04-13 00:04:00 +0000 |
|---|---|---|
| committer | Markus Engel <markus.engel@tum.de> | 2013-04-13 00:04:00 +0000 |
| commit | 5cc7d4298f09086a9c3d97e5d75efce3a16ad9fd (patch) | |
| tree | 3c78ffba7f4cf9747d9efdfa53a859e415f61f73 /src | |
| parent | Added prefPaths to contexts; modified SPFactory (diff) | |
| download | inkscape-5cc7d4298f09086a9c3d97e5d75efce3a16ad9fd.tar.gz inkscape-5cc7d4298f09086a9c3d97e5d75efce3a16ad9fd.zip | |
Made factory a template.
(bzr r11608.1.95)
Diffstat (limited to 'src')
| -rw-r--r-- | src/sp-factory.cpp | 64 | ||||
| -rw-r--r-- | src/sp-factory.h | 58 |
2 files changed, 75 insertions, 47 deletions
diff --git a/src/sp-factory.cpp b/src/sp-factory.cpp index 389589d09..27aa6a973 100644 --- a/src/sp-factory.cpp +++ b/src/sp-factory.cpp @@ -1,34 +1,34 @@ #include "sp-factory.h" -#include <stdexcept> - -#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 throw() { - return type.c_str(); -} - -SPFactory& SPFactory::instance() { - static SPFactory factory; - return factory; -} - -bool SPFactory::registerObject(const std::string& id, CreateFunction* createFunction) { - return this->objectMap.insert(std::make_pair(id, createFunction)).second; -} - -SPObject* SPFactory::createObject(const std::string& id) const { - std::map<const std::string, CreateFunction*>::const_iterator it = this->objectMap.find(id); - - if (it == this->objectMap.end()) { - throw TypeNotRegistered(id); - } - - return (*it).second(); -} +//#include <stdexcept> + +//#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 throw() { +// return type.c_str(); +//} + +//SPFactory& SPFactory::instance() { +// static SPFactory factory; +// return factory; +//} + +//bool SPFactory::registerObject(const std::string& id, CreateFunction* createFunction) { +// return this->objectMap.insert(std::make_pair(id, createFunction)).second; +//} + +//SPObject* SPFactory::createObject(const std::string& id) const { +// std::map<const std::string, CreateFunction*>::const_iterator it = this->objectMap.find(id); +// +// if (it == this->objectMap.end()) { +// throw TypeNotRegistered(id); +// } +// +// return (*it).second(); +//} diff --git a/src/sp-factory.h b/src/sp-factory.h index 645644412..111375415 100644 --- a/src/sp-factory.h +++ b/src/sp-factory.h @@ -4,40 +4,68 @@ #include <map> #include <string> -class SPObject; - -namespace Inkscape { - namespace XML { - class Node; +/** + * A simple singleton implementation. + */ +template<class T> +struct Singleton { + static T& instance() { + static T i; + return i; } -} +}; /** - * Factory for creating objects from the SPObject tree. + * A Factory for creating objects which can be identified by strings. */ -class SPFactory { +template<class BaseObject> +class Factory : public Singleton< Factory<BaseObject> > { public: class TypeNotRegistered : public std::exception { public: - TypeNotRegistered(const std::string& type); - const char* what() const throw(); + TypeNotRegistered(const std::string& typeString) : std::exception(), typeString(typeString) { + } + + virtual ~TypeNotRegistered() throw() { + } + + const char* what() const throw() { + return typeString.c_str(); + } private: - const std::string type; + const std::string typeString; }; - static SPFactory& instance(); + typedef BaseObject* CreateFunction(); - typedef SPObject* CreateFunction(); + bool registerObject(const std::string& id, CreateFunction* createFunction) { + return this->objectMap.insert(std::make_pair(id, createFunction)).second; + } + + BaseObject* createObject(const std::string& id) const { + typename std::map<const std::string, CreateFunction*>::const_iterator it = this->objectMap.find(id); - bool registerObject(const std::string& id, CreateFunction* createFunction); - SPObject* createObject(const std::string& id) const; + if (it == this->objectMap.end()) { + throw TypeNotRegistered(id); + } + + return it->second(); + } private: std::map<const std::string, CreateFunction*> objectMap; }; +class SPObject; +typedef Factory<SPObject> SPFactory; + +class SPEventContext; +typedef Factory<SPEventContext> ToolFactory; + + + #include "xml/node.h" struct NodeTraits { |
