Revision 1cee0f5b
Von Christian Ehringfeld vor fast 9 Jahren hinzugefügt
src/entityinstancefactory.h | ||
---|---|---|
namespace CuteEntityManager {
|
||
class Entity;
|
||
class EntityInstanceFactory {
|
||
public:
|
||
public:
|
||
static Entity *createInstance(const char *className);
|
||
static Entity *createInstance(const QString &className);
|
||
static Entity *createInstance(int metaTypeId);
|
||
... | ... | |
static Entity *createInstance() {
|
||
return EntityInstanceFactory::createInstance(qMetaTypeId<T>());
|
||
}
|
||
protected:
|
||
|
||
//http://www.mimec.org/node/350
|
||
template<typename T>
|
||
static void registerClass()
|
||
{
|
||
constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
|
||
}
|
||
|
||
static Entity* createObject( const QByteArray& className)
|
||
{
|
||
Constructor constructor = constructors().value( className );
|
||
if ( constructor == NULL )
|
||
return NULL;
|
||
return (*constructor)();
|
||
}
|
||
|
||
private:
|
||
typedef Entity* (*Constructor)();
|
||
|
||
template<typename T>
|
||
static Entity* constructorHelper()
|
||
{
|
||
return new T();
|
||
}
|
||
|
||
static QHash<QByteArray, Constructor>& constructors()
|
||
{
|
||
static QHash<QByteArray, Constructor> instance;
|
||
return instance;
|
||
}
|
||
|
||
protected:
|
||
EntityInstanceFactory();
|
||
};
|
||
}
|
Auch abrufbar als: Unified diff
stupid stuff