Sen API
Sen Libraries
Loading...
Searching...
No Matches
class_helpers.h
Go to the documentation of this file.
1// === class_helpers.h =================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_CLASS_HELPERS_H
9#define SEN_CORE_BASE_CLASS_HELPERS_H
10
12
13#include <type_traits>
14#include <utility>
15#include <variant>
16
20
21namespace sen
22{
23
24//--------------------------------------------------------------------------------------------------------------
25// Metaprogramming helpers
26//--------------------------------------------------------------------------------------------------------------
27
30
32template <typename T>
33struct ShouldBePassedByValue: std::false_type
34{
35};
36
37template <typename T>
39
41template <typename T>
42using AddConstRef = std::add_lvalue_reference_t<std::add_const_t<T>>;
43
45template <typename T>
46using MaybeRef = std::conditional_t<std::is_arithmetic_v<T> || shouldBePassedByValueV<T>, T, AddConstRef<T>>;
47
49template <typename, typename = std::void_t<>>
50struct HasValueType: std::false_type
51{
52};
53
55template <typename T>
56struct HasValueType<T, std::void_t<typename T::ValueType>>: std::true_type
57{
58};
59
61template <typename S, typename T = S>
63{
64private:
65 template <typename SS, typename TT>
66 static auto eqTest(unsigned) -> decltype(std::declval<SS&>() == std::declval<TT>(), std::true_type());
67
68 template <typename, typename>
69 static auto eqTest(...) -> std::false_type;
70
71 template <typename SS, typename TT>
72 static auto neTest(unsigned) -> decltype(std::declval<SS&>() != std::declval<TT>(), std::true_type());
73
74 template <typename, typename>
75 static auto neTest(...) -> std::false_type;
76
77 template <typename SS, typename TT>
78 static auto ltTest(unsigned) -> decltype(std::declval<SS&>() < std::declval<TT>(), std::true_type());
79
80 template <typename, typename>
81 static auto ltTest(...) -> std::false_type;
82
83 template <typename SS, typename TT>
84 static auto leTest(unsigned) -> decltype(std::declval<SS&>() <= std::declval<TT>(), std::true_type());
85
86 template <typename, typename>
87 static auto leTest(...) -> std::false_type;
88
89 template <typename SS, typename TT>
90 static auto gtTest(unsigned) -> decltype(std::declval<SS&>() > std::declval<TT>(), std::true_type());
91
92 template <typename, typename>
93 static auto gtTest(...) -> std::false_type;
94
95 template <typename SS, typename TT>
96 static auto geTest(unsigned) -> decltype(std::declval<SS&>() >= std::declval<TT>(), std::true_type());
97
98 template <typename, typename>
99 static auto geTest(...) -> std::false_type;
100
101 template <typename SS, typename TT>
102 static auto mulTest(unsigned) -> decltype(std::declval<SS&>() * std::declval<TT&>(), std::true_type());
103
104 template <typename, typename>
105 static auto mulTest(...) -> std::false_type;
106
107 template <typename SS>
108 static auto incTest(unsigned) -> decltype(std::declval<SS&>()++, std::true_type());
109
110 template <typename>
111 static auto incTest(...) -> std::false_type;
112
113 template <typename SS>
114 static auto decTest(unsigned) -> decltype(std::declval<SS&>()--, std::true_type());
115
116 template <typename>
117 static auto decTest(...) -> std::false_type;
118
119 template <typename SS, typename TT>
120 static auto modTest(unsigned) -> decltype(std::declval<SS&>() % std::declval<TT&>(), std::true_type());
121
122 template <typename, typename>
123 static auto modTest(...) -> std::false_type;
124
125public:
126 static constexpr bool eq = decltype(eqTest<S, T>(0U))::value;
127 static constexpr bool ne = decltype(neTest<S, T>(0U))::value;
128 static constexpr bool lt = decltype(ltTest<S, T>(0U))::value;
129 static constexpr bool le = decltype(leTest<S, T>(0U))::value;
130 static constexpr bool gt = decltype(gtTest<S, T>(0U))::value;
131 static constexpr bool ge = decltype(geTest<S, T>(0U))::value;
132 static constexpr bool mul = decltype(mulTest<S, T>(0U))::value;
133 static constexpr bool inc = decltype(incTest<S>(0U))::value;
134 static constexpr bool dec = decltype(decTest<S>(0U))::value;
135 static constexpr bool mod = decltype(modTest<S, T>(0U))::value;
136};
137
168template <class... Ts>
169struct Overloaded: Ts...
170{
171 using Ts::operator()...;
172};
173
175template <class... Ts>
176Overloaded(Ts...) -> Overloaded<Ts...>;
177
179template <typename T, typename... TypeList>
180struct IsContained: public std::disjunction<std::is_same<T, TypeList>...>
181{
182};
183
185template <typename T, typename VariantType>
187
189template <typename T, typename... MemberTypes>
190struct IsVariantMember<T, std::variant<MemberTypes...>>: public IsContained<T, MemberTypes...>
191{
192};
193
195
196} // namespace sen
197
198#endif // SEN_CORE_BASE_CLASS_HELPERS_H
constexpr bool shouldBePassedByValueV
Definition class_helpers.h:38
Overloaded(Ts...) -> Overloaded< Ts... >
Explicit deduction guide (not needed as of C++20).
std::conditional_t< std::is_arithmetic_v< T >||shouldBePassedByValueV< T >, T, AddConstRef< T > > MaybeRef
returns 'const T&' or 'T' depending on the type
Definition class_helpers.h:46
std::add_lvalue_reference_t< std::add_const_t< T > > AddConstRef
returns 'const T&'
Definition class_helpers.h:42
Checks if T is on of the member types of the given VariantType.
Definition class_helpers.h:186
Definition assert.h:17
STL namespace.
Allows compile-time check for the presence of various operators.
Definition class_helpers.h:63
static constexpr bool mod
Definition class_helpers.h:135
static constexpr bool ne
Definition class_helpers.h:127
static constexpr bool ge
Definition class_helpers.h:131
static constexpr bool dec
Definition class_helpers.h:134
static constexpr bool lt
Definition class_helpers.h:128
static constexpr bool mul
Definition class_helpers.h:132
static constexpr bool eq
Definition class_helpers.h:126
static constexpr bool le
Definition class_helpers.h:129
static constexpr bool gt
Definition class_helpers.h:130
static constexpr bool inc
Definition class_helpers.h:133
return false if T has not an 'using ValueType' or a 'typedef ValueType'
Definition class_helpers.h:51
Checks if T is on of the types in TypeList.
Definition class_helpers.h:181
Helper type for std::variant lambda visitors.
Definition class_helpers.h:170
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34