Skip to content

Commit

Permalink
Change from emptyRelations to cloneRelations
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Jun 3, 2024
1 parent 8203580 commit 07021a4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
3 changes: 2 additions & 1 deletion python/templates/macros/declarations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
{{ full_type }}& operator=({{ full_type }} other);

/// create a mutable deep-copy of the object with identical relations
Mutable{{ type }} clone(bool emptyRelations=false) const;
/// if cloneRelations=false, the relations are not cloned and will be empty
Mutable{{ type }} clone(bool cloneRelations=true) const;

/// destructor
~{{ full_type }}() = default;
Expand Down
6 changes: 3 additions & 3 deletions python/templates/macros/implementations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
return *this;
}

Mutable{{ type }} {{ full_type }}::clone(bool emptyRelations) const {
Mutable{{ type }} {{ full_type }}::clone(bool cloneRelations) const {
{% if prefix %}
if (emptyRelations) {
if (!cloneRelations) {
auto tmp = new {{ type }}Obj(podio::ObjectID{}, m_obj->data);
{% for relation in multi_relations %}
tmp->m_{{ relation.name }} = new std::vector<{{ relation.full_type }}>();
Expand All @@ -35,7 +35,7 @@ Mutable{{ type }} {{ full_type }}::clone(bool emptyRelations) const {
{% for relation in multi_relations %}
tmp->m_{{ relation.name }} = new std::vector<{{ relation.full_type }}>();
{% endfor %}
if (!emptyRelations) {
if (cloneRelations) {
{% for relation in multi_relations %}
// If the current object has been read from a file, then the object may only have a slice of the relation vector
// so this slice has to be copied in case we want to modify it
Expand Down
14 changes: 5 additions & 9 deletions tests/unittests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1386,15 +1386,15 @@ TEST_CASE("Relations after cloning with SIO", "[relations][basics]") {

#endif

void testCloneEmptyRelations() {
TEST_CASE("Clone empty relations", "[relations][basics]") {
auto coll = ExampleClusterCollection();
coll.create();
coll.create();
coll[0].addHits(ExampleHit());
coll[0].addHits(ExampleHit());
auto newColl = ExampleClusterCollection();
newColl.push_back(coll.at(0).clone(true));
newColl.push_back(coll.at(1).clone(true));
newColl.push_back(coll.at(0).clone(false));
newColl.push_back(coll.at(1).clone(false));
std::cout << newColl[0].Hits().size() << '\n';
std::cout << newColl[1].Hits().size() << '\n';
REQUIRE(newColl[0].Hits().empty());
Expand All @@ -1406,16 +1406,12 @@ void testCloneEmptyRelations() {

auto immCluster = ExampleCluster(coll.at(0));
auto immCluster2 = ExampleCluster(coll.at(1));
auto clonedImmCluster = immCluster.clone(true);
auto clonedImmCluster2 = immCluster2.clone(true);
auto clonedImmCluster = immCluster.clone(false);
auto clonedImmCluster2 = immCluster2.clone(false);
REQUIRE(clonedImmCluster.Hits().empty());
REQUIRE(clonedImmCluster2.Hits().empty());
clonedImmCluster.addHits(ExampleHit());
REQUIRE(clonedImmCluster.Hits().size() == 1);
clonedImmCluster.addHits(ExampleHit());
REQUIRE(clonedImmCluster.Hits().size() == 2);
}

TEST_CASE("Clone empty relations", "[relations][basics]") {
testCloneEmptyRelations();
}

0 comments on commit 07021a4

Please sign in to comment.