diff options
Diffstat (limited to 'src/utils/factory.h')
-rw-r--r-- | src/utils/factory.h | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/utils/factory.h b/src/utils/factory.h index 0ed4b96..e9c86a5 100644 --- a/src/utils/factory.h +++ b/src/utils/factory.h @@ -21,7 +21,24 @@ #include <QStringList> #include "singelton.h" -template <typename T, typename TI> +template <typename TI> +class FactoryHelperBase +{ +public: + virtual TI *create() = 0; +}; + +template <typename TI, typename ST> +class FactoryHelper : public FactoryHelperBase<TI> +{ +public: + TI *create() + { + return new ST(); + } +}; + +template <typename T, typename TI, typename Helper> class Factory : public Singelton<T> { public: @@ -30,7 +47,7 @@ public: T *inst = T::instance(); if (!inst->m_map.contains(key)) return NULL; - return inst->m_map[key](); + return inst->m_map[key].create(); } static QStringList keys() { @@ -38,10 +55,9 @@ public: 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(); } + template <typename ST> void add() { m_map[ST::staticTypeName()] = Helper<TI, ST>(); } typedef TI *(*CreateFunc)(); - QMap<QString, CreateFunc> m_map; + QMap<QString, FactoryHelperBase> m_map; }; #define FACTORY_ADD_TYPE(ST) add<ST>(); |