add shader plugin reg
This commit is contained in:
46
doric-Qt/doric/utils/object_factory.h
Normal file
46
doric-Qt/doric/utils/object_factory.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <QByteArray>
|
||||
#include <QMetaObject>
|
||||
#include <QHash>
|
||||
|
||||
#ifndef OBJECTFACTORY_H
|
||||
#define OBJECTFACTORY_H
|
||||
|
||||
class ObjectFactory
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
static void registerClass(QString name)
|
||||
{
|
||||
constructors().insert(name, &constructorHelper<T>);
|
||||
}
|
||||
|
||||
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<typename T>
|
||||
static QObject* constructorHelper(QObject* parent)
|
||||
{
|
||||
return new T(parent);
|
||||
}
|
||||
|
||||
static QHash<QString, Constructor>& constructors()
|
||||
{
|
||||
static QHash<QString, Constructor> instance;
|
||||
return instance;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // OBJECTFACTORY_H
|
Reference in New Issue
Block a user