Skip to content

Commit

Permalink
Fix the equal comparison for relations of cloned objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed May 27, 2024
1 parent e28adf7 commit d421d1d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
33 changes: 16 additions & 17 deletions include/podio/utilities/MaybeSharedPtr.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef PODIO_UTILITIES_MAYBESHAREDPTR_H
#define PODIO_UTILITIES_MAYBESHAREDPTR_H

#include "podio/ObjectID.h"

#include <atomic>

namespace podio::utils {
Expand Down Expand Up @@ -146,24 +148,21 @@ void swap(MaybeSharedPtr<T>& a, MaybeSharedPtr<T>& b) {
swap(a.m_ctrlBlock, b.m_ctrlBlock);
}

// helper macro for avoiding a bit of typing/repetition
#define DEFINE_COMPARISON_OPERATOR(op) \
template <typename U> \
bool operator op(const MaybeSharedPtr<U>& lhs, const MaybeSharedPtr<U>& rhs) { \
return lhs.m_ptr op rhs.m_ptr; \
} \
template <typename U> \
bool operator op(const MaybeSharedPtr<U>& lhs, const U* rhs) { \
return lhs.m_ptr op rhs; \
} \
template <typename U> \
bool operator op(const U* lhs, const MaybeSharedPtr<U>& rhs) { \
return lhs op rhs.m_ptr; \
}
template <typename U>
bool operator==(const MaybeSharedPtr<U>& lhs, const MaybeSharedPtr<U>& rhs) {
return lhs.m_ptr == rhs.m_ptr ||
// When the pointers are different:
// If there are any nullptrs the comparison is false automatically
// If one of them is untracked (either lhs is or if not then they will have different ID)
// the comparison is also false
// If none of the above, they are tracked objects and they have to have the same id for the comparison to be true
(lhs.m_ptr && rhs.m_ptr && lhs.m_ptr->id.index != podio::ObjectID::untracked && lhs.m_ptr->id == rhs.m_ptr->id);
}
template <typename U>
bool operator<(const MaybeSharedPtr<U>& lhs, const MaybeSharedPtr<U>& rhs) {
return lhs.m_ptr < rhs.m_ptr;
}

DEFINE_COMPARISON_OPERATOR(==)
DEFINE_COMPARISON_OPERATOR(!=)
DEFINE_COMPARISON_OPERATOR(<)
#undef DEFINE_COMPARISON_OPERATOR

} // namespace podio::utils
Expand Down
37 changes: 37 additions & 0 deletions tests/unittests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "datamodel/ExampleForCyclicDependency1Collection.h"
#include "datamodel/ExampleForCyclicDependency2Collection.h"
#include "datamodel/ExampleHitCollection.h"
#include "datamodel/ExampleMCCollection.h"
#include "datamodel/ExampleWithArray.h"
#include "datamodel/ExampleWithArrayComponent.h"
#include "datamodel/ExampleWithComponent.h"
Expand Down Expand Up @@ -469,8 +470,15 @@ TEST_CASE("Equality", "[basics]") {
auto clu2 = ExampleCluster::makeEmpty();
// Empty handles always compare equal
REQUIRE(clu == clu2);
REQUIRE(clu2 == clu);
// They never compare equal to a non-empty handle
REQUIRE(clu != cluster);
REQUIRE(cluster != clu);

auto coll = ExampleClusterCollection();
coll.create();
REQUIRE(coll[0] != clu);
REQUIRE(clu != coll[0]);
}

TEST_CASE("UserInitialization", "[basics][code-gen]") {
Expand Down Expand Up @@ -1385,3 +1393,32 @@ TEST_CASE("Relations after cloning with SIO", "[relations][basics]") {
}

#endif

void runRelationAfterCloneEqualCheck() {
auto newColl = ExampleMCCollection();
auto coll = ExampleMCCollection();
coll.create();
coll.create();
coll[0].addparents(coll[0]);
coll[0].adddaughters(coll[1]);
coll[0].adddaughters(coll[0]);
coll[1].addparents(coll[0]);
newColl.push_back(coll.at(0).clone());
newColl.push_back(coll.at(1).clone());

REQUIRE(newColl[0].parents()[0] == newColl[0]);
REQUIRE(newColl[0].daughters()[0] == newColl[1]);
REQUIRE(newColl[0].daughters()[1] == newColl[0]);
REQUIRE(newColl[1].parents()[0] == newColl[0]);

REQUIRE(newColl[0].parents()[0] != newColl[1]);
REQUIRE(newColl[0].daughters()[0] != newColl[0]);
REQUIRE(newColl[0].daughters()[1] != newColl[1]);
REQUIRE(newColl[1].parents()[0] != newColl[1]);


}

TEST_CASE("Relations after cloning with equal check", "[relations][basics]") {
runRelationAfterCloneEqualCheck();
}

0 comments on commit d421d1d

Please sign in to comment.