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 <limits>
14#include <type_traits>
15#include <cstdint>
16#include <cstddef>
17
18namespace comms
19{
20
21namespace util
22{
23
24template <typename...>
25struct Accumulate;
26
27template <typename...>
28struct AccumulateFromUntil;
29
30template <bool TCond>
31struct Conditional;
32
33namespace details
34{
35
36template<bool TEmpty>
37struct AccumulateLoop
38{
39 template <
40 template<typename...> class TAlg,
41 template<typename...> class TTransformOp,
42 template<typename...> class TBinaryOp,
43 typename TStart,
44 typename... TRest
45 >
46 using Type = typename TAlg<>::template Type<TTransformOp, TBinaryOp, TStart, TRest...>;
47};
48
49template<>
50struct AccumulateLoop<true>
51{
52 template <
53 template<typename...> class TAlg,
54 template<typename...> class TTransformOp,
55 template<typename...> class TBinaryOp,
56 typename TStart,
57 typename... TRest
58 >
59 using Type = TStart;
60};
61
62template<bool TEmpty>
63struct AccumulateFromUntilLoop
64{
65 template <
66 std::size_t TFrom,
67 std::size_t TUntil,
68 template<typename...> class TAlg,
69 template<typename...> class TTransformOp,
70 template<typename...> class TBinaryOp,
71 typename TStart,
72 typename... TRest
73 >
74 using Type = typename TAlg<>::template Type<TFrom, TUntil, TTransformOp, TBinaryOp, TStart, TRest...>;
75};
76
77template<>
78struct AccumulateFromUntilLoop<true>
79{
80 template <
81 std::size_t TFrom,
82 std::size_t TUntil,
83 template<typename...> class TAlg,
84 template<typename...> class TTransformOp,
85 template<typename...> class TBinaryOp,
86 typename TStart,
87 typename... TRest
88 >
89 using Type = TStart;
90};
91
92
93template <bool TEmpty>
94struct AccumulateImpl
95{
96 template <
97 template<typename...> class TTransformOp,
98 template<typename...> class TBinaryOp,
99 typename TStart,
100 typename T,
101 typename... TRest>
102 using Type =
103 typename AccumulateLoop<0U == sizeof...(TRest)>::template Type<
104 comms::util::Accumulate,
105 TTransformOp,
106 TBinaryOp,
107 typename TBinaryOp<>::template Type<
108 TStart,
109 typename TTransformOp<>::template Type<T>
110 >,
111 TRest...>;
112};
113
114template <>
115struct AccumulateImpl<true>
116{
117 template <
118 template<typename...> class TTransformOp,
119 template<typename...> class TBinaryOp,
120 typename TStart,
121 typename...>
122 using Type = TStart;
123};
124
125template <bool TEmpty>
126struct AccumulateFromUntilImpl
127{
128 template <
129 std::size_t TFrom,
130 std::size_t TUntil,
131 template<typename...> class TTransformOp,
132 template<typename...> class TBinaryOp,
133 typename TStart,
134 typename T,
135 typename... TRest>
136 using Type =
137 typename Conditional<
138 0U < TFrom
139 >::template Type<
140 typename AccumulateFromUntilImpl<
141 0U == sizeof...(TRest)
142 >::template Type<
143 TFrom - 1U,
144 TUntil - 1U,
145 TTransformOp,
146 TBinaryOp,
147 TStart,
148 TRest...
149 >,
150 typename AccumulateFromUntilLoop<
151 (0U == sizeof...(TRest)) || (TUntil <= 1)
152 >::template Type<
153 0U,
154 TUntil - 1,
155 comms::util::AccumulateFromUntil,
156 TTransformOp,
157 TBinaryOp,
158 typename TBinaryOp<>::template Type<
159 TStart,
160 typename TTransformOp<>::template Type<T>
161 >,
162 TRest...>
163 >;
164};
165
166template <>
167struct AccumulateFromUntilImpl<true>
168{
169 template <
170 std::size_t TFrom,
171 std::size_t TUntil,
172 template<typename...> class TTransformOp,
173 template<typename...> class TBinaryOp,
174 typename TStart,
175 typename...>
176 using Type = TStart;
177};
178
179template <bool THasFixedBitLength>
180struct FieldBitLengthIntTypeImpl
181{
182 template <typename T>
183 using Type =
184 std::integral_constant<std::size_t, T::ParsedOptions::FixedBitLength>;
185};
186
187template <>
188struct FieldBitLengthIntTypeImpl<false>
189{
190public:
191 template <typename T>
192 using Type =
193 std::integral_constant<
194 std::size_t,
195 T::maxLength() * std::numeric_limits<std::uint8_t>::digits>;
196};
197
198} // namespace details
199
200} // namespace util
201
202} // namespace comms
Main namespace for all classes / functions of COMMS library.