Herunterladen als
root/src/entityinstancefactory.cpp @ 7b467bf5
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 | #include "entityinstancefactory.h"
|
|
#include "entity.h"
|
|||
e8d1537c | Christian Ehringfeld | #include "entityhelper.h"
|
|
f4e3904b | Christian Ehringfeld | #include <QMetaType>
|
|
058a5170 | Christian Ehringfeld | #include <QDebug>
|
|
f4e3904b | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
b9dcff08 | Christian Ehringfeld | ||
QHash<QByteArray, EntityInstanceFactory::Constructor>
|
|||
EntityInstanceFactory::instance =
|
|||
QHash<QByteArray, EntityInstanceFactory::Constructor>();
|
|||
f4e3904b | Christian Ehringfeld | EntityInstanceFactory::EntityInstanceFactory() {
|
|
}
|
|||
Entity *EntityInstanceFactory::createInstance(const char *className) {
|
|||
6d91d381 | Christian Ehringfeld | QString s = QString(className);
|
|
if (!s.contains("*")) {
|
|||
s.append("*");
|
|||
}
|
|||
1cee0f5b | Christian Ehringfeld | auto ptr = EntityInstanceFactory::createInstance(QMetaType::type(
|
|
b7446f4c | Christian Ehringfeld | s.toUtf8().constData()));
|
|
if (!ptr) {
|
|||
s.remove("*");
|
|||
1cee0f5b | Christian Ehringfeld | ptr = EntityInstanceFactory::createObject(s.toUtf8());
|
|
}
|
|||
return ptr;
|
|||
f4e3904b | Christian Ehringfeld | }
|
|
d568923d | Christian Ehringfeld | Entity *EntityInstanceFactory::createInstance(const QString &className) {
|
|
return EntityInstanceFactory::createInstance(className.toUtf8().constData());
|
|||
}
|
|||
bb5e9339 | SebastianDiel | Entity *EntityInstanceFactory::createInstance(int metaTypeId) {
|
|
35cf13b7 | Christian Ehringfeld | Entity *e = nullptr;
|
|
f5087482 | Christian Ehringfeld | if (metaTypeId != QMetaType::UnknownType) {
|
|
6d91d381 | Christian Ehringfeld | auto metaObject = QMetaType::metaObjectForType(metaTypeId);
|
|
b7446f4c | Christian Ehringfeld | if (metaObject) {
|
|
ca25da8b | Christian Ehringfeld | e = qobject_cast<Entity *>(EntityInstanceFactory::createObject(metaObject->className()));
|
|
if(!e) {
|
|||
e = qobject_cast<Entity *>(metaObject->newInstance());
|
|||
qDebug() << "Backup method for dynamic object creation was called. Maybe the class " +
|
|||
6be60ddf | Christian Ehringfeld | QString(metaObject->className()) + " isn't registered?";
|
|
ca25da8b | Christian Ehringfeld | }
|
|
033279c9 | SebastianDiel | } else {
|
|
b7446f4c | Christian Ehringfeld | void *newObj = QMetaType::create(metaTypeId);
|
|
if (newObj) {
|
|||
e = static_cast<Entity *>(newObj);
|
|||
033279c9 | SebastianDiel | }
|
|
}
|
|||
6d91d381 | Christian Ehringfeld | }
|
|
f4e3904b | Christian Ehringfeld | return e;
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | Entity *EntityInstanceFactory::createInstance(const char *className,
|
|
const QHash<QString, QVariant> &attributes) {
|
|||
7d60c5a2 | Christian Ehringfeld | Entity *e = EntityInstanceFactory::createInstance(className);
|
|
23337ecc | Christian Ehringfeld | EntityInstanceFactory::setAttributes(e, attributes);
|
|
f4e3904b | Christian Ehringfeld | return e;
|
|
}
|
|||
23337ecc | Christian Ehringfeld | void EntityInstanceFactory::setAttributes(Entity *&e,
|
|
e0e1ead8 | Christian Ehringfeld | const QHash<QString, QVariant> &attributes,
|
|
f4e3904b | Christian Ehringfeld | QHash<QString, QMetaProperty> metaprops) {
|
|
if (e) {
|
|||
auto iterator = attributes.constBegin();
|
|||
while (iterator != attributes.constEnd()) {
|
|||
if (metaprops.contains(iterator.key())) {
|
|||
QMetaProperty prop = metaprops.value(iterator.key());
|
|||
df1e56bd | Christian Ehringfeld | if (prop.isWritable()) {
|
|
if (prop.isEnumType()) {
|
|||
prop.write(e, prop.enumerator().key(iterator.value().toInt()));
|
|||
2ce163c3 | Christian Ehringfeld | } else {
|
|
prop.write(e, iterator.value());
|
|||
}
|
|||
} else {
|
|||
fc71bc10 | Christian Ehringfeld | qWarning() << prop.name() << "on Entity" << EntityHelper::getClassname(
|
|
e2ee17bf | Christian Ehringfeld | e) << "not writeable!";
|
|
f4e3904b | Christian Ehringfeld | }
|
|
}
|
|||
++iterator;
|
|||
}
|
|||
}
|
|||
}
|
|||
23337ecc | Christian Ehringfeld | void EntityInstanceFactory::setAttributes(Entity *&e,
|
|
e0e1ead8 | Christian Ehringfeld | const QHash<QString, QVariant> &attributes) {
|
|
7d60c5a2 | Christian Ehringfeld | if (!attributes.isEmpty()) {
|
|
e8d1537c | Christian Ehringfeld | auto metaprops = EntityHelper::getMetaProperties(e);
|
|
23337ecc | Christian Ehringfeld | EntityInstanceFactory::setAttributes(e, attributes, metaprops);
|
|
dfaffebe | Christian Ehringfeld | }
|
|
f4e3904b | Christian Ehringfeld | }
|
|
2d9fab10 | Christian Ehringfeld | ||
const QString EntityInstanceFactory::extractEntityType(const QString &s) {
|
|||
QString r = "";
|
|||
const QString sptr = "QSharedPointer";
|
|||
if (s.contains(sptr)) {
|
|||
97846191 | Christian Ehringfeld | const int index = s.indexOf(sptr) + sptr.count() + 1;
|
|
r = s.mid(index, s.indexOf(">", index) - index);
|
|||
706de2e8 | Christian Ehringfeld | } else {
|
|
r = s;
|
|||
2d9fab10 | Christian Ehringfeld | }
|
|
return r;
|
|||
}
|
|||
ce6994c4 | Christian Ehringfeld | ||
a1389432 | Christian Ehringfeld | Entity *EntityInstanceFactory::newSuperClassInstance(const Entity *e) {
|
|
35cf13b7 | Christian Ehringfeld | Entity *super = nullptr;
|
|
ce6994c4 | Christian Ehringfeld | if (e) {
|
|
auto metaObject = e->metaObject()->superClass();
|
|||
f5087482 | Christian Ehringfeld | if (QString(metaObject->className()) != QString("CuteEntityManager::Entity")) {
|
|
ce6994c4 | Christian Ehringfeld | super = EntityInstanceFactory::createInstance(metaObject->className());
|
|
}
|
|||
}
|
|||
return super;
|
|||
}
|
|||
59bf3900 | Christian Ehringfeld | ||
Entity *EntityInstanceFactory::createInstance(const QMetaObject *object) {
|
|||
1701766b | Christian Ehringfeld | auto o = object->newInstance();
|
|
if(o) {
|
|||
return qobject_cast<Entity *>(o);
|
|||
}
|
|||
3214951e | Christian Ehringfeld | return EntityInstanceFactory::createInstance(object->className());
|
|
59bf3900 | Christian Ehringfeld | }
|
|
d7727319 | Christian Ehringfeld | ||
ca25da8b | Christian Ehringfeld | QList<QSharedPointer<Entity>> EntityInstanceFactory::castQVariantList(
|
|
QVariant &list) {
|
|||
d7727319 | Christian Ehringfeld | return *reinterpret_cast<QList<QSharedPointer<Entity>>*>(list.data());
|
|
}
|
|||
QSharedPointer<Entity> EntityInstanceFactory::castQVariant(
|
|||
QVariant &entity) {
|
|||
5757762e | Christian Ehringfeld | return *reinterpret_cast<QSharedPointer<Entity>*>(entity.data());
|
|
d7727319 | Christian Ehringfeld | }
|
|
e332a521 | Christian Ehringfeld | ||
QStringList EntityInstanceFactory::getRegisteredClasses() {
|
|||
QStringList registered = QStringList();
|
|||
for (auto i = EntityInstanceFactory::instance.constBegin();
|
|||
i != EntityInstanceFactory::instance.constEnd(); ++i) {
|
|||
registered.append(i.key());
|
|||
}
|
|||
return registered;
|
|||
}
|
|||
be2e9500 | Christian Ehringfeld | ||
Entity *EntityInstanceFactory::createInstance(Attribute *&attr) {
|
|||
return EntityInstanceFactory::createInstance(
|
|||
EntityInstanceFactory::extractEntityType(
|
|||
attr->getMetaProperty().typeName()));
|
|||
}
|