COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
CommonFuncs.h
1//
2// Copyright 2017 - 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
8#pragma once
9
10#include <type_traits>
11#include <iterator>
12
13#include "comms/ErrorStatus.h"
14#include "comms/util/Tuple.h"
15#include "comms/field/details/FieldOpHelpers.h"
16
17namespace comms
18{
19
20namespace field
21{
22
23namespace basic
24{
25
26struct CommonFuncs
27{
28 template <typename TField, typename TIter>
29 static comms::ErrorStatus readSequence(TField& field, TIter& iter, std::size_t len)
30 {
31 field.clear();
32 auto remLen = len;
33 while (0 < remLen) {
34 auto& elem = field.createBack();
35 auto es = field.readElement(elem, iter, remLen);
37 field.value().pop_back();
38 return es;
39 }
40 }
41
43 }
44
45 template <typename TField, typename TIter>
46 static comms::ErrorStatus readSequenceN(TField& field, std::size_t count, TIter& iter, std::size_t& len)
47 {
48 field.clear();
49 while (0 < count) {
50 auto& elem = field.createBack();
51 auto es = field.readElement(elem, iter, len);
53 field.value().pop_back();
54 return es;
55 }
56 --count;
57 }
59 }
60
61 template <typename TField, typename TIter>
62 static void readSequenceNoStatusN(TField& field, std::size_t count, TIter& iter)
63 {
64 field.clear();
65 while (0 < count) {
66 auto& elem = field.createBack();
67 field.readElementNoStatus(elem, iter);
68 --count;
69 }
70 }
71
72 template <typename TField>
73 static bool canWriteSequence(const TField& field)
74 {
75 for (auto& elem : field.value()) {
76 if (!field.canWriteElement(elem)) {
77 return false;
78 }
79 }
80
81 return true;
82 }
83
84 template <typename TField, typename TIter>
85 static comms::ErrorStatus writeSequence(const TField& field, TIter& iter, std::size_t len)
86 {
87 auto es = ErrorStatus::Success;
88 auto remainingLen = len;
89 for (auto& elem : field.value()) {
90 if (!field.canWriteElement(elem)) {
92 break;
93 }
94
95 es = field.writeElement(elem, iter, remainingLen);
97 break;
98 }
99 }
100
101 return es;
102 }
103
104 template <typename TField, typename TIter>
105 static void writeSequenceNoStatus(TField& field, TIter& iter)
106 {
107 for (auto& elem : field.value()) {
108 field.writeElementNoStatus(elem, iter);
109 }
110 }
111
112 template <typename TField, typename TIter>
113 static comms::ErrorStatus writeSequenceN(const TField& field, std::size_t count, TIter& iter, std::size_t& len)
114 {
115 auto es = ErrorStatus::Success;
116 for (auto& elem : field.value()) {
117 if (count == 0) {
118 break;
119 }
120
121 es = field.writeElement(elem, iter, len);
122 if (es != ErrorStatus::Success) {
123 break;
124 }
125
126 --count;
127 }
128
129 return es;
130 }
131
132 template <typename TField, typename TIter>
133 static void writeSequenceNoStatusN(const TField& field, std::size_t count, TIter& iter)
134 {
135 for (auto& elem : field.value()) {
136 if (count == 0) {
137 break;
138 }
139
140 field.writeElementNoStatus(elem, iter);
141 --count;
142 }
143 }
144
145 template <typename TIter>
146 static void advanceWriteIterator(TIter& iter, std::size_t len)
147 {
148 using IterType = typename std::decay<decltype(iter)>::type;
149 using ByteType = typename std::iterator_traits<IterType>::value_type;
150 while (len > 0U) {
151 *iter = ByteType();
152 ++iter;
153 --len;
154 }
155 }
156
157 static constexpr std::size_t maxSupportedLength()
158 {
159 return 0xffff;
160 }
161
162 template <typename TFields, typename TVersionType>
163 static bool setVersionForMembers(TFields& fields, TVersionType version)
164 {
165 return comms::util::tupleAccumulate(fields, false, makeVersionUpdater(version));
166 }
167
168 template <typename... TFields>
169 using IsAnyFieldVersionDependentBoolType =
171 comms::util::tupleTypeIsAnyOf<std::tuple<TFields...> >(
172 comms::field::details::FieldVersionDependentCheckHelper<>())
173 >::template Type<
174 std::true_type,
175 std::false_type
176 >;
177
178 template <typename... TFields>
179 using FieldSelectMaxLengthIntType =
180 std::integral_constant<
181 std::size_t,
182 comms::util::tupleTypeAccumulate<std::tuple<TFields...> >(
183 std::size_t(0), comms::field::details::FieldMaxLengthCalcHelper<>())
184 >;
185
186 template <typename... TFields>
187 using FieldSumMaxLengthIntType =
188 std::integral_constant<
189 std::size_t,
190 comms::util::tupleTypeAccumulate<std::tuple<TFields...> >(
191 std::size_t(0), comms::field::details::FieldMaxLengthSumCalcHelper<>())
192 >;
193
194 template <std::size_t TFrom, std::size_t TUntil, typename... TFields>
195 using FieldSumMaxLengthFromUntilIntType =
196 std::integral_constant<
197 std::size_t,
198 comms::util::tupleTypeAccumulateFromUntil<TFrom, TUntil, std::tuple<TFields...> >(
199 std::size_t(0), comms::field::details::FieldMaxLengthSumCalcHelper<>())
200 >;
201
202 template <typename... TFields>
203 using FieldSumMinLengthIntType =
204 std::integral_constant<
205 std::size_t,
206 comms::util::tupleTypeAccumulate<std::tuple<TFields...> >(
207 std::size_t(0), comms::field::details::FieldMinLengthSumCalcHelper<>())
208 >;
209
210 template <std::size_t TFrom, std::size_t TUntil, typename... TFields>
211 using FieldSumMinLengthFromUntilIntType =
212 std::integral_constant<
213 std::size_t,
214 comms::util::tupleTypeAccumulateFromUntil<TFrom, TUntil, std::tuple<TFields...> >(
215 std::size_t(0), comms::field::details::FieldMinLengthSumCalcHelper<>())
216 >;
217
218 template <typename... TFields>
219 using FieldSumTotalBitLengthIntType =
220 std::integral_constant<
221 std::size_t,
222 comms::util::tupleTypeAccumulate<std::tuple<TFields...> >(
223 std::size_t(0), comms::field::details::FieldTotalBitLengthSumCalcHelper<>())
224 >;
225
226 template <std::size_t TFrom, std::size_t TUntil, typename... TFields>
227 using FieldSumTotalBitLengthFromUntilIntType =
228 std::integral_constant<
229 std::size_t,
230 comms::util::tupleTypeAccumulateFromUntil<TFrom, TUntil, std::tuple<TFields...> >(
231 std::size_t(0), comms::field::details::FieldTotalBitLengthSumCalcHelper<>())
232 >;
233
234 template <typename... TFields>
235 using AnyFieldHasNonDefaultRefreshBoolType =
237 comms::util::tupleTypeIsAnyOf<std::tuple<TFields...> >(
238 comms::field::details::FieldNonDefaultRefreshCheckHelper<>())
239 >::template Type<
240 std::true_type,
241 std::false_type
242 >;
243
244 template <typename... TFields>
245 using AllFieldsHaveReadNoStatusBoolType =
247 comms::util::tupleTypeAccumulate<std::tuple<TFields...> >(
248 true, comms::field::details::FieldReadNoStatusDetectHelper<>())
249 >::template Type<
250 std::true_type,
251 std::false_type
252 >;
253
254 template <typename... TFields>
255 using AllFieldsHaveWriteNoStatusBoolType =
257 comms::util::tupleTypeAccumulate<std::tuple<TFields...> >(
258 true, comms::field::details::FieldWriteNoStatusDetectHelper<>())
259 >::template Type<
260 std::true_type,
261 std::false_type
262 >;
263
264private:
265
266 template <typename TVersionType>
267 static comms::field::details::FieldVersionUpdateHelper<TVersionType> makeVersionUpdater(TVersionType version)
268 {
269 return comms::field::details::FieldVersionUpdateHelper<TVersionType>(version);
270 }
271};
272
273} // namespace basic
274
275} // namespace field
276
277} // namespace comms
This file contain definition of error statuses used by comms module.
Contains various tuple type manipulation classes and functions.
constexpr TValue tupleTypeAccumulate(const TValue &value, TFunc &&func)
Performs "accumulate" algorithm on every type of the tuple.
Definition Tuple.h:685
constexpr TValue tupleTypeAccumulateFromUntil(const TValue &value, TFunc &&func)
Performs "accumulate" algorithm on specified types inside the tuple.
Definition Tuple.h:713
constexpr bool tupleTypeIsAnyOf(TFunc &&func)
Compile time check of whether any type within a tuple has a certain condition.
Definition Tuple.h:990
constexpr TValue tupleAccumulate(TTuple &&tuple, const TValue &value, TFunc &&func)
Performs "accumulate" algorithm on every element of the tuple.
Definition Tuple.h:586
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:17
@ Success
Used to indicate successful outcome of the operation.
@ InvalidMsgData
Used to indicate that a message has invalid data.
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:64
Replacement to std::conditional.
Definition type_traits.h:28