Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #854 MetaDataStorage class is made copyable/movable #855

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions core/indigo-core/molecule/metadata_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define __metadata_storage__

#include <cstdint>
#include <memory>
#include <vector>

#include "base_cpp/ptr_array.h"

Expand All @@ -42,9 +44,9 @@ namespace indigo
void clone(const MetaDataStorage& other);
void append(const MetaDataStorage& other);

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

virtual ~MetaDataStorage() = default;

int addMetaObject(MetaObject* pobj);

Expand All @@ -59,7 +61,7 @@ namespace indigo

void resetReactionData();

const PtrArray<MetaObject>& metaData() const
const MetadataVector& metaData() const
{
return _meta_data;
}
Expand All @@ -71,11 +73,11 @@ namespace indigo
int getMetaObjectIndex(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 @@ -649,7 +649,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 @@ -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);

Expand Down
34 changes: 27 additions & 7 deletions core/indigo-core/molecule/src/metadata_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,9 +102,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 @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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
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 @@ -388,7 +388,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 @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions core/indigo-core/tests/tests/metadata_storage.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

#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<const KETTextObject&>(ms.getMetaObject(KETTextObject::CID, 0));
ASSERT_EQ(to2._content, "{ \"data\" : \"test\" }");

const KETSimpleObject& so2 = static_cast<const KETSimpleObject&>(ms.getMetaObject(KETSimpleObject::CID, 0));
ASSERT_EQ(so2._mode, 10);

MetaDataStorage ms2(ms);

MetaDataStorage ms3(std::move(ms2));
}