Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (3,01 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:
d933d48e Christian Ehringfeld
static Entity *createInstance(const char *className);
static Entity *createInstance(const QString &className);
static Entity *createInstance(int metaTypeId);
static Entity *createInstance(const char *className,
e0e1ead8 Christian Ehringfeld
const QHash<QString, QVariant> &attributes);
d933d48e Christian Ehringfeld
static Entity *setAttributes(Entity *&e,
47f9301a Christian Ehringfeld
const QHash<QString, QVariant> &attributes,
a205e8a9 Christian Ehringfeld
QHash<QString, QMetaProperty> metaprops);
d933d48e Christian Ehringfeld
static Entity *setAttributes(Entity *&e,
47f9301a Christian Ehringfeld
const QHash<QString, QVariant> &attributes);
d933d48e Christian Ehringfeld
static const QString extractEntityType(const QString &s);
static Entity *newSuperClassInstance(const Entity *e);
static Entity *createInstance(const QMetaObject *object);
static QList<QSharedPointer<Entity>> castQVariantList(QVariant &list);
static QSharedPointer<Entity> castQVariant(QVariant &entity);
a205e8a9 Christian Ehringfeld
template<typename T>
d933d48e Christian Ehringfeld
static Entity *createInstance() {
a205e8a9 Christian Ehringfeld
return EntityInstanceFactory::createInstance(qMetaTypeId<T>());
}
1cee0f5b Christian Ehringfeld
//http://www.mimec.org/node/350
template<typename T>
d933d48e Christian Ehringfeld
static void registerClass() {
b9dcff08 Christian Ehringfeld
EntityInstanceFactory::instance.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
}

d933d48e Christian Ehringfeld
static Entity *createObject( const QByteArray &className) {
b9dcff08 Christian Ehringfeld
Constructor constructor = EntityInstanceFactory::instance.value( className );
if ( constructor == nullptr ) {
return nullptr;
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();
}
b9dcff08 Christian Ehringfeld
static QHash<QByteArray, Constructor> instance;
1cee0f5b Christian Ehringfeld
b7446f4c Christian Ehringfeld
protected:
f4e3904b Christian Ehringfeld
EntityInstanceFactory();
};
}
#endif // ENTITYINSTANCEFACTORY_H