COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
BitmaskValue.h
Go to the documentation of this file.
1//
2// Copyright 2014 - 2025 (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 "comms/Field.h"
15
16#include "comms/util/SizeToType.h"
17#include "details/AdaptBasicField.h"
18#include "details/OptionsParser.h"
19#include "comms/details/gen_enum.h"
20#include "comms/details/bits_access.h"
22#include "comms/field/tag.h"
23
24namespace comms
25{
26
27namespace field
28{
29
30namespace details
31{
32
33template <bool THasFixedLength>
34struct BitmaskUndertlyingType;
35
36template <>
37struct BitmaskUndertlyingType<true>
38{
39 template <typename TOptionsBundle>
40 using Type = typename comms::util::SizeToType<TOptionsBundle::FixedLength, false>::Type;
41};
42
43template <>
44struct BitmaskUndertlyingType<false>
45{
46 template <typename TOptionsBundle>
47 using Type = unsigned;
48};
49
50template <typename TOptionsBundle>
51using BitmaskUndertlyingTypeT =
52 typename BitmaskUndertlyingType<TOptionsBundle::HasFixedLengthLimit>::template Type<TOptionsBundle>;
53
54
55} // namespace details
56
103template <typename TFieldBase, typename... TOptions>
104class BitmaskValue : public TFieldBase
105{
106 using BaseImpl = TFieldBase;
107
108 using OptionsBundle = details::OptionsParser<TOptions...>;
109
110 using IntValueType = details::BitmaskUndertlyingTypeT<OptionsBundle>;
111
112 using IntValueField =
113 IntValue<
114 TFieldBase,
115 IntValueType,
116 TOptions...
117 >;
118
119public:
121 using FieldBase = TFieldBase;
122
124 using Endian = typename BaseImpl::Endian;
125
127 using VersionType = typename BaseImpl::VersionType;
128
130 using ParsedOptions = OptionsBundle;
131
133 using CommsTag = tag::Bitmask;
134
139
143 using FieldType = typename ParsedOptions::FieldType;
144
147 BitmaskValue() = default;
148
151 explicit BitmaskValue(const ValueType& val)
152 : intValue_(val)
153 {
154 }
155
157 BitmaskValue(const BitmaskValue&) = default;
158
160 ~BitmaskValue() noexcept = default;
161
163 BitmaskValue& operator=(const BitmaskValue&) = default;
164
167 static constexpr bool hasFailOnInvalid()
168 {
169 return ParsedOptions::HasFailOnInvalid;
170 }
171
174 static constexpr bool hasIgnoreInvalid()
175 {
176 return ParsedOptions::HasIgnoreInvalid;
177 }
178
181 static constexpr bool hasEmptySerialization()
182 {
183 return ParsedOptions::HasEmptySerialization;
184 }
185
188 static constexpr bool hasFieldType()
189 {
190 return ParsedOptions::HasFieldType;
191 }
192
195 static constexpr bool hasFixedValue()
196 {
197 return ParsedOptions::HasFixedValue;
198 }
199
202 static constexpr bool hasName()
203 {
204 return ParsedOptions::HasName;
205 }
206
209 const ValueType& value() const
210 {
211 return intValue_.value();
212 }
213
217 {
218 return intValue_.value();
219 }
220
222 const ValueType& getValue() const
223 {
224 return intValue_.getValue();
225 }
226
228 template <typename U>
229 void setValue(U&& val)
230 {
231 intValue_.setValue(std::forward<U>(val));
232 }
233
236 constexpr std::size_t length() const
237 {
238 return intValue_.length();
239 }
240
243 static constexpr std::size_t maxLength()
244 {
246 }
247
250 static constexpr std::size_t minLength()
251 {
253 }
254
260 template <typename TIter>
261 ErrorStatus read(TIter& iter, std::size_t size)
262 {
263 return intValue_.read(iter, size);
264 }
265
268 static constexpr bool hasReadNoStatus()
269 {
270 return BaseImpl::hasReadNoStatus();
271 }
272
278 template <typename TIter>
279 void readNoStatus(TIter& iter)
280 {
281 intValue_.readNoStatus(iter);
282 }
283
285 bool canWrite() const
286 {
287 return BaseImpl::canWrite();
288 }
289
295 template <typename TIter>
296 ErrorStatus write(TIter& iter, std::size_t size) const
297 {
298 return intValue_.write(iter, size);
299 }
300
303 static constexpr bool hasWriteNoStatus()
304 {
305 return BaseImpl::hasWriteNoStatus();
306 }
307
313 template <typename TIter>
314 void writeNoStatus(TIter& iter) const
315 {
316 intValue_.writeNoStatus(iter);
317 }
318
320 constexpr bool valid() const
321 {
322 return intValue_.valid();
323 }
324
327 bool refresh()
328 {
329 return intValue_.refresh();
330 }
331
335 bool hasAllBitsSet(ValueType mask) const
336 {
337 return (value() & mask) == mask;
338 }
339
343 bool hasAnyBitsSet(ValueType mask) const
344 {
345 return (value() & mask) != 0;
346 }
347
352 {
353 value() |= mask;
354 }
355
360 {
361 value() &= static_cast<ValueType>(~mask);
362 }
363
365 bool getBitValue(unsigned bitNum) const
366 {
367 return hasAllBitsSet(
368 static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum));
369 }
370
372 void setBitValue(unsigned bitNum, bool val)
373 {
374 auto mask = static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum);
375 if (val) {
376 setBits(mask);
377 }
378 else {
379 clearBits(mask);
380 }
381 }
382
384 static constexpr bool isVersionDependent()
385 {
387 }
388
390 static constexpr bool hasNonDefaultRefresh()
391 {
392 return BaseImpl::hasNonDefaultRefresh();
393 }
394
398 {
399 return intValue_.getVersion();
400 }
401
405 {
406 return intValue_.setVersion(version);
407 }
408
416 void setForcedLength(int len)
417 {
418 intValue_.setForcedLength(len);
419 }
420
423 int getForcedLength() const
424 {
425 return intValue_.getForcedLength();
426 }
427
428protected:
429 using BaseImpl::readData;
430 using BaseImpl::writeData;
431
432private:
433
434 static_assert(!ParsedOptions::HasSerOffset,
435 "comms::option::def::NumValueSerOffset option is not applicable to BitmaskValue field");
436 static_assert(!ParsedOptions::HasVarLengthLimits,
437 "comms::option::def::VarLength option is not applicable to BitmaskValue field");
438 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
439 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to BitmaskValue field");
440 static_assert(!ParsedOptions::HasSequenceSizeForcing,
441 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to BitmaskValue field");
442 static_assert(!ParsedOptions::HasSequenceLengthForcing,
443 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to BitmaskValue field");
444 static_assert(!ParsedOptions::HasSequenceFixedSize,
445 "comms::option::def::SequenceFixedSize option is not applicable to BitmaskValue field");
446 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
447 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to BitmaskValue field");
448 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
449 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to BitmaskValue field");
450 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
451 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to BitmaskValue field");
452 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
453 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to BitmaskValue field");
454 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
455 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to BitmaskValue field");
456 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
457 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to BitmaskValue field");
458 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
459 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to BitmaskValue field");
460 static_assert(!ParsedOptions::HasFixedSizeStorage,
461 "comms::option::app::FixedSizeStorage option is not applicable to BitmaskValue field");
462 static_assert(!ParsedOptions::HasCustomStorageType,
463 "comms::option::app::CustomStorageType option is not applicable to BitmaskValue field");
464 static_assert(!ParsedOptions::HasScalingRatio,
465 "comms::option::def::ScalingRatio option is not applicable to BitmaskValue field");
466 static_assert(!ParsedOptions::HasUnits,
467 "comms::option::def::Units option is not applicable to BitmaskValue field");
468 static_assert(!ParsedOptions::HasOrigDataView,
469 "comms::option::app::OrigDataView option is not applicable to BitmaskValue field");
470 static_assert(!ParsedOptions::HasMultiRangeValidation,
471 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to BitmaskValue field");
472 static_assert(!ParsedOptions::HasVersionsRange,
473 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to BitmaskValue field");
474 static_assert(!ParsedOptions::HasInvalidByDefault,
475 "comms::option::def::InvalidByDefault option is not applicable to BitmaskValue field");
476 static_assert(!ParsedOptions::HasMissingOnReadFail,
477 "comms::option::def::MissingOnReadFail option is not applicable to BitmaskValue field");
478 static_assert(!ParsedOptions::HasMissingOnInvalid,
479 "comms::option::def::MissingOnInvalid option is not applicable to BitmaskValue field");
480 IntValueField intValue_;
481};
482
483// Implementation
484
490template <typename TFieldBase, typename... TOptions>
493 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
494{
495 return field1.value() == field2.value();
496}
497
503template <typename TFieldBase, typename... TOptions>
506 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
507{
508 return field1.value() != field2.value();
509}
510
516template <typename TFieldBase, typename... TOptions>
519 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
520{
521 return field1.value() < field2.value();
522}
523
529template <typename T>
530constexpr bool isBitmaskValue()
531{
532 return std::is_same<typename T::CommsTag, tag::Bitmask>::value;
533}
534
538template <typename TFieldBase, typename... TOptions>
539inline
540BitmaskValue<TFieldBase, TOptions...>&
542{
543 return field;
544}
545
549template <typename TFieldBase, typename... TOptions>
550inline
551const BitmaskValue<TFieldBase, TOptions...>&
553{
554 return field;
555}
556
557
606#define COMMS_BITMASK_BITS(...) COMMS_DEFINE_ENUM(BitIdx, __VA_ARGS__)
607
700#define COMMS_BITMASK_BITS_ACCESS(...) \
701 COMMS_AS_BITMASK_FUNC { \
702 return comms::field::toFieldBase(*this); \
703 }\
704 COMMS_AS_BITMASK_CONST_FUNC { \
705 return comms::field::toFieldBase(*this); \
706 } \
707 COMMS_DO_BIT_ACC_FUNC(asBitmask(), __VA_ARGS__)
708
719#define COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(...) \
720 COMMS_DO_BIT_ACC_FUNC((*this), __VA_ARGS__)
721
722
792#define COMMS_BITMASK_BITS_SEQ(...) \
793 COMMS_BITMASK_BITS(__VA_ARGS__) \
794 COMMS_BITMASK_BITS_ACCESS(__VA_ARGS__)
795
807#define COMMS_BITMASK_BITS_SEQ_NOTEMPLATE(...) \
808 COMMS_BITMASK_BITS(__VA_ARGS__) \
809 COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(__VA_ARGS__)
810
811
812} // namespace field
813
814} // namespace comms
815
816
Contains definition of comms::Field class.
Contains definition of comms::field::IntValue.
Bitmask value field.
Definition BitmaskValue.h:105
const ValueType & value() const
Get access to underlying mask value storage.
Definition BitmaskValue.h:209
void setForcedLength(int len)
Force serialization length of the field.
Definition BitmaskValue.h:416
void clearBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:359
int getForcedLength() const
Get forced serialization length.
Definition BitmaskValue.h:423
void setValue(U &&val)
Set value.
Definition BitmaskValue.h:229
VersionType getVersion() const
Get version of the field.
Definition BitmaskValue.h:397
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition BitmaskValue.h:261
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition BitmaskValue.h:195
bool operator<(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition BitmaskValue.h:517
typename IntValueField::ValueType ValueType
Type of underlying integral value.
Definition BitmaskValue.h:138
bool getBitValue(unsigned bitNum) const
Get bit value.
Definition BitmaskValue.h:365
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition BitmaskValue.h:303
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition BitmaskValue.h:384
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition BitmaskValue.h:268
constexpr bool valid() const
Check validity of the field value.
Definition BitmaskValue.h:320
typename BaseImpl::VersionType VersionType
Version type.
Definition BitmaskValue.h:127
bool setVersion(VersionType version)
Default implementation of version update.
Definition BitmaskValue.h:404
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition BitmaskValue.h:279
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition BitmaskValue.h:181
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition BitmaskValue.h:250
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition BitmaskValue.h:202
constexpr bool isBitmaskValue()
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue.
Definition BitmaskValue.h:530
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition BitmaskValue.h:188
void setBitValue(unsigned bitNum, bool val)
Set bit value.
Definition BitmaskValue.h:372
ValueType & value()
Get access to underlying mask value storage.
Definition BitmaskValue.h:216
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition BitmaskValue.h:285
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition BitmaskValue.h:174
tag::Bitmask CommsTag
Tag indicating type of the field.
Definition BitmaskValue.h:133
const BitmaskValue< TFieldBase, TOptions... > & toFieldBase(const BitmaskValue< TFieldBase, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::BitmaskValue type in order to have ac...
Definition BitmaskValue.h:552
~BitmaskValue() noexcept=default
Destructor.
bool hasAnyBitsSet(ValueType mask) const
Check whether any bits from provided mask are set.
Definition BitmaskValue.h:343
bool refresh()
Refresh contents of the field.
Definition BitmaskValue.h:327
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition BitmaskValue.h:143
OptionsBundle ParsedOptions
All the options provided to this class bundled into struct.
Definition BitmaskValue.h:130
BitmaskValue(const ValueType &val)
Constructor.
Definition BitmaskValue.h:151
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition BitmaskValue.h:296
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition BitmaskValue.h:236
BitmaskValue(const BitmaskValue &)=default
Copy constructor.
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition BitmaskValue.h:121
bool operator!=(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition BitmaskValue.h:504
const ValueType & getValue() const
Get value.
Definition BitmaskValue.h:222
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition BitmaskValue.h:243
void setBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:351
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition BitmaskValue.h:167
BitmaskValue()=default
Default constructor.
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition BitmaskValue.h:390
BitmaskValue< TFieldBase, TOptions... > & toFieldBase(BitmaskValue< TFieldBase, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::BitmaskValue type in order to have ac...
Definition BitmaskValue.h:541
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition BitmaskValue.h:314
bool operator==(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equality comparison operator.
Definition BitmaskValue.h:491
bool hasAllBitsSet(ValueType mask) const
Check whether all bits from provided mask are set.
Definition BitmaskValue.h:335
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition BitmaskValue.h:124
typename BaseImpl::ValueType ValueType
Type of underlying integral value.
Definition IntValue.h:96
bool setVersion(VersionType version)
Default implementation of version update.
Definition IntValue.h:425
bool valid() const
Check validity of the field value.
Definition IntValue.h:328
int getForcedLength() const
Get forced serialization length.
Definition IntValue.h:444
VersionType getVersion() const
Get version of the field.
Definition IntValue.h:418
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition IntValue.h:308
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition IntValue.h:322
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition IntValue.h:364
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition IntValue.h:315
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition IntValue.h:346
void setForcedLength(int len)
Force serialization length of the field.
Definition IntValue.h:437
void setValue(U &&val)
Set value.
Definition IntValue.h:272
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition IntValue.h:381
const ValueType & value() const
Get access to integral value storage.
Definition IntValue.h:251
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition IntValue.h:399
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition IntValue.h:405
bool refresh()
Refresh the field's value.
Definition IntValue.h:335
const ValueType & getValue() const
Get value.
Definition IntValue.h:264
Contains definition of various tag classes.
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