yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
MetaUtils.h
Go to the documentation of this file.
1#ifndef META_UTILS_H
2#define META_UTILS_H
3
4#include <tuple>
5
6#define CHECK_TEST_RES(pred) MetaUtils::check(pred, __PRETTY_FUNCTION__)
7
9namespace MetaUtils {
10
11
12 template <class Func, std::size_t... Is>
13 constexpr void staticForImpl(Func &&f,
14 std::index_sequence<Is...>) {
15 // Use C++17 fold expressions to apply Func f
16 // to all Is... in sequence
17 //
18 // Won't work on empty tuple (Binning is not intended to work with 0 dimensions).
19 //
20 // std::integral_constant used to encode value into type, otherwise it's not
21 // possible to use Is inside functor as consant expression
22 // (std::get<Is> etc. won't work)
23
24 ((void)f(std::integral_constant<std::size_t, Is>()), ...);
25 }
26
33 template <size_t N, class Func>
34 constexpr void staticFor(Func&& f) {
35 staticForImpl(std::forward<Func>(f),
36 std::make_index_sequence<N>{});
37 }
38
39
42 template<class...> struct conjunction : std::true_type { };
43 template<class B1> struct conjunction<B1> : B1 { };
44 template<class B1, class... Bn>
45 struct conjunction<B1, Bn...>
46 : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
47
48 struct nonesuch {
49 ~nonesuch() = delete;
50 nonesuch(nonesuch const&) = delete;
51 void operator=(nonesuch const&) = delete;
52 };
53
54 namespace operatorTraits {
55 template<class T>
56 using addition_assignment_t = decltype(std::declval<T&>() += std::declval<const T&>());
57 }
58
61 namespace detail {
62 template <class Default, class AlwaysVoid,
63 template<class...> class Op, class... Args>
64 struct detector {
65 using value_t = std::false_type;
66 using type = Default;
67 };
68
69 template <class Default, template<class...> class Op, class... Args>
70 struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
71 using value_t = std::true_type;
72 using type = Op<Args...>;
73 };
74
75 } // namespace detail
76
78 namespace {
79 template <typename, template <typename...> class>
80 struct is_instance_impl : public std::false_type {};
81
82 template <template <typename...> class U, typename...Ts>
83 struct is_instance_impl<U<Ts...>, U> : public std::true_type {};
84 }
85
87 template <typename T, template <typename ...> class U>
88 using is_instance = is_instance_impl<std::decay_t<T>, U>;
89
90 template <template<class...> class Op, class... Args>
91 using is_detected = typename detail::detector<nonesuch, void, Op, Args...>::value_t;
92
93 template <template<class...> class Op, class... Args>
94 using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;
95
96 template <class Default, template<class...> class Op, class... Args>
97 using detected_or = detail::detector<Default, void, Op, Args...>;
98
99 template <class Expected, template<class...> class Op, class... Args>
100 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
101
102 template< template<class...> class Op, class... Args >
103 constexpr bool is_detected_v = is_detected<Op, Args...>::value;
104
109 template <typename T, template <class U> class Concept>
110 constexpr bool checkConcept() {
111 return Concept<T>::checkResult::value;
112 }
113
114 inline int check(bool pred, const char* funcName) {
115 if (pred == false)
116 std::clog << funcName << " FAILED\n";
117 return (int)(pred ? EXIT_SUCCESS : EXIT_FAILURE);
118 }
119
120
121 namespace {
122
123 template <size_t, size_t>
124 struct _tuple {
125 template <typename T>
126 static auto remove (const T& t) {
127 return std::make_tuple(t);
128 }
129 };
130
131 template <size_t N>
132 struct _tuple<N, N> {
133 template <typename T>
134 static std::tuple<> remove (const T&) {
135 return {};
136 }
137 };
138
139 template<size_t N, typename T, size_t... Is>
140 auto _removeTupleElement(const T& tup, std::index_sequence<Is...>) {
141 return std::tuple_cat( _tuple<Is, N>::remove(std::get<Is>(tup))...);
142 }
143
144 }
145
146 template<size_t I, typename... Ts>
147 auto removeTupleElement(const std::tuple<Ts...>& tup) {
148 return _removeTupleElement<I>(tup, std::make_index_sequence<sizeof...(Ts)>{});
149 }
150
151
152}
153
154#endif
decltype(std::declval< T & >()+=std::declval< const T & >()) addition_assignment_t
Definition MetaUtils.h:56
constexpr void staticForImpl(Func &&f, std::index_sequence< Is... >)
Definition MetaUtils.h:13
constexpr void staticFor(Func &&f)
Used to apply functor on tuple. Calls lambda with integral constant, which can be used to query tuple...
Definition MetaUtils.h:34
auto removeTupleElement(const std::tuple< Ts... > &tup)
Definition MetaUtils.h:147
typename detail::detector< nonesuch, void, Op, Args... >::type detected_t
Definition MetaUtils.h:94
typename detail::detector< nonesuch, void, Op, Args... >::value_t is_detected
Definition MetaUtils.h:91
constexpr bool is_detected_v
Definition MetaUtils.h:103
is_instance_impl< std::decay_t< T >, U > is_instance
Detects if type T is an instance of template U.
Definition MetaUtils.h:88
int check(bool pred, const char *funcName)
Definition MetaUtils.h:114
std::is_same< Expected, detected_t< Op, Args... > > is_detected_exact
Definition MetaUtils.h:100
constexpr bool checkConcept()
checks whether if T satisfies user defined Concept
Definition MetaUtils.h:110
Logical conjuction implementation.
Definition MetaUtils.h:42
std::false_type value_t
Definition MetaUtils.h:65
nonesuch(nonesuch const &)=delete
void operator=(nonesuch const &)=delete