#ifndef OBJECTFACTORY_H #define OBJECTFACTORY_H #include #include #include class DoricObjectFactory { public: template static void registerClass(QString name) { constructors().insert(name, &constructorHelper); } static bool acquireClass(QString name) { return constructors().keys().contains(name); } static QObject *createObject(const QString &name, QObject *parent = NULL) { Constructor constructor = constructors().value(name); if (constructor == NULL) return NULL; return (*constructor)(parent); } private: typedef QObject *(*Constructor)(QObject *parent); template static QObject *constructorHelper(QObject *parent) { return new T(parent); } static QHash &constructors() { static QHash instance; return instance; } }; #endif // OBJECTFACTORY_H