Projekt

Allgemein

Profil

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

72b5abad SebastianDiel
Q_DECL_EXPORT 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