COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
type_traits.h
Go to the documentation of this file.
1//
2// Copyright 2013 - 2024 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
10
11#pragma once
12
13#include <type_traits>
15
16namespace comms
17{
18
19namespace util
20{
21
22template <typename...>
23struct EmptyStruct {};
24
26template <bool TCond>
28{
29 template <typename TTrue, typename TFalse>
30 using Type = TTrue;
31};
32
33template <>
35{
36 template <typename TTrue, typename TFalse>
37 using Type = TFalse;
38};
39
41template <bool TCond, typename TTrue, typename TFalse>
42using ConditionalT = typename Conditional<TCond>::template Type<TTrue, TFalse>;
43
44template <bool TCond>
45struct LazyShallowConditional
46{
47 template <template<typename...> class TTrue, template<typename...> class TFalse, typename... TParams>
48 using Type = TTrue<TParams...>;
49};
50
51template <>
52struct LazyShallowConditional<false>
53{
54 template <template<typename...> class TTrue, template<typename...> class TFalse, typename... TParams>
55 using Type = TFalse<TParams...>;
56};
57
58template <bool TCond>
59struct LazyDeepConditional
60{
61 template <template<typename...> class TTrue, template<typename...> class TFalse, typename... TParams>
62 using Type = typename TTrue<>::template Type<TParams...>;
63};
64
65template <>
66struct LazyDeepConditional<false>
67{
68 template <template<typename...> class TTrue, template<typename...> class TFalse, typename... TParams>
69 using Type = typename TFalse<>::template Type<TParams...>;
70};
71
72template <bool TCond>
73struct LazyShallowDeepConditional
74{
75 template <template<typename...> class TTrue, template<typename...> class TFalse, typename... TParams>
76 using Type = TTrue<TParams...>;
77};
78
79template <>
80struct LazyShallowDeepConditional<false>
81{
82 template <template<typename...> class TTrue, template<typename...> class TFalse, typename... TParams>
83 using Type = typename TFalse<>::template Type<TParams...>;
84};
85
86template <typename...>
87class TypeDeepWrap
88{
89public:
90 template <typename T, typename...>
91 using Type = T;
92};
93
94template <typename...>
95class FieldCheckVersionDependent
96{
97public:
98 template <typename T>
99 using Type = std::integral_constant<bool, T::isVersionDependent()>;
100};
101
102template <typename...>
103class FieldCheckNonDefaultRefresh
104{
105public:
106 template <typename T>
107 using Type = std::integral_constant<bool, T::hasNonDefaultRefresh()>;
108};
109
110template <typename...>
111class FieldCheckVarLength
112{
113public:
114 template <typename T>
115 using Type = std::integral_constant<bool, T::minLength() != T::maxLength()>;
116};
117
118template <typename...>
119class FieldCheckReadNoStatus
120{
121public:
122 template <typename T>
123 using Type = std::integral_constant<bool, T::hasReadNoStatus()>;
124};
125
126template <typename...>
127class FieldCheckWriteNoStatus
128{
129public:
130 template <typename T>
131 using Type = std::integral_constant<bool, T::hasWriteNoStatus()>;
132};
133
134template <typename...>
135class TrueType
136{
137public:
138 template <typename...>
139 using Type = std::true_type;
140};
141
142
143template <typename...>
144class FalseType
145{
146public:
147 template <typename...>
148 using Type = std::false_type;
149};
150
151template <typename...>
152class AliasType
153{
154public:
155 template <typename T, typename...>
156 using Type = T;
157};
158
159template <typename...>
160struct LogicalOrBinaryOp
161{
162 // TFirst and TSecond are either std::true_type || std::false_type
163 template <typename TFirst, typename TSecond>
164 using Type = std::integral_constant<bool, (TFirst::value || TSecond::value)>;
165};
166
167template <typename...>
168struct LogicalAndBinaryOp
169{
170 // TFirst and TSecond are either std::true_type || std::false_type
171 template <typename TFirst, typename TSecond>
172 using Type = std::integral_constant<bool, (TFirst::value && TSecond::value)>;
173};
174
175template <typename...>
176struct IntMaxBinaryOp
177{
178 template <typename TFirst, typename TSecond>
179 using Type =
180 typename Conditional<
181 (TFirst::value >= TSecond::value)
182 >::template Type<
183 TFirst,
184 TSecond
185 >;
186};
187
188template <typename...>
189struct IntMinBinaryOp
190{
191 template <typename TFirst, typename TSecond>
192 using Type =
193 typename Conditional<
194 (TFirst::value <= TSecond::value)
195 >::template Type<
196 TFirst,
197 TSecond
198 >;
199};
200
201template <typename...>
202struct IntSumBinaryOp
203{
204 template <typename TFirst, typename TSecond>
205 using Type =
206 std::integral_constant<typename TFirst::value_type, TFirst::value + TSecond::value>;
207};
208
209template <typename...>
210class FieldMinLengthIntType
211{
212public:
213 template <typename T>
214 using Type = std::integral_constant<std::size_t, T::minLength()>;
215};
216
217template <typename...>
218class FieldMaxLengthIntType
219{
220public:
221 template <typename T>
222 using Type = std::integral_constant<std::size_t, T::maxLength()>;
223};
224
225template <typename...>
226class FieldBitLengthIntType
227{
228public:
229 template <typename T>
230 using Type =
231 typename details::FieldBitLengthIntTypeImpl<
232 T::ParsedOptions::HasFixedBitLengthLimit
233 >::template Type<T>;
234};
235
236template <typename...>
237class AlignmentIntType
238{
239public:
240 template <typename T>
241 using Type = std::integral_constant<std::size_t, alignof(T)>;
242};
243
244template <typename...>
245class SizeIntType
246{
247public:
248 template <typename T>
249 using Type = std::integral_constant<std::size_t, sizeof(T)>;
250};
251
252template <typename...>
253class MessageCheckHasStaticId
254{
255public:
256 template <typename TMessage>
257 using Type = std::integral_constant<bool, TMessage::hasStaticMsgId()>;
258};
259
260template <typename...>
261struct AccumulateFromUntil
262{
263 template <
264 std::size_t TFrom,
265 std::size_t TUntil,
266 template<typename...> class TTransformOp,
267 template<typename...> class TBinaryOp,
268 typename TStart,
269 typename... TRest>
270 using Type =
271 typename details::AccumulateFromUntilImpl<
272 (0 == sizeof...(TRest)) || (TUntil == 0U)
273 >::template Type<
274 TFrom,
275 TUntil,
276 TTransformOp,
277 TBinaryOp,
278 TStart,
279 TRest...>;
280};
281
282template <typename...>
283struct Accumulate
284{
285 template <
286 template<typename...> class TTransformOp,
287 template<typename...> class TBinaryOp,
288 typename TStart,
289 typename... TRest>
290 using Type =
291 typename AccumulateFromUntil<>::template Type<
292 0,
293 sizeof...(TRest),
294 TTransformOp,
295 TBinaryOp,
296 TStart,
297 TRest...
298 >;
299};
300
301} // namespace util
302
303} // namespace comms
Replacement to some types from standard type_traits.
typename Conditional< TCond >::template Type< TTrue, TFalse > ConditionalT
Replacement to std::conditional.
Definition type_traits.h:42
Main namespace for all classes / functions of COMMS library.
Replacement to std::conditional.
Definition type_traits.h:28