diff options
author | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-08 15:22:31 +0100 |
---|---|---|
committer | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-08 15:22:31 +0100 |
commit | d3fec0515174d4599dbacc712d4a13429e6bd053 (patch) | |
tree | 9d8143c36bab99bdc307d149e347436f0c240e74 | |
parent | 8fe7f4f0f2d9a3864c21426df99569481882503b (diff) | |
download | dbmodel-d3fec0515174d4599dbacc712d4a13429e6bd053.tar.gz dbmodel-d3fec0515174d4599dbacc712d4a13429e6bd053.tar.bz2 |
Refactor Singelton/Factory out of DiagramItemFactory
-rw-r--r-- | src/diagramdocument.cpp | 2 | ||||
-rw-r--r-- | src/diagramitem.cpp | 2 | ||||
-rw-r--r-- | src/diagramitemfactory.cpp | 23 | ||||
-rw-r--r-- | src/diagramitemfactory.h | 17 | ||||
-rw-r--r-- | src/utils/factory.h | 49 | ||||
-rw-r--r-- | src/utils/singelton.h | 32 | ||||
-rw-r--r-- | src/utils/utils.pri | 4 |
7 files changed, 94 insertions, 35 deletions
diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp index d48e90b..71b38db 100644 --- a/src/diagramdocument.cpp +++ b/src/diagramdocument.cpp @@ -243,7 +243,7 @@ DiagramDocument::load(const QString &fileName) QDomElement itemElement = itemListElement.firstChildElement("item"); while (!itemElement.isNull()) { QString itemTypeName = itemElement.attribute("type"); - DiagramItem *item = DiagramItemFactory::createItem(itemTypeName); + DiagramItem *item = DiagramItemFactory::create(itemTypeName); if (item == NULL) { qWarning() << "Unknown item type:" << itemTypeName; } diff --git a/src/diagramitem.cpp b/src/diagramitem.cpp index 41e028f..1d75039 100644 --- a/src/diagramitem.cpp +++ b/src/diagramitem.cpp @@ -101,7 +101,7 @@ DiagramItem::fromMimeData(const QMimeData *mimeData) if (element.isNull()) return 0; - DiagramItem *item = DiagramItemFactory::createItem(element.attribute("type")); + DiagramItem *item = DiagramItemFactory::create(element.attribute("type")); if (item == NULL) return 0; diff --git a/src/diagramitemfactory.cpp b/src/diagramitemfactory.cpp index 093d980..a48ed14 100644 --- a/src/diagramitemfactory.cpp +++ b/src/diagramitemfactory.cpp @@ -15,32 +15,13 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include <QDebug> -#include "diagramitem.h" #include "diagramitemfactory.h" -DiagramItemFactory *DiagramItemFactory::m_registry = 0; - -template <class T> DiagramItem *__createItem() { return new T(); } -#define ADD_TYPE(x) m_map[x::staticTypeName()] = &(__createItem<x>); - -DiagramItem * -DiagramItemFactory::createItem(const QString &typeName) -{ - if (!DiagramItemFactory::m_registry) - DiagramItemFactory::m_registry = new DiagramItemFactory(); - if (!DiagramItemFactory::m_registry->m_map.contains(typeName)) - return 0; - return DiagramItemFactory::m_registry->m_map[typeName](); -} - -// ========================================================================= -// ========================================================================= - #include "items/database/databasetable.h" #include "items/database/databaserelationship.h" DiagramItemFactory::DiagramItemFactory() { - ADD_TYPE(DatabaseTable); - ADD_TYPE(DatabaseRelationship); + FACTORY_ADD_TYPE(DatabaseTable) + FACTORY_ADD_TYPE(DatabaseRelationship) } diff --git a/src/diagramitemfactory.h b/src/diagramitemfactory.h index eaaae2f..274957e 100644 --- a/src/diagramitemfactory.h +++ b/src/diagramitemfactory.h @@ -14,23 +14,18 @@ // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -#ifndef DIAGRAMITEMREGISTRY_H -#define DIAGRAMITEMREGISTRY_H +#ifndef DIAGRAMITEMFACTORY_H +#define DIAGRAMITEMFACTORY_H -#include <QString> -class DiagramItem; +#include "utils/factory.h" -typedef DiagramItem *(*createItemFunc)(); +class DiagramItem; -class DiagramItemFactory +class DiagramItemFactory : public Factory<DiagramItemFactory, DiagramItem> { -public: - static DiagramItem *createItem(const QString &typeName); - private: DiagramItemFactory(); - static DiagramItemFactory *m_registry; - QMap<QString, createItemFunc> m_map; + friend class Singelton<DiagramItemFactory>; }; #endif diff --git a/src/utils/factory.h b/src/utils/factory.h new file mode 100644 index 0000000..0ed4b96 --- /dev/null +++ b/src/utils/factory.h @@ -0,0 +1,49 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef FACTORY_H +#define FACTORY_H + +#include <QMap> +#include <QStringList> +#include "singelton.h" + +template <typename T, typename TI> +class Factory : public Singelton<T> +{ +public: + static TI *create(const QString &key) + { + T *inst = T::instance(); + if (!inst->m_map.contains(key)) + return NULL; + return inst->m_map[key](); + } + static QStringList keys() + { + T *inst = T::instance(); + return inst->m_map.keys(); + } +protected: + template <typename ST> void add() { m_map[ST::staticTypeName()] = &(createObject<ST>); } + template <typename ST> static TI *createObject() { return new ST(); } + typedef TI *(*CreateFunc)(); + QMap<QString, CreateFunc> m_map; +}; + +#define FACTORY_ADD_TYPE(ST) add<ST>(); + +#endif diff --git a/src/utils/singelton.h b/src/utils/singelton.h new file mode 100644 index 0000000..743c5ba --- /dev/null +++ b/src/utils/singelton.h @@ -0,0 +1,32 @@ +// Copyright (C) 2008 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef SINGELTON_H +#define SINGELTON_H + +template <typename T> +class Singelton +{ +public: + static T *instance() { + static T *_instance = NULL; + if (_instance == NULL) + _instance = new T(); + return _instance; + } +}; + +#endif diff --git a/src/utils/utils.pri b/src/utils/utils.pri index f178afb..05efeb6 100644 --- a/src/utils/utils.pri +++ b/src/utils/utils.pri @@ -4,4 +4,6 @@ SOURCES += \ iconprovider.cpp HEADERS += \ - iconprovider.h + iconprovider.h \ + factory.h \ + singelton.h |