Skip to content

Commit

Permalink
fix setting link with interface without specific link direction (#736)
Browse files Browse the repository at this point in the history
* fix setting link with interface without specific link direction

* use always_false template to avoid compilation error in
`else static_assert(false)`
  • Loading branch information
m-fila authored Feb 17, 2025
1 parent cc36b39 commit 21d3e4b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/podio/detail/Link.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "podio/detail/LinkObj.h"
#include "podio/utilities/MaybeSharedPtr.h"
#include "podio/utilities/TypeHelpers.h"
#include <type_traits>

#ifdef PODIO_JSON_OUTPUT
#include "nlohmann/json.hpp"
Expand Down Expand Up @@ -262,8 +263,16 @@ class LinkT {
void set(T value) {
if constexpr (std::is_same_v<T, FromT>) {
setFrom(std::move(value));
} else {
} else if constexpr (std::is_same_v<T, ToT>) {
setTo(std::move(value));
} else if constexpr (detail::isInterfaceInitializableFrom<FromT, T> &&
!detail::isInterfaceInitializableFrom<ToT, T>) {
setFrom(std::move(value));
} else if constexpr (detail::isInterfaceInitializableFrom<ToT, T> &&
!detail::isInterfaceInitializableFrom<FromT, T>) {
setTo(std::move(value));
} else {
static_assert(detail::always_false<T>, "Argument type is ambiguous, can't determine link direction");
}
}

Expand Down
5 changes: 5 additions & 0 deletions include/podio/utilities/TypeHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ namespace det {

namespace detail {

// A helper variable template that is always false, used for static_asserts
// and other compile-time checks that should always fail.
template <typename T>
inline constexpr bool always_false = false;

/// Helper struct to determine whether a given type T is in a tuple of types
/// that act as a type list in this case
template <typename T, typename>
Expand Down
15 changes: 15 additions & 0 deletions tests/unittests/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,21 @@ TEST_CASE("Links with interfaces", "[links][interface-types]") {
link.set(hit);
REQUIRE(link.get<TypeWithEnergy>() == hit);
REQUIRE(link.get<ExampleCluster>() == cluster);

// Check determining `set` direction for a link going in a reverse direction
using ReverseInterfaceLinkCollection =
podio::LinkCollection<TestInterfaceLinkCollection::to_type, TestInterfaceLinkCollection::from_type>;
auto rColl = ReverseInterfaceLinkCollection{};
auto rLink = rColl.create();

rLink.set(cluster);
rLink.set(iface);
REQUIRE(rLink.get<TypeWithEnergy>() == iface);
REQUIRE(rLink.get<ExampleCluster>() == cluster);

rLink.set(hit);
REQUIRE(rLink.get<TypeWithEnergy>() == hit);
REQUIRE(rLink.get<ExampleCluster>() == cluster);
}

#ifdef PODIO_JSON_OUTPUT
Expand Down

0 comments on commit 21d3e4b

Please sign in to comment.