Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (3,06 KB) Statistiken
| Zweig: | Revision:
c22391b2 Christian Ehringfeld
/*
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
f4e3904b Christian Ehringfeld
#ifndef ENTITYINSTANCEFACTORY_H
#define ENTITYINSTANCEFACTORY_H
#include <QString>
#include <QVariant>
#include <QHash>

namespace CuteEntityManager {
class Entity;
class EntityInstanceFactory {
b7446f4c Christian Ehringfeld
public:
f4e3904b Christian Ehringfeld
static Entity *createInstance(const char *className);
d568923d Christian Ehringfeld
static Entity *createInstance(const QString &className);
bb5e9339 SebastianDiel
static Entity *createInstance(int metaTypeId);
e0e1ead8 Christian Ehringfeld
static Entity *createInstance(const char *className,
const QHash<QString, QVariant> &attributes);
6d91d381 Christian Ehringfeld
static Entity *setAttributes(Entity *&e,
47f9301a Christian Ehringfeld
const QHash<QString, QVariant> &attributes,
a205e8a9 Christian Ehringfeld
QHash<QString, QMetaProperty> metaprops);
6d91d381 Christian Ehringfeld
static Entity *setAttributes(Entity *&e,
47f9301a Christian Ehringfeld
const QHash<QString, QVariant> &attributes);
2d9fab10 Christian Ehringfeld
static const QString extractEntityType(const QString &s);
a1389432 Christian Ehringfeld
static Entity *newSuperClassInstance(const Entity *e);
59bf3900 Christian Ehringfeld
static Entity *createInstance(const QMetaObject *object);
d7727319 Christian Ehringfeld
static QList<QSharedPointer<Entity>> castQVariantList(QVariant &list);
static QSharedPointer<Entity> castQVariant(QVariant &entity);
a205e8a9 Christian Ehringfeld
template<typename T>
static Entity *createInstance() {
return EntityInstanceFactory::createInstance(qMetaTypeId<T>());
}
1cee0f5b Christian Ehringfeld
//http://www.mimec.org/node/350
template<typename T>
b7446f4c Christian Ehringfeld
static void registerClass() {
1cee0f5b Christian Ehringfeld
constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
3fd96253 Christian Ehringfeld
QString lName = "QList<QSharedPointer<";
lName.append(T::staticMetaObject.className());
lName.append(">>");
/**
* @brief qRegisterMetaType<QList<QSharedPointer<T> > >
* @todo would be great if we could remove this shit
*/
qRegisterMetaType<QList<QSharedPointer<T>>>(lName.toLatin1().constData());
1cee0f5b Christian Ehringfeld
}

b7446f4c Christian Ehringfeld
static Entity *createObject( const QByteArray &className) {
1cee0f5b Christian Ehringfeld
Constructor constructor = constructors().value( className );
b7446f4c Christian Ehringfeld
if ( constructor == NULL ) {
1cee0f5b Christian Ehringfeld
return NULL;
b7446f4c Christian Ehringfeld
}
1cee0f5b Christian Ehringfeld
return (*constructor)();
}

b7446f4c Christian Ehringfeld
private:
typedef Entity *(*Constructor)();
1cee0f5b Christian Ehringfeld
template<typename T>
b7446f4c Christian Ehringfeld
static Entity *constructorHelper() {
1cee0f5b Christian Ehringfeld
return new T();
}

b7446f4c Christian Ehringfeld
static QHash<QByteArray, Constructor> &constructors() {
1cee0f5b Christian Ehringfeld
static QHash<QByteArray, Constructor> instance;
return instance;
}

b7446f4c Christian Ehringfeld
protected:
f4e3904b Christian Ehringfeld
EntityInstanceFactory();
};
}
#endif // ENTITYINSTANCEFACTORY_H