Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (3,18 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>
be2e9500 Christian Ehringfeld
#include "attribute.h"
f4e3904b Christian Ehringfeld
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);
23337ecc Christian Ehringfeld
static void setAttributes(Entity *&e,
47f9301a Christian Ehringfeld
const QHash<QString, QVariant> &attributes,
a205e8a9 Christian Ehringfeld
QHash<QString, QMetaProperty> metaprops);
23337ecc Christian Ehringfeld
static void 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);
c24e100e Christian Ehringfeld
static const QList<QSharedPointer<Entity>> castQVariantList(QVariant &list);
static const QSharedPointer<Entity> castQVariant(QVariant &entity);
e332a521 Christian Ehringfeld
static QStringList getRegisteredClasses();
a205e8a9 Christian Ehringfeld
template<typename T>
d933d48e Christian Ehringfeld
static Entity *createInstance() {
a205e8a9 Christian Ehringfeld
return EntityInstanceFactory::createInstance(qMetaTypeId<T>());
}
be2e9500 Christian Ehringfeld
static Entity *createInstance(Attribute *&attr);

1cee0f5b Christian Ehringfeld
//http://www.mimec.org/node/350
template<typename T>
d933d48e Christian Ehringfeld
static void registerClass() {
e2ee17bf Christian Ehringfeld
if (!EntityInstanceFactory::instance.contains(
T::staticMetaObject.className())) {
EntityInstanceFactory::instance.insert( T::staticMetaObject.className(),
&constructorHelper<T> );
QString lName = "QList<QSharedPointer<";
lName.append(T::staticMetaObject.className());
lName.append(">>");
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