From f67ebc51b43d093a151620e03ea1e6123e17e825 Mon Sep 17 00:00:00 2001 From: Aliaksander Stsepaniuk Date: Fri, 23 Sep 2022 22:09:37 +0300 Subject: [PATCH] Issue #854 MetaDataStorage class is made copyable/movable --- core/indigo-core/molecule/metadata_storage.h | 22 +++++----- .../molecule/src/base_molecule.cpp | 4 +- .../molecule/src/metadata_storage.cpp | 42 ++++++++++++------- .../molecule/src/molecule_json_saver.cpp | 16 +++---- .../reaction/src/base_reaction.cpp | 2 +- .../reaction/src/reaction_json_loader.cpp | 2 +- .../reaction/src/reaction_json_saver.cpp | 2 +- 7 files changed, 52 insertions(+), 38 deletions(-) diff --git a/core/indigo-core/molecule/metadata_storage.h b/core/indigo-core/molecule/metadata_storage.h index a4efef968c..360a019985 100644 --- a/core/indigo-core/molecule/metadata_storage.h +++ b/core/indigo-core/molecule/metadata_storage.h @@ -18,6 +18,9 @@ #ifndef __metadata_storage__ #define __metadata_storage__ +#include +#include + #include "base_cpp/ptr_array.h" namespace indigo @@ -37,11 +40,10 @@ namespace indigo { public: DECL_ERROR; - void clone(const MetaDataStorage& other); - virtual ~MetaDataStorage() - { - } + using MetadataVector = std::vector>; + + virtual ~MetaDataStorage() = default; void addMetaObject(MetaObject* pobj); @@ -55,7 +57,7 @@ namespace indigo void resetReactionData(); - const PtrArray& metaData() const + const MetadataVector& metaData() const { return _meta_data; } @@ -64,11 +66,11 @@ namespace indigo const MetaObject& getMetaObject(uint32_t meta_type, int index) const; protected: - PtrArray _meta_data; // TODO: should be replaced with list of unique_ptr - Array _plus_indexes; - Array _arrow_indexes; - Array _simple_object_indexes; - Array _text_object_indexes; + MetadataVector _meta_data; + std::vector _plus_indexes; + std::vector _arrow_indexes; + std::vector _simple_object_indexes; + std::vector _text_object_indexes; }; } #endif \ No newline at end of file diff --git a/core/indigo-core/molecule/src/base_molecule.cpp b/core/indigo-core/molecule/src/base_molecule.cpp index 705976f0ac..81d618e6a9 100644 --- a/core/indigo-core/molecule/src/base_molecule.cpp +++ b/core/indigo-core/molecule/src/base_molecule.cpp @@ -636,7 +636,7 @@ void BaseMolecule::clone(BaseMolecule& other, Array* mapping, Array* i makeSubmolecule(other, *mapping, inv_mapping, skip_flags); - _meta.clone(other._meta); + _meta = other._meta; name.copy(other.name); } @@ -667,7 +667,7 @@ void BaseMolecule::clone_KeepIndices(BaseMolecule& other, int skip_flags) _cloneGraph_KeepIndices(other); - _meta.clone(other._meta); + _meta = other._meta; _mergeWithSubmolecule_Sub(other, vertices, 0, mapping, edge_mapping, skip_flags); diff --git a/core/indigo-core/molecule/src/metadata_storage.cpp b/core/indigo-core/molecule/src/metadata_storage.cpp index 9f7d430935..1110ca3c75 100644 --- a/core/indigo-core/molecule/src/metadata_storage.cpp +++ b/core/indigo-core/molecule/src/metadata_storage.cpp @@ -9,36 +9,27 @@ IMPL_ERROR(MetaDataStorage, "metadata storage"); void MetaDataStorage::addMetaObject(MetaObject* pobj) { int index = _meta_data.size(); - _meta_data.expand(index + 1); - _meta_data.set(index, pobj); + _meta_data.emplace_back(pobj); switch (pobj->_class_id) { case KETTextObject::CID: - _text_object_indexes.push() = index; + _text_object_indexes.push_back(index); break; case KETSimpleObject::CID: - _simple_object_indexes.push() = index; + _simple_object_indexes.push_back(index); break; case KETReactionPlus::CID: - _plus_indexes.push() = index; + _plus_indexes.push_back(index); break; case KETReactionArrow::CID: - _arrow_indexes.push() = index; + _arrow_indexes.push_back(index); break; default: break; } } -void MetaDataStorage::clone(const MetaDataStorage& other) -{ - resetMetaData(); - const auto& meta = other.metaData(); - for (int i = 0; i < meta.size(); i++) - addMetaObject(meta[i]->clone()); -} - const MetaObject& MetaDataStorage::getMetaObject(uint32_t meta_type, int index) const { switch (meta_type) @@ -87,9 +78,30 @@ void MetaDataStorage::resetReactionData() { _plus_indexes.clear(); _arrow_indexes.clear(); + + std::vector indexes_to_remove; for (int i = _meta_data.size() - 1; i >= 0; i--) { if (_meta_data[i]->_class_id == KETReactionArrow::CID || _meta_data[i]->_class_id == KETReactionPlus::CID) - _meta_data.remove(i); + { + indexes_to_remove.push_back(i); + } + } + + for (int i : indexes_to_remove) + { + _meta_data.erase(_meta_data.begin() + i); + + for (int& toi : _text_object_indexes) + { + if (toi > i) + --toi; + } + + for (int& soi : _simple_object_indexes) + { + if (soi > i) + --soi; + } } } diff --git a/core/indigo-core/molecule/src/molecule_json_saver.cpp b/core/indigo-core/molecule/src/molecule_json_saver.cpp index 81790b0a70..7a3c68e439 100644 --- a/core/indigo-core/molecule/src/molecule_json_saver.cpp +++ b/core/indigo-core/molecule/src/molecule_json_saver.cpp @@ -1059,14 +1059,14 @@ void MoleculeJsonSaver::saveMetaData(rapidjson::Writer& } break; case KETSimpleObject::CID: { - auto simple_obj = (KETSimpleObject*)pobj; + KETSimpleObject& simple_obj = (KETSimpleObject&)(*pobj); writer.StartObject(); writer.Key("type"); writer.String("simpleObject"); writer.Key("data"); writer.StartObject(); writer.Key("mode"); - switch (simple_obj->_mode) + switch (simple_obj._mode) { case KETSimpleObject::EKETEllipse: writer.String("ellipse"); @@ -1081,7 +1081,7 @@ void MoleculeJsonSaver::saveMetaData(rapidjson::Writer& writer.Key("pos"); writer.StartArray(); - auto& coords = simple_obj->_coordinates; + auto& coords = simple_obj._coordinates; // point1 writer.StartObject(); @@ -1112,22 +1112,22 @@ void MoleculeJsonSaver::saveMetaData(rapidjson::Writer& break; } case KETTextObject::CID: { - auto simple_obj = (KETTextObject*)pobj; + KETTextObject& simple_obj = (KETTextObject&)(*pobj); writer.StartObject(); writer.Key("type"); writer.String("text"); writer.Key("data"); writer.StartObject(); writer.Key("content"); - writer.String(simple_obj->_content.c_str()); + writer.String(simple_obj._content.c_str()); writer.Key("position"); writer.StartObject(); writer.Key("x"); - writer.Double(simple_obj->_pos.x); + writer.Double(simple_obj._pos.x); writer.Key("y"); - writer.Double(simple_obj->_pos.y); + writer.Double(simple_obj._pos.y); writer.Key("z"); - writer.Double(simple_obj->_pos.z); + writer.Double(simple_obj._pos.z); writer.EndObject(); // end position writer.EndObject(); // end data writer.EndObject(); // end node diff --git a/core/indigo-core/reaction/src/base_reaction.cpp b/core/indigo-core/reaction/src/base_reaction.cpp index daace30bd3..9d800ed310 100644 --- a/core/indigo-core/reaction/src/base_reaction.cpp +++ b/core/indigo-core/reaction/src/base_reaction.cpp @@ -372,7 +372,7 @@ void BaseReaction::clone(BaseReaction& other, Array* mol_mapping, ObjArray< } name.copy(other.name); - _meta.clone(other._meta); + _meta = other._meta; } void BaseReaction::_clone(BaseReaction& other, int index, int i, ObjArray>* mol_mappings) diff --git a/core/indigo-core/reaction/src/reaction_json_loader.cpp b/core/indigo-core/reaction/src/reaction_json_loader.cpp index 830f78a772..7930f4be9e 100644 --- a/core/indigo-core/reaction/src/reaction_json_loader.cpp +++ b/core/indigo-core/reaction/src/reaction_json_loader.cpp @@ -68,7 +68,7 @@ void ReactionJsonLoader::loadReaction(BaseReaction& rxn) else throw Error("unknown reaction type: %s", typeid(rxn).name()); - rxn.meta().clone(_pmol->meta()); + rxn.meta() = std::move(_pmol->meta()); _pmol->meta().resetMetaData(); int arrow_count = rxn.meta().getMetaCount(KETReactionArrow::CID); diff --git a/core/indigo-core/reaction/src/reaction_json_saver.cpp b/core/indigo-core/reaction/src/reaction_json_saver.cpp index d51dd79ee2..759c94a659 100644 --- a/core/indigo-core/reaction/src/reaction_json_saver.cpp +++ b/core/indigo-core/reaction/src/reaction_json_saver.cpp @@ -66,7 +66,7 @@ void ReactionJsonSaver::saveReactionWithMetaData(BaseReaction& rxn, BaseMolecule for (int i = rxn.begin(); i != rxn.end(); i = rxn.next(i)) merged.mergeWithMolecule(rxn.getBaseMolecule(i), 0, 0); - merged.meta().clone(rxn.meta()); + merged.meta() = rxn.meta(); StringBuffer s; Writer writer(s);