Herunterladen als
root/src/entityinstancefactory.cpp @ 59bf3900
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"
|
|||
#include <QMetaType>
|
|||
using namespace CuteEntityManager;
|
|||
EntityInstanceFactory::EntityInstanceFactory() {
|
|||
}
|
|||
Entity *EntityInstanceFactory::createInstance(const char *className) {
|
|||
6d91d381 | Christian Ehringfeld | QString s = QString(className);
|
|
if (!s.contains("*")) {
|
|||
s.append("*");
|
|||
}
|
|||
59bf3900 | Christian Ehringfeld | return EntityInstanceFactory::createInstance(QMetaType::type(
|
|
s.toUtf8().constData()));
|
|||
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) {
|
|
f4e3904b | Christian Ehringfeld | Entity *e = 0;
|
|
f5087482 | Christian Ehringfeld | if (metaTypeId != QMetaType::UnknownType) {
|
|
6d91d381 | Christian Ehringfeld | auto metaObject = QMetaType::metaObjectForType(metaTypeId);
|
|
e = static_cast<Entity *>(metaObject->newInstance());
|
|||
if (!e) {
|
|||
f5087482 | Christian Ehringfeld | qDebug() << "Entity instance could not created!";
|
|
6d91d381 | Christian Ehringfeld | throw - 2; //testing
|
|
f5087482 | Christian Ehringfeld | }
|
|
6d91d381 | Christian Ehringfeld | } else {
|
|
qDebug() << metaTypeId << "EntityClass NOT registered! Please register it!";
|
|||
throw - 1; //testing
|
|||
}
|
|||
f4e3904b | Christian Ehringfeld | return e;
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | Entity *EntityInstanceFactory::createInstance(const char *className,
|
|
const QHash<QString, QVariant> &attributes) {
|
|||
f4e3904b | Christian Ehringfeld | Entity *e = EntityInstanceFactory::createInstance(className);
|
|
6d91d381 | Christian Ehringfeld | e = EntityInstanceFactory::setAttributes(e, attributes);
|
|
f4e3904b | Christian Ehringfeld | return e;
|
|
}
|
|||
6d91d381 | Christian Ehringfeld | Entity *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());
|
|||
6d91d381 | Christian Ehringfeld | if (!(prop.isWritable() && prop.write(e, iterator.value()))) {
|
|
f4e3904b | Christian Ehringfeld | qDebug() << prop.name() << "on Entity" << e->getClassname() << "not writeable!";
|
|
}
|
|||
}
|
|||
++iterator;
|
|||
}
|
|||
}
|
|||
return e;
|
|||
}
|
|||
6d91d381 | Christian Ehringfeld | Entity *EntityInstanceFactory::setAttributes(Entity *&e,
|
|
e0e1ead8 | Christian Ehringfeld | const QHash<QString, QVariant> &attributes) {
|
|
f4e3904b | Christian Ehringfeld | auto metaprops = e->getMetaProperties();
|
|
return EntityInstanceFactory::setAttributes(e, attributes, metaprops);
|
|||
}
|
|||
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) {
|
|
ce6994c4 | Christian Ehringfeld | Entity *super = 0;
|
|
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) {
|
|||
return qobject_cast<Entity *>(object->newInstance());
|
|||
}
|