Skip to content

Commit 8c88cd5

Browse files
authored
DPL Analysis: Add pack selection based on condition with multiple parameters
1 parent c75a946 commit 8c88cd5

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

Framework/Foundation/include/Framework/Pack.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,28 @@ template <typename P1, typename P2>
100100
using interleaved_pack_t = decltype(interleave_pack(P1{}, P2{}));
101101

102102
/// Selects from the pack types that satisfy the Condition
103-
template <template <typename> typename Condition, typename Result>
104-
constexpr auto select_pack(Result result, pack<>)
103+
/// Multicondition takes the type to check as first template parameter
104+
/// and any helper types as the following parameters
105+
template <template <typename...> typename Condition, typename Result, typename... Cs>
106+
constexpr auto select_pack(Result result, pack<>, pack<Cs...>)
105107
{
106108
return result;
107109
}
108110

109-
template <template <typename> typename Condition, typename Result, typename T, typename... Ts>
110-
constexpr auto select_pack(Result result, pack<T, Ts...>)
111+
template <template <typename...> typename Condition, typename Result, typename T, typename... Cs, typename... Ts>
112+
constexpr auto select_pack(Result result, pack<T, Ts...>, pack<Cs...> condPack)
111113
{
112-
if constexpr (Condition<T>()) {
113-
return select_pack<Condition>(concatenate_pack(result, pack<T>{}), pack<Ts...>{});
114+
if constexpr (Condition<T, Cs...>()) {
115+
return select_pack<Condition>(concatenate_pack(result, pack<T>{}), pack<Ts...>{}, condPack);
114116
} else {
115-
return select_pack<Condition>(result, pack<Ts...>{});
117+
return select_pack<Condition>(result, pack<Ts...>{}, condPack);
116118
}
117119
}
118120

119-
template <template <typename> typename Condition, typename... Types>
120-
using selected_pack = std::decay_t<decltype(select_pack<Condition>(pack<>{}, pack<Types...>{}))>;
121+
template <template <typename...> typename Condition, typename... Types>
122+
using selected_pack = std::decay_t<decltype(select_pack<Condition>(pack<>{}, pack<Types...>{}, pack<>{}))>;
123+
template <template <typename...> typename Condition, typename CondPack, typename Pack>
124+
using selected_pack_multicondition = std::decay_t<decltype(select_pack<Condition>(pack<>{}, Pack{}, CondPack{}))>;
121125

122126
/// Select only the items of a pack which match Condition
123127
template <template <typename> typename Condition, typename Result>

Framework/Foundation/test/test_FunctionalHelpers.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ using namespace o2::framework;
2323
template <typename T>
2424
using is_int_t = std::is_same<typename std::decay_t<T>, int>;
2525

26+
template <typename T, typename T2>
27+
using is_same_as_second_t = std::is_same<typename std::decay_t<T>, T2>;
28+
2629
template <int A, int B>
2730
struct TestStruct {
2831
};
@@ -36,6 +39,7 @@ BOOST_AUTO_TEST_CASE(TestOverride)
3639
static_assert(has_type_at<double>(pck) == pack_size(pck) + 1, "double is not in the pack so the function returns size + 1");
3740

3841
static_assert(std::is_same_v<selected_pack<is_int_t, int, float, char>, pack<int>>, "selector should select int");
42+
static_assert(std::is_same_v<selected_pack_multicondition<is_same_as_second_t, pack<int>, pack<int, float, char>>, pack<int>>, "multiselector should select int");
3943
static_assert(std::is_same_v<filtered_pack<is_int_t, int, float, char>, pack<float, char>>, "filter should remove int");
4044
static_assert(std::is_same_v<intersected_pack_t<pack<int, float, char>, pack<float, double>>, pack<float>>, "filter intersect two packs");
4145
static_assert(has_type_v<pack_element_t<0, pack<int>>, pack<int>> == true, "foo");

0 commit comments

Comments
 (0)