diff --git a/core/indigo-core/molecule/metadata_storage.h b/core/indigo-core/molecule/metadata_storage.h index 4046d42aba..16a75aeca1 100644 --- a/core/indigo-core/molecule/metadata_storage.h +++ b/core/indigo-core/molecule/metadata_storage.h @@ -19,6 +19,8 @@ #define __metadata_storage__ #include +#include +#include #include "base_cpp/ptr_array.h" @@ -42,9 +44,9 @@ namespace indigo void clone(const MetaDataStorage& other); void append(const MetaDataStorage& other); - virtual ~MetaDataStorage() - { - } + using MetadataVector = std::vector>; + + virtual ~MetaDataStorage() = default; int addMetaObject(MetaObject* pobj); @@ -59,7 +61,7 @@ namespace indigo void resetReactionData(); - const PtrArray& metaData() const + const MetadataVector& metaData() const { return _meta_data; } @@ -71,11 +73,11 @@ namespace indigo int getMetaObjectIndex(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 a50e2d6ed6..4b381ade4c 100644 --- a/core/indigo-core/molecule/src/base_molecule.cpp +++ b/core/indigo-core/molecule/src/base_molecule.cpp @@ -649,7 +649,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); } @@ -680,7 +680,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 0ffb799281..d4b453329b 100644 --- a/core/indigo-core/molecule/src/metadata_storage.cpp +++ b/core/indigo-core/molecule/src/metadata_storage.cpp @@ -9,22 +9,21 @@ IMPL_ERROR(MetaDataStorage, "metadata storage"); int 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; @@ -103,9 +102,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 e14efd63a3..8d54919cad 100644 --- a/core/indigo-core/molecule/src/molecule_json_saver.cpp +++ b/core/indigo-core/molecule/src/molecule_json_saver.cpp @@ -1233,14 +1233,14 @@ void MoleculeJsonSaver::saveMetaData(JsonWriter& writer, MetaDataStorage& meta) } 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"); @@ -1255,7 +1255,7 @@ void MoleculeJsonSaver::saveMetaData(JsonWriter& writer, MetaDataStorage& meta) writer.Key("pos"); writer.StartArray(); - auto& coords = simple_obj->_coordinates; + auto& coords = simple_obj._coordinates; // point1 writer.StartObject(); @@ -1286,22 +1286,22 @@ void MoleculeJsonSaver::saveMetaData(JsonWriter& writer, MetaDataStorage& meta) 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 aaa1b6b740..163e77b48f 100644 --- a/core/indigo-core/reaction/src/base_reaction.cpp +++ b/core/indigo-core/reaction/src/base_reaction.cpp @@ -388,7 +388,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 a8c479ea46..47dc916305 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 406c84ddc3..e1ba041b8e 100644 --- a/core/indigo-core/reaction/src/reaction_json_saver.cpp +++ b/core/indigo-core/reaction/src/reaction_json_saver.cpp @@ -81,7 +81,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; JsonWriter writer(pretty_json); writer.Reset(s); diff --git a/core/indigo-core/tests/tests/metadata_storage.cpp b/core/indigo-core/tests/tests/metadata_storage.cpp new file mode 100644 index 0000000000..4bdeeae14a --- /dev/null +++ b/core/indigo-core/tests/tests/metadata_storage.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** + * Copyright (C) from 2009 to Present EPAM Systems. + * + * This file is part of Indigo toolkit. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ***************************************************************************/ + +#include + +#include "common.h" +#include "molecule/ket_commons.h" +#include "molecule/metadata_storage.h" + +using namespace std; +using namespace indigo; + +class IndigoCoreMetadataStorageTest : public IndigoCoreTest +{ +}; + +TEST_F(IndigoCoreMetadataStorageTest, reset_reaction_data) +{ + MetaDataStorage ms; + + KETTextObject* to = new KETTextObject({}, "{ \"data\" : \"test\" }"); + KETSimpleObject* so = new KETSimpleObject(10, std::make_pair(Vec2f{}, Vec2f{})); + KETReactionArrow* ra = new KETReactionArrow(15, {}, {}); + KETReactionPlus* rp = new KETReactionPlus({20, 25}); + + ms.addMetaObject(ra); + ms.addMetaObject(rp); + ms.addMetaObject(to); + ms.addMetaObject(so); + + ms.resetReactionData(); + + ASSERT_EQ(ms.metaData().size(), 2); + + const KETTextObject& to2 = static_cast(ms.getMetaObject(KETTextObject::CID, 0)); + ASSERT_EQ(to2._content, "{ \"data\" : \"test\" }"); + + const KETSimpleObject& so2 = static_cast(ms.getMetaObject(KETSimpleObject::CID, 0)); + ASSERT_EQ(so2._mode, 10); + + MetaDataStorage ms2(ms); + + MetaDataStorage ms3(std::move(ms2)); +}