Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (3,25 KB) Statistiken
| Zweig: | Revision:
/*
* 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/>.
*/
#ifndef ENTITYINSTANCEFACTORY_H
#define ENTITYINSTANCEFACTORY_H
#include <QString>
#include <QVariant>
#include <QHash>

namespace CuteEntityManager {
class Entity;
class EntityInstanceFactory {
public:
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,
const QHash<QString, QVariant> &attributes);
Q_DECL_EXPORT static Entity *setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes,
QHash<QString, QMetaProperty> metaprops);
Q_DECL_EXPORT static Entity *setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes);
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);

template<typename T>
Q_DECL_EXPORT static Entity *createInstance() {
return EntityInstanceFactory::createInstance(qMetaTypeId<T>());
}

//http://www.mimec.org/node/350
template<typename T>
Q_DECL_EXPORT static void registerClass() {
constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
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());
}

Q_DECL_EXPORT 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();
};
}
#endif // ENTITYINSTANCEFACTORY_H
(12-12/22)