Skip to content

Commit

Permalink
Issue #854 MetaDataStorage class is made copyable/movable
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliaksander Stsepaniuk authored and Aliaksander Stsepaniuk committed Sep 23, 2022
1 parent 2cceed3 commit f67ebc5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 38 deletions.
22 changes: 12 additions & 10 deletions core/indigo-core/molecule/metadata_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifndef __metadata_storage__
#define __metadata_storage__

#include <memory>
#include <vector>

#include "base_cpp/ptr_array.h"

namespace indigo
Expand All @@ -37,11 +40,10 @@ namespace indigo
{
public:
DECL_ERROR;
void clone(const MetaDataStorage& other);

virtual ~MetaDataStorage()
{
}
using MetadataVector = std::vector<std::shared_ptr<MetaObject>>;

virtual ~MetaDataStorage() = default;

void addMetaObject(MetaObject* pobj);

Expand All @@ -55,7 +57,7 @@ namespace indigo

void resetReactionData();

const PtrArray<MetaObject>& metaData() const
const MetadataVector& metaData() const
{
return _meta_data;
}
Expand All @@ -64,11 +66,11 @@ namespace indigo
const MetaObject& getMetaObject(uint32_t meta_type, int index) const;

protected:
PtrArray<MetaObject> _meta_data; // TODO: should be replaced with list of unique_ptr
Array<int> _plus_indexes;
Array<int> _arrow_indexes;
Array<int> _simple_object_indexes;
Array<int> _text_object_indexes;
MetadataVector _meta_data;
std::vector<int> _plus_indexes;
std::vector<int> _arrow_indexes;
std::vector<int> _simple_object_indexes;
std::vector<int> _text_object_indexes;
};
}
#endif
4 changes: 2 additions & 2 deletions core/indigo-core/molecule/src/base_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ void BaseMolecule::clone(BaseMolecule& other, Array<int>* mapping, Array<int>* i

makeSubmolecule(other, *mapping, inv_mapping, skip_flags);

_meta.clone(other._meta);
_meta = other._meta;

name.copy(other.name);
}
Expand Down Expand Up @@ -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);

Expand Down
42 changes: 27 additions & 15 deletions core/indigo-core/molecule/src/metadata_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -87,9 +78,30 @@ void MetaDataStorage::resetReactionData()
{
_plus_indexes.clear();
_arrow_indexes.clear();

std::vector<int> 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;
}
}
}
16 changes: 8 additions & 8 deletions core/indigo-core/molecule/src/molecule_json_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,14 +1059,14 @@ void MoleculeJsonSaver::saveMetaData(rapidjson::Writer<rapidjson::StringBuffer>&
}
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");
Expand All @@ -1081,7 +1081,7 @@ void MoleculeJsonSaver::saveMetaData(rapidjson::Writer<rapidjson::StringBuffer>&
writer.Key("pos");
writer.StartArray();

auto& coords = simple_obj->_coordinates;
auto& coords = simple_obj._coordinates;

// point1
writer.StartObject();
Expand Down Expand Up @@ -1112,22 +1112,22 @@ void MoleculeJsonSaver::saveMetaData(rapidjson::Writer<rapidjson::StringBuffer>&
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
Expand Down
2 changes: 1 addition & 1 deletion core/indigo-core/reaction/src/base_reaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void BaseReaction::clone(BaseReaction& other, Array<int>* mol_mapping, ObjArray<
}

name.copy(other.name);
_meta.clone(other._meta);
_meta = other._meta;
}

void BaseReaction::_clone(BaseReaction& other, int index, int i, ObjArray<Array<int>>* mol_mappings)
Expand Down
2 changes: 1 addition & 1 deletion core/indigo-core/reaction/src/reaction_json_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion core/indigo-core/reaction/src/reaction_json_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringBuffer> writer(s);
Expand Down

0 comments on commit f67ebc5

Please sign in to comment.