From 89a2323944e2a50ff33fc8267ec00e539ba72507 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 24 Oct 2017 19:50:16 +0300 Subject: [PATCH] Fix problem with updating objects inside vcontainer. --HG-- branch : feature --- src/libs/vpatterndb/vcontainer.cpp | 37 --------------- src/libs/vpatterndb/vcontainer.h | 73 +++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 43 deletions(-) diff --git a/src/libs/vpatterndb/vcontainer.cpp b/src/libs/vpatterndb/vcontainer.cpp index 5457a88d0..10caece95 100644 --- a/src/libs/vpatterndb/vcontainer.cpp +++ b/src/libs/vpatterndb/vcontainer.cpp @@ -249,22 +249,6 @@ void VContainer::UpdateId(quint32 newId) } } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief UpdateObject update object in container - * @param id id of existing object - * @param point object - */ -template -void VContainer::UpdateObject(const quint32 &id, val point) -{ - Q_ASSERT_X(id != NULL_ID, Q_FUNC_INFO, "id == 0"); //-V654 //-V712 - SCASSERT(point.isNull() == false) - point->setId(id); - d->gObjects.insert(id, point); - UpdateId(id); -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief Clear clear data in container. Id will be 0. @@ -497,27 +481,6 @@ quint32 VContainer::AddObject(QHash &obj, val value) return id; } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief UpdateGObject update GObject by id - * @param id id of existing GObject - * @param obj object - */ -void VContainer::UpdateGObject(quint32 id, VGObject* obj) -{ - SCASSERT(obj != nullptr) - QSharedPointer pointer(obj); - UpdateGObject(id, pointer); -} - -//--------------------------------------------------------------------------------------------------------------------- -void VContainer::UpdateGObject(quint32 id, const QSharedPointer &obj) -{ - SCASSERT(not obj.isNull()) - UpdateObject(id, obj); - uniqueNames.insert(obj->name()); -} - //--------------------------------------------------------------------------------------------------------------------- void VContainer::UpdatePiece(quint32 id, const VPiece &detail) { diff --git a/src/libs/vpatterndb/vcontainer.h b/src/libs/vpatterndb/vcontainer.h index 0d6850d02..2837bbddd 100644 --- a/src/libs/vpatterndb/vcontainer.h +++ b/src/libs/vpatterndb/vcontainer.h @@ -160,8 +160,10 @@ public: void RemoveVariable(const QString& name); void RemovePiece(quint32 id); - void UpdateGObject(quint32 id, VGObject* obj); - void UpdateGObject(quint32 id, const QSharedPointer &obj); + template + void UpdateGObject(quint32 id, T* obj); + template + void UpdateGObject(quint32 id, const QSharedPointer &obj); void UpdatePiece(quint32 id, const VPiece &detail); void UpdatePiecePath(quint32 id, const VPiecePath &path); @@ -221,8 +223,8 @@ private: // cppcheck-suppress functionStatic const val GetObject(const QHash &obj, key id) const; - template - void UpdateObject(const quint32 &id, val point); + template + void UpdateObject(const quint32 &id, const QSharedPointer &point); template static quint32 AddObject(QHash &obj, val value); @@ -312,14 +314,23 @@ void VContainer::AddVariable(const QString& name, const QSharedPointer &var) { if (d->variables.value(name)->GetType() == var->GetType()) { - d->variables[name].clear(); + QSharedPointer v = qSharedPointerDynamicCast(d->variables.value(name)); + if (v.isNull()) + { + throw VExceptionBadId(tr("Can't cast object."), name); + } + *v = *var; } else { throw VExceptionBadId(tr("Can't find object. Type mismatch."), name); } } - d->variables.insert(name, var); + else + { + d->variables.insert(name, var); + } + uniqueNames.insert(name); } @@ -329,4 +340,54 @@ uint VContainer::qHash( const QSharedPointer &p ) { return qHash( p.data() ); } + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateGObject update GObject by id + * @param id id of existing GObject + * @param obj object + */ +template +void VContainer::UpdateGObject(quint32 id, T* obj) +{ + SCASSERT(obj != nullptr) + UpdateGObject(id, QSharedPointer(obj)); +} + +//--------------------------------------------------------------------------------------------------------------------- +template +void VContainer::UpdateGObject(quint32 id, const QSharedPointer &obj) +{ + SCASSERT(not obj.isNull()) + UpdateObject(id, obj); + uniqueNames.insert(obj->name()); +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateObject update object in container + * @param id id of existing object + * @param point object + */ +template +void VContainer::UpdateObject(const quint32 &id, const QSharedPointer &point) +{ + Q_ASSERT_X(id != NULL_ID, Q_FUNC_INFO, "id == 0"); //-V654 //-V712 + SCASSERT(point.isNull() == false) + point->setId(id); + if (d->gObjects.contains(id)) + { + QSharedPointer obj = qSharedPointerDynamicCast(d->gObjects.value(id)); + if (obj.isNull()) + { + throw VExceptionBadId(tr("Can't cast object"), id); + } + *obj = *point; + } + else + { + d->gObjects.insert(id, point); + } + UpdateId(id); +} #endif // VCONTAINER_H