Revision b9dcff08
Von Christian Ehringfeld vor etwa 10 Jahren hinzugefügt
| src/entityinstancefactory.cpp | ||
|---|---|---|
|
#include "entityhelper.h"
|
||
|
#include <QMetaType>
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
QHash<QByteArray, EntityInstanceFactory::Constructor>
|
||
|
EntityInstanceFactory::instance =
|
||
|
QHash<QByteArray, EntityInstanceFactory::Constructor>();
|
||
|
|
||
|
EntityInstanceFactory::EntityInstanceFactory() {
|
||
|
|
||
|
}
|
||
| src/entityinstancefactory.h | ||
|---|---|---|
|
//http://www.mimec.org/node/350
|
||
|
template<typename T>
|
||
|
static void registerClass() {
|
||
|
constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
|
||
|
EntityInstanceFactory::instance.insert( T::staticMetaObject.className(), &constructorHelper<T> );
|
||
|
QString lName = "QList<QSharedPointer<";
|
||
|
lName.append(T::staticMetaObject.className());
|
||
|
lName.append(">>");
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
static Entity *createObject( const QByteArray &className) {
|
||
|
Constructor constructor = constructors().value( className );
|
||
|
if ( constructor == NULL ) {
|
||
|
return NULL;
|
||
|
Constructor constructor = EntityInstanceFactory::instance.value( className );
|
||
|
if ( constructor == nullptr ) {
|
||
|
return nullptr;
|
||
|
}
|
||
|
return (*constructor)();
|
||
|
}
|
||
| ... | ... | |
|
static Entity *constructorHelper() {
|
||
|
return new T();
|
||
|
}
|
||
|
|
||
|
static QHash<QByteArray, Constructor> &constructors() {
|
||
|
static QHash<QByteArray, Constructor> instance;
|
||
|
return instance;
|
||
|
}
|
||
|
static QHash<QByteArray, Constructor> instance;
|
||
|
|
||
|
protected:
|
||
|
EntityInstanceFactory();
|
||
Auch abrufbar als: Unified diff
fix