Skip to content

Commit

Permalink
Move template helpers to class and introduce a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Jul 22, 2024
1 parent 76d23d4 commit acdc78b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
37 changes: 24 additions & 13 deletions include/podio/detail/Association.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ class AssociationT {
friend AssociationCollectionIteratorT<FromT, ToT, Mutable>;
friend AssociationT<FromT, ToT, !Mutable>;

/// Helper member variable to check whether FromU and ToU can be used for this
/// Association. We need this to make SFINAE trigger in some cases below
template <typename FromU, typename ToU>
constexpr static bool sameTypes = std::is_same_v<FromU, FromT> && std::is_same_v<ToU, ToT>;

/// Variable template to for determining whether T is either FromT or ToT.
/// Mainly defined for convenience
template <typename T>
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>
static constexpr bool isMutableFromOrToT =
detail::isInTuple<T,
std::tuple<FromT, ToT, detail::GetMutableHandleType<FromT>, detail::GetMutableHandleType<ToT>>>;

public:
using mutable_type = podio::MutableAssociation<FromT, ToT>;
using value_type = podio::Association<FromT, ToT>;
Expand All @@ -57,15 +74,13 @@ class AssociationT {
}

/// Implicit conversion of mutable to immutable associations
template <typename FromU, typename ToU,
typename = std::enable_if_t<Mutable && std::is_same_v<FromU, FromT> && std::is_same_v<ToU, ToT>>>
template <typename FromU, typename ToU, typename = std::enable_if_t<Mutable && sameTypes<FromU, ToU>>>
operator AssociationT<FromU, ToU, false>() const {
return AssociationT<FromU, ToU, false>(m_obj);
}

/// Create a mutable deep-copy with identical relations
template <typename FromU = FromT, typename ToU = ToT,
typename = std::enable_if_t<std::is_same_v<FromU, FromT> && std::is_same_v<ToU, ToT>>>
template <typename FromU = FromT, typename ToU = ToT, typename = std::enable_if_t<sameTypes<FromU, ToU>>>
MutableAssociation<FromU, ToU> clone(bool cloneRelations = true) const {
auto tmp = new AssociationObjT(podio::ObjectID{}, m_obj->weight);
if (cloneRelations) {
Expand Down Expand Up @@ -138,7 +153,7 @@ class AssociationT {
///
/// @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>>>
template <typename T, typename = std::enable_if_t<!std::is_same_v<ToT, FromT> && isFromOrToT<T>>>
T get() const {
if constexpr (std::is_same_v<T, FromT>) {
return getFrom();
Expand Down Expand Up @@ -174,9 +189,7 @@ class AssociationT {
///
/// @tparam T type of value (**infered!**)
/// @param value the element to set for this association.
template <
typename T,
typename = std::enable_if_t<Mutable && !std::is_same_v<ToT, FromT> && detail::isMutableFromOrToT<T, FromT, ToT>>>
template <typename T, typename = std::enable_if_t<Mutable && !std::is_same_v<ToT, FromT> && isMutableFromOrToT<T>>>
void set(T value) {
if constexpr (std::is_same_v<T, FromT>) {
setFrom(std::move(value));
Expand Down Expand Up @@ -215,14 +228,12 @@ class AssociationT {
return !(*this == other);
}

template <typename FromU, typename ToU,
typename = std::enable_if_t<std::is_same_v<FromU, FromT> && std::is_same_v<ToU, ToT>>>
template <typename FromU, typename ToU, typename = std::enable_if_t<sameTypes<FromU, ToU>>>
bool operator==(const AssociationT<FromU, ToU, !Mutable>& other) const {
return m_obj == other.m_obj;
}

template <typename FromU, typename ToU,
typename = std::enable_if_t<std::is_same_v<FromU, FromT> && std::is_same_v<ToU, ToT>>>
template <typename FromU, typename ToU, typename = std::enable_if_t<sameTypes<FromU, ToU>>>
bool operator!=(const AssociationT<FromU, ToU, !Mutable>& other) const {
return !(*this == other);
}
Expand All @@ -241,7 +252,7 @@ class AssociationT {
explicit AssociationT(podio::utils::MaybeSharedPtr<AssociationObjT> obj) : m_obj(std::move(obj)) {
}

template <bool Mut, typename = std::enable_if_t<!Mut>>
template <typename FromU, typename ToU, typename = std::enable_if_t<!Mutable && sameTypes<FromU, ToU>>>
AssociationT(AssociationObjT* obj) : m_obj(podio::utils::MaybeSharedPtr<AssociationObjT>(obj)) {
}

Expand Down
11 changes: 0 additions & 11 deletions include/podio/detail/AssociationFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
namespace podio {
namespace detail {

/// 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, GetMutableHandleType<FromT>, GetMutableHandleType<ToT>>>;

/// Get the collection type name for an AssociationCollection
///
/// @tparam FromT the From type of the association
Expand Down

0 comments on commit acdc78b

Please sign in to comment.