COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
Bitfield.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 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 "comms/ErrorStatus.h"
14#include "comms/options.h"
15#include "basic/Bitfield.h"
16#include "details/AdaptBasicField.h"
17
18namespace comms
19{
20
21namespace field
22{
23
88template <typename TFieldBase, typename TMembers, typename... TOptions>
89class Bitfield : public
90 details::AdaptBasicFieldT<
91 basic::Bitfield<
92 TFieldBase,
93 details::OptionsParser<TOptions...>::ForcedMembersVersionDependency,
94 TMembers
95 >,
96 TOptions...
97 >
98{
99 using BaseImpl =
100 details::AdaptBasicFieldT<
101 basic::Bitfield<
102 TFieldBase,
103 details::OptionsParser<TOptions...>::ForcedMembersVersionDependency,
104 TMembers
105 >,
106 TOptions...
107 >;
108
109public:
111 using FieldBase = TFieldBase;
112
114 using Endian = typename BaseImpl::Endian;
115
117 using VersionType = typename BaseImpl::VersionType;
118
120 using ParsedOptions = details::OptionsParser<TOptions...>;
121
123 using CommsTag = typename BaseImpl::CommsTag;
124
128 using ValueType = typename BaseImpl::ValueType;
129
133 using FieldType = typename ParsedOptions::FieldType;
134
137 Bitfield() = default;
138
141 explicit Bitfield(const ValueType& val)
142 : BaseImpl(val)
143 {
144 }
145
148 explicit Bitfield(ValueType&& val)
149 : BaseImpl(std::move(val))
150 {
151 }
152
155 static constexpr bool hasFailOnInvalid()
156 {
157 return ParsedOptions::HasFailOnInvalid;
158 }
159
162 static constexpr bool hasIgnoreInvalid()
163 {
164 return ParsedOptions::HasIgnoreInvalid;
165 }
166
169 static constexpr bool hasEmptySerialization()
170 {
171 return ParsedOptions::HasEmptySerialization;
172 }
173
176 static constexpr bool hasFieldType()
177 {
178 return ParsedOptions::HasFieldType;
179 }
180
185 template <std::size_t TIdx>
186 static constexpr std::size_t memberBitLength()
187 {
188 return BaseImpl::template memberBitLength<TIdx>();
189 }
190
191
194 const ValueType& value() const
195 {
196 return BaseImpl::value();
197 }
198
202 {
203 return BaseImpl::value();
204 }
205
208 const ValueType& getValue() const
209 {
210 return BaseImpl::getValue();
211 }
212
215 template <typename U>
216 void setValue(U&& val)
217 {
218 BaseImpl::setValue(std::forward<U>(val));
219 }
220
223 constexpr std::size_t length() const
224 {
225 return BaseImpl::length();
226 }
227
230 static constexpr std::size_t minLength()
231 {
232 return BaseImpl::minLength();
233 }
234
237 static constexpr std::size_t maxLength()
238 {
239 return BaseImpl::maxLength();
240 }
241
247 template <typename TIter>
248 ErrorStatus read(TIter& iter, std::size_t size)
249 {
250 return BaseImpl::read(iter, size);
251 }
252
255 static constexpr bool hasReadNoStatus()
256 {
257 return BaseImpl::hasReadNoStatus();
258 }
259
265 template <typename TIter>
266 void readNoStatus(TIter& iter)
267 {
268 BaseImpl::readNoStatus(iter);
269 }
270
272 bool canWrite() const
273 {
274 return BaseImpl::canWrite();
275 }
276
282 template <typename TIter>
283 ErrorStatus write(TIter& iter, std::size_t size) const
284 {
285 return BaseImpl::write(iter, size);
286 }
287
290 static constexpr bool hasWriteNoStatus()
291 {
292 return BaseImpl::hasWriteNoStatus();
293 }
294
300 template <typename TIter>
301 void writeNoStatus(TIter& iter) const
302 {
303 BaseImpl::writeNoStatus(iter);
304 }
305
307 bool valid() const
308 {
309 return BaseImpl::valid();
310 }
311
315 bool refresh()
316 {
317 return BaseImpl::refresh();
318 }
319
321 static constexpr bool isVersionDependent()
322 {
323 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
324 }
325
327 static constexpr bool hasNonDefaultRefresh()
328 {
329 return BaseImpl::hasNonDefaultRefresh();
330 }
331
335 {
336 return BaseImpl::getVersion();
337 }
338
342 {
343 return BaseImpl::setVersion(version);
344 }
345
346protected:
347 using BaseImpl::readData;
348 using BaseImpl::writeData;
349
350private:
351 static_assert(!ParsedOptions::HasSerOffset,
352 "comms::option::def::NumValueSerOffset option is not applicable to Bitfield field");
353 static_assert(!ParsedOptions::HasFixedLengthLimit,
354 "comms::option::def::FixedLength option is not applicable to Bitfield field");
355 static_assert(!ParsedOptions::HasFixedBitLengthLimit,
356 "comms::option::def::FixedBitLength option is not applicable to Bitfield field");
357 static_assert(!ParsedOptions::HasVarLengthLimits,
358 "comms::option::def::VarLength option is not applicable to Bitfield field");
359 static_assert(!ParsedOptions::HasAvailableLengthLimit,
360 "comms::option::def::AvailableLengthLimit option is not applicable to Bitfield field");
361 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
362 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to Bitfield field");
363 static_assert(!ParsedOptions::HasSequenceSizeForcing,
364 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to Bitfield field");
365 static_assert(!ParsedOptions::HasSequenceLengthForcing,
366 "comms::option::def::SequenceLengthorcingEnabled option is not applicable to Bitfield field");
367 static_assert(!ParsedOptions::HasSequenceFixedSize,
368 "comms::option::def::SequenceFixedSize option is not applicable to Bitfield field");
369 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
370 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to Bitfield field");
371 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
372 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to Bitfield field");
373 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
374 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to Bitfield field");
375 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
376 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to Bitfield field");
377 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
378 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to Bitfield field");
379 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
380 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to Bitfield field");
381 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
382 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to Bitfield field");
383 static_assert(!ParsedOptions::HasFixedSizeStorage,
384 "comms::option::app::FixedSizeStorage option is not applicable to Bitfield field");
385 static_assert(!ParsedOptions::HasCustomStorageType,
386 "comms::option::app::CustomStorageType option is not applicable to Bitfield field");
387 static_assert(!ParsedOptions::HasScalingRatio,
388 "comms::option::def::ScalingRatio option is not applicable to Bitfield field");
389 static_assert(!ParsedOptions::HasUnits,
390 "comms::option::def::Units option is not applicable to Bitfield field");
391 static_assert(!ParsedOptions::HasOrigDataView,
392 "comms::option::app::OrigDataView option is not applicable to Bitfield field");
393 static_assert(!ParsedOptions::HasMultiRangeValidation,
394 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to Bitfield field");
395 static_assert(!ParsedOptions::HasVersionsRange,
396 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to Bitfield field");
397 static_assert(!ParsedOptions::HasInvalidByDefault,
398 "comms::option::def::InvalidByDefault option is not applicable to Bitfield field");
399 static_assert(!ParsedOptions::HasMissingOnReadFail,
400 "comms::option::def::MissingOnReadFail option is not applicable to Bitfield field");
401 static_assert(!ParsedOptions::HasMissingOnInvalid,
402 "comms::option::def::MissingOnInvalid option is not applicable to Bitfield field");
403};
404
410template <typename TFieldBase, typename TMembers, typename... TOptions>
413 const Bitfield<TFieldBase, TMembers, TOptions...>& field2) noexcept
414{
415 return field1.value() == field2.value();
416}
417
423template <typename TFieldBase, typename TMembers, typename... TOptions>
426 const Bitfield<TFieldBase, TMembers, TOptions...>& field2) noexcept
427{
428 return field1.value() != field2.value();
429}
430
436template <typename TFieldBase, typename TMembers, typename... TOptions>
439 const Bitfield<TFieldBase, TMembers, TOptions...>& field2) noexcept
440{
441 return field1.value() < field2.value();
442}
443
449template <typename T>
450constexpr bool isBitfield()
451{
452 return std::is_same<typename T::CommsTag, tag::Bitfield>::value;
453}
454
458template <typename TFieldBase, typename TMembers, typename... TOptions>
459inline
460Bitfield<TFieldBase, TMembers, TOptions...>&
465
469template <typename TFieldBase, typename TMembers, typename... TOptions>
470inline
471const Bitfield<TFieldBase, TMembers, TOptions...>&
473{
474 return field;
475}
476
477} // namespace field
478
479} // namespace comms
480
481
This file contain definition of error statuses used by comms module.
Bitfield field.
Definition Bitfield.h:98
const ValueType & value() const
Get access to the stored tuple of fields.
Definition Bitfield.h:194
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition Bitfield.h:114
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition Bitfield.h:248
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition Bitfield.h:123
Bitfield()=default
Default constructor.
VersionType getVersion() const
Get version of the field.
Definition Bitfield.h:334
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition Bitfield.h:223
Bitfield(const ValueType &val)
Constructor.
Definition Bitfield.h:141
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition Bitfield.h:169
Bitfield< TFieldBase, TMembers, TOptions... > & toFieldBase(Bitfield< TFieldBase, TMembers, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::Bitfield type in order to have access...
Definition Bitfield.h:461
bool operator==(const Bitfield< TFieldBase, TMembers, TOptions... > &field1, const Bitfield< TFieldBase, TMembers, TOptions... > &field2) noexcept
Equality comparison operator.
Definition Bitfield.h:411
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition Bitfield.h:266
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition Bitfield.h:111
constexpr bool isBitfield()
Compile time check function of whether a provided type is any variant of comms::field::Bitfield.
Definition Bitfield.h:450
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition Bitfield.h:283
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition Bitfield.h:230
Bitfield(ValueType &&val)
Constructor.
Definition Bitfield.h:148
bool refresh()
Refresh the field's contents.
Definition Bitfield.h:315
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition Bitfield.h:162
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition Bitfield.h:176
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition Bitfield.h:301
typename BaseImpl::ValueType ValueType
Value type.
Definition Bitfield.h:128
bool operator<(const Bitfield< TFieldBase, TMembers, TOptions... > &field1, const Bitfield< TFieldBase, TMembers, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Bitfield.h:437
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition Bitfield.h:272
bool operator!=(const Bitfield< TFieldBase, TMembers, TOptions... > &field1, const Bitfield< TFieldBase, TMembers, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition Bitfield.h:424
const Bitfield< TFieldBase, TMembers, TOptions... > & toFieldBase(const Bitfield< TFieldBase, TMembers, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::Bitfield type in order to have access...
Definition Bitfield.h:472
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition Bitfield.h:237
ValueType & value()
Get access to the stored tuple of fields.
Definition Bitfield.h:201
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition Bitfield.h:120
void setValue(U &&val)
Set value.
Definition Bitfield.h:216
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition Bitfield.h:321
bool setVersion(VersionType version)
Default implementation of version update.
Definition Bitfield.h:341
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition Bitfield.h:155
typename BaseImpl::VersionType VersionType
Version type.
Definition Bitfield.h:117
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition Bitfield.h:133
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition Bitfield.h:290
const ValueType & getValue() const
Get value.
Definition Bitfield.h:208
static constexpr std::size_t memberBitLength()
Retrieve number of bits specified member field consumes.
Definition Bitfield.h:186
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition Bitfield.h:327
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition Bitfield.h:255
bool valid() const
Check validity of the field value.
Definition Bitfield.h:307
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:17
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:64
STL namespace.
Contains definition of all the options used by the COMMS library.