Skip to content

Commit

Permalink
Add templated get/set and structured bindings to Associations
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Mar 24, 2023
1 parent dd43692 commit a516dcb
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
61 changes: 61 additions & 0 deletions include/podio/Association.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,67 @@ class AssociationT {
m_obj->m_to = new detail::GetDefT<ToU>(value);
}

/**
* Templated version for getting an element of the association by type. Only
* available for Associations where FromT and ToT are **not the same type**,
* and if the requested type is actually part of the Association. It is only
* possible to get the immutable types from this. Will result in a compilation
* error if any of these conditions is not met.
*
* @tparam T the desired type
* @returns T the element of the Association
* */
template <typename T, typename = std::enable_if_t<!std::is_same_v<ToT, FromT> && detail::isFromOrToT<T, FromT, ToT>>>
T get() const {
if constexpr (std::is_same_v<T, FromT>) {
return getFrom();
} else {
return getTo();
}
}

/**
* Tuple like index based access to the elements of the Association. Returns
* only immutable types of the associations. This method enables structured
* bindings for Associations.
*
* @tparam Index an index (smaller than 3) to access an element of the Association
* @returns Depending on the value of Index:
* - 0: The From element of the Association
* - 1: The To element of the Association
* - 2: The weight of the Association
*/
template <size_t Index, typename = std::enable_if_t<(Index < 3)>>
auto get() const {
if constexpr (Index == 0) {
return getFrom();
} else if constexpr (Index == 1) {
return getTo();
} else {
return getWeight();
}
}

/**
* Templated version for setting an element of the association by type. Only
* available for Associations where FromT and ToT are **not the same type**,
* and if the requested type is actually part of the Association. Will result
* in a compilation error if any of these conditions is not met.
*
* @tparam T type of value (**infered!**)
* @param value the element to set for this association.
*/
template <
typename T, bool Mut = Mutable,
typename = std::enable_if_t<Mut && !std::is_same_v<ToT, FromT> && detail::isMutableFromOrToT<T, FromT, ToT>>>
void set(T value) {
if constexpr (std::is_same_v<T, FromT>) {
setFrom(std::move(value));
} else {
setTo(std::move(value));
}
}

/// check whether the object is actually available
bool isAvailable() const {
return m_obj;
Expand Down
26 changes: 26 additions & 0 deletions include/podio/detail/AssociationFwd.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef PODIO_DETAIL_ASSOCIATIONFWD_H
#define PODIO_DETAIL_ASSOCIATIONFWD_H

#include "podio/utilities/TypeHelpers.h"

#include <algorithm>
#include <deque>
#include <string>
Expand Down Expand Up @@ -36,6 +38,20 @@ namespace detail {
template <typename T>
using GetCollT = typename GetCollType<T>::type;

/**
* Variable template to for determining whether T is either FromT or ToT.
* Mainly defined for convenience
*/
template <typename T, typename FromT, typename ToT>
static constexpr bool isFromOrToT = detail::isInTuple<T, std::tuple<FromT, ToT>>;

/**
* Variable template to for determining whether T is either FromT or ToT or
* any of their mutable versions.
*/
template <typename T, typename FromT, typename ToT>
static constexpr bool isMutableFromOrToT = detail::isInTuple<T, std::tuple<FromT, ToT, GetMutT<FromT>, GetMutT<ToT>>>;

/**
* Get the collection type name for an AssociationCollection
*
Expand Down Expand Up @@ -104,4 +120,14 @@ using AssociationMutableCollectionIterator = AssociationCollectionIteratorT<From

} // namespace podio

namespace std {
/// Specialization for enabling structure bindings for Associations
template <typename F, typename T, bool M>
struct tuple_size<podio::AssociationT<F, T, M>> : std::integral_constant<size_t, 3> {};

/// Specialization for enabling structure bindings for Associations
template <size_t Index, typename F, typename T, bool M>
struct tuple_element<Index, podio::AssociationT<F, T, M>> : tuple_element<Index, std::tuple<F, T, float>> {};
} // namespace std

#endif // PODIO_DETAIL_ASSOCIATIONFWD_H
32 changes: 32 additions & 0 deletions tests/associations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ TEST_CASE("Association basics", "[associations]") {
}
}

TEST_CASE("Associations templated accessors", "[associations]") {
ExampleHit hit;
ExampleCluster cluster;

TestMutA assoc;
assoc.set(hit);
assoc.set(cluster);
assoc.setWeight(1.0);

SECTION("Mutable Association") {
REQUIRE(hit == assoc.get<ExampleHit>());
REQUIRE(cluster == assoc.get<ExampleCluster>());

const auto [h, c, w] = assoc;
REQUIRE(h == hit);
REQUIRE(c == cluster);
REQUIRE(w == 1.0);
}

SECTION("Immutable association") {
TestA a{assoc};

REQUIRE(hit == a.get<ExampleHit>());
REQUIRE(cluster == a.get<ExampleCluster>());

const auto [h, c, w] = a;
REQUIRE(h == hit);
REQUIRE(c == cluster);
REQUIRE(w == 1.0);
}
}

TEST_CASE("AssociationCollection basics", "[associations]") {
auto coll = TestAColl();

Expand Down

0 comments on commit a516dcb

Please sign in to comment.