diff options
| author | Markus Engel <markus.engel@tum.de> | 2013-09-16 17:32:58 +0000 |
|---|---|---|
| committer | Markus Engel <markus.engel@tum.de> | 2013-09-16 17:32:58 +0000 |
| commit | abde5067bcfbb4c0e3ba61c6f69db7925f80600a (patch) | |
| tree | 16e2b6879acc79107afe7d314a2400d42fa7ed46 /src | |
| parent | Added runtime check in SP_-cast macros. (diff) | |
| download | inkscape-abde5067bcfbb4c0e3ba61c6f69db7925f80600a.tar.gz inkscape-abde5067bcfbb4c0e3ba61c6f69db7925f80600a.zip | |
Removed TypeInfo; adjusted Factory to meet code style conventions.
(bzr r11608.1.124)
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile_insert | 1 | ||||
| -rw-r--r-- | src/document.cpp | 2 | ||||
| -rw-r--r-- | src/factory.h | 137 | ||||
| -rw-r--r-- | src/sp-object.cpp | 4 | ||||
| -rw-r--r-- | src/sp-tref.cpp | 2 | ||||
| -rw-r--r-- | src/sp-use.cpp | 2 | ||||
| -rw-r--r-- | src/type-info.cpp | 49 | ||||
| -rw-r--r-- | src/type-info.h | 31 |
8 files changed, 84 insertions, 144 deletions
diff --git a/src/Makefile_insert b/src/Makefile_insert index c2ab95300..e719f8894 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -243,7 +243,6 @@ ink_common_sources += \ tools-switch.cpp tools-switch.h \ transf_mat_3x4.cpp transf_mat_3x4.h \ tweak-context.h tweak-context.cpp \ - type-info.h type-info.cpp \ unclump.cpp unclump.h \ undo-stack-observer.h \ unicoderange.cpp unicoderange.h \ diff --git a/src/document.cpp b/src/document.cpp index 65c4cb10a..ec831745c 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -349,7 +349,7 @@ SPDocument *SPDocument::createDoc(Inkscape::XML::Document *rdoc, document->name = g_strdup(name); // Create SPRoot element - const std::string typeString = NodeTraits::getTypeString(*rroot); + const std::string typeString = NodeTraits::get_type_string(*rroot); SPObject* rootObj = SPFactory::instance().createObject(typeString); document->root = dynamic_cast<SPRoot*>(rootObj); diff --git a/src/factory.h b/src/factory.h index ca90a6e9a..a1df55277 100644 --- a/src/factory.h +++ b/src/factory.h @@ -1,95 +1,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> +template <class T> struct Singleton { - static T& instance() { - static T inst; - return inst; - } + static T &instance() { + static T inst; + return inst; + } }; namespace FactoryExceptions { - class TypeNotRegistered : public std::exception { - public: - TypeNotRegistered(const std::string& typeString) : std::exception(), typeString(typeString) { - } +class TypeNotRegistered : public std::exception { +public: + TypeNotRegistered(std::string const &type) + : std::exception() + , _type_string(type) { + } - virtual ~TypeNotRegistered() throw() { - } + virtual ~TypeNotRegistered() throw() { + } - const char* what() const throw() { - return typeString.c_str(); - } + char const *what() const throw() { + return _type_string.c_str(); + } - private: - const std::string typeString; - }; -} +private: + std::string const _type_string; +}; +} // namespace FactoryExceptions /** * A Factory for creating objects which can be identified by strings. */ -template<class BaseObject> +template <class BaseObject> class Factory { public: - typedef BaseObject* CreateFunction(); + typedef BaseObject *CreateFunction(); - bool registerObject(const std::string& id, CreateFunction* createFunction) { - return this->objectMap.insert(std::make_pair(id, createFunction)).second; - } + bool registerObject(std::string const &id, CreateFunction *creator) { + return this->_object_map.insert(std::make_pair(id, creator)).second; + } - BaseObject* createObject(const std::string& id) const throw(FactoryExceptions::TypeNotRegistered) { - typename std::map<const std::string, CreateFunction*>::const_iterator it = this->objectMap.find(id); + 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->objectMap.end()) { - throw FactoryExceptions::TypeNotRegistered(id); - } + if (it == this->_object_map.end()) { + throw FactoryExceptions::TypeNotRegistered(id); + } - return it->second(); - } + return it->second(); + } private: - std::map<const std::string, CreateFunction*> objectMap; + std::map<std::string const, CreateFunction *> _object_map; }; -#include "xml/node.h" - struct NodeTraits { - static std::string getTypeString(const Inkscape::XML::Node& 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; - } + 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 : diff --git a/src/sp-object.cpp b/src/sp-object.cpp index 3dacc8b70..b622d14e9 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -578,7 +578,7 @@ void SPObject::child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) SPObject* object = this; try { - const std::string typeString = NodeTraits::getTypeString(*child); + const std::string typeString = NodeTraits::get_type_string(*child); SPObject* ochild = SPFactory::instance().createObject(typeString); @@ -645,7 +645,7 @@ void SPObject::build(SPDocument *document, Inkscape::XML::Node *repr) { // } try { - const std::string typeString = NodeTraits::getTypeString(*rchild); + const std::string typeString = NodeTraits::get_type_string(*rchild); SPObject* child = SPFactory::instance().createObject(typeString); diff --git a/src/sp-tref.cpp b/src/sp-tref.cpp index 97c446c33..1872cdf7c 100644 --- a/src/sp-tref.cpp +++ b/src/sp-tref.cpp @@ -405,7 +405,7 @@ void sp_tref_update_text(SPTRef *tref) Inkscape::XML::Document *xml_doc = tref->document->getReprDoc(); Inkscape::XML::Node *newStringRepr = xml_doc->createTextNode(charData.c_str()); - tref->stringChild = SPFactory::instance().createObject(NodeTraits::getTypeString(*newStringRepr)); + tref->stringChild = SPFactory::instance().createObject(NodeTraits::get_type_string(*newStringRepr)); // Add this SPString as a child of the tref tref->attach(tref->stringChild, tref->lastChild()); diff --git a/src/sp-use.cpp b/src/sp-use.cpp index 159660458..0887ab50e 100644 --- a/src/sp-use.cpp +++ b/src/sp-use.cpp @@ -453,7 +453,7 @@ sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use) // } // } - SPObject* obj = SPFactory::instance().createObject(NodeTraits::getTypeString(*childrepr)); + SPObject* obj = SPFactory::instance().createObject(NodeTraits::get_type_string(*childrepr)); if (SP_IS_ITEM(obj)) { use->child = obj; diff --git a/src/type-info.cpp b/src/type-info.cpp deleted file mode 100644 index dac61e786..000000000 --- a/src/type-info.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "type-info.h" - - -TypeInfo::TypeInfo(const std::type_info& type_info) : type_info(&type_info) { -} - -TypeInfo::TypeInfo(const TypeInfo& tinfo) : type_info(tinfo.type_info) { -} - -TypeInfo& TypeInfo::operator=(const TypeInfo& rhs) { - this->type_info = rhs.type_info; - return *this; -} - -bool TypeInfo::before(const TypeInfo& tinfo) const { - return this->type_info->before(*tinfo.type_info); -} - -const char* TypeInfo::name() const { - return this->type_info->name(); -} - -const std::type_info& TypeInfo::get() const { - return *this->type_info; -} - -bool operator==(const TypeInfo& lhs, const TypeInfo& rhs) { - return lhs.get() == rhs.get(); -} - -bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs) { - return !(lhs == rhs); -} - -bool operator<(const TypeInfo& lhs, const TypeInfo& rhs) { - return lhs.before(rhs); -} - -bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs) { - return !(lhs > rhs); -} - -bool operator>(const TypeInfo& lhs, const TypeInfo& rhs) { - return rhs < lhs; -} - -bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs) { - return !(lhs < rhs); -} diff --git a/src/type-info.h b/src/type-info.h deleted file mode 100644 index 3340e08e5..000000000 --- a/src/type-info.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include <typeinfo> - -/** - * A wrapper around typeinfo. Inspired by Andrei Alexandrescu's "Modern C++ Design". - * Used as a temporary replacement for glib's type-checking system as long as SPObject - * must not be polymorphic / new objects are instantiated by g_object_new. - */ -class TypeInfo { -public: - TypeInfo(const std::type_info& type_info); - TypeInfo(const TypeInfo& tinfo); - - TypeInfo& operator=(const TypeInfo& tinfo); - - bool before(const TypeInfo& tinfo) const; - const char* name() const; - - const std::type_info& get() const; - -private: - const std::type_info* type_info; -}; - -bool operator==(const TypeInfo& lhs, const TypeInfo& rhs); -bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs); -bool operator<(const TypeInfo& lhs, const TypeInfo& rhs); -bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs); -bool operator>(const TypeInfo& lhs, const TypeInfo& rhs); -bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs); |
