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 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include "comms/details/bits_access.h"
16#include "comms/details/gen_enum.h"
17#include "comms/Field.h"
18#include "comms/field/details/AdaptBasicField.h"
19#include "comms/field/details/OptionsParser.h"
21#include "comms/field/tag.h"
22#include "comms/util/SizeToType.h"
23
24#include <cstddef>
25#include <cstdint>
26#include <type_traits>
27#include <utility>
28
29namespace comms
30{
31
32namespace field
33{
34
35namespace details
36{
37
38template <bool THasFixedLength>
39struct BitmaskUndertlyingType;
40
41template <>
42struct BitmaskUndertlyingType<true>
43{
44 template <typename TOptionsBundle>
45 using Type = typename comms::util::SizeToType<TOptionsBundle::FixedLength, false>::Type;
46};
47
48template <>
49struct BitmaskUndertlyingType<false>
50{
51 template <typename TOptionsBundle>
52 using Type = std::uintmax_t;
53};
54
55template <typename TOptionsBundle>
56using BitmaskUndertlyingTypeT =
57 typename BitmaskUndertlyingType<TOptionsBundle::HasFixedLengthLimit>::template Type<TOptionsBundle>;
58
59} // namespace details
60
107template <typename TFieldBase, typename... TOptions>
108class BitmaskValue : public TFieldBase
109{
110 using BaseImpl = TFieldBase;
111
112 using OptionsBundle = details::OptionsParser<TOptions...>;
113
114 using IntValueType = details::BitmaskUndertlyingTypeT<OptionsBundle>;
115
116 using IntValueField =
117 IntValue<
118 TFieldBase,
119 IntValueType,
120 TOptions...
121 >;
122
123public:
125 using FieldBase = TFieldBase;
126
128 using Endian = typename BaseImpl::Endian;
129
131 using VersionType = typename BaseImpl::VersionType;
132
134 using ParsedOptions = OptionsBundle;
135
137 using CommsTag = tag::Bitmask;
138
143
147 using FieldType = typename ParsedOptions::FieldType;
148
151 BitmaskValue() = default;
152
155 explicit BitmaskValue(const ValueType& val)
156 : m_intValue(val)
157 {
158 }
159
161 BitmaskValue(const BitmaskValue&) = default;
162
164 ~BitmaskValue() noexcept = default;
165
167 BitmaskValue& operator=(const BitmaskValue&) = default;
168
171 static constexpr bool hasFailOnInvalid()
172 {
173 return ParsedOptions::HasFailOnInvalid;
174 }
175
178 static constexpr bool hasIgnoreInvalid()
179 {
180 return ParsedOptions::HasIgnoreInvalid;
181 }
182
185 static constexpr bool hasEmptySerialization()
186 {
187 return ParsedOptions::HasEmptySerialization;
188 }
189
192 static constexpr bool hasFieldType()
193 {
194 return ParsedOptions::HasFieldType;
195 }
196
199 static constexpr bool hasFixedValue()
200 {
201 return ParsedOptions::HasFixedValue;
202 }
203
206 static constexpr bool hasName()
207 {
208 return ParsedOptions::HasName;
209 }
210
213 const ValueType& value() const
214 {
215 return m_intValue.value();
216 }
217
221 {
222 return m_intValue.value();
223 }
224
226 const ValueType& getValue() const
227 {
228 return m_intValue.getValue();
229 }
230
232 template <typename U>
233 void setValue(U&& val)
234 {
235 m_intValue.setValue(std::forward<U>(val));
236 }
237
240 constexpr std::size_t length() const
241 {
242 return m_intValue.length();
243 }
244
247 static constexpr std::size_t maxLength()
248 {
250 }
251
254 static constexpr std::size_t minLength()
255 {
257 }
258
264 template <typename TIter>
265 ErrorStatus read(TIter& iter, std::size_t size)
266 {
267 return m_intValue.read(iter, size);
268 }
269
272 static constexpr bool hasReadNoStatus()
273 {
274 return BaseImpl::hasReadNoStatus();
275 }
276
282 template <typename TIter>
283 void readNoStatus(TIter& iter)
284 {
285 m_intValue.readNoStatus(iter);
286 }
287
289 bool canWrite() const
290 {
291 return BaseImpl::canWrite();
292 }
293
299 template <typename TIter>
300 ErrorStatus write(TIter& iter, std::size_t size) const
301 {
302 return m_intValue.write(iter, size);
303 }
304
307 static constexpr bool hasWriteNoStatus()
308 {
309 return BaseImpl::hasWriteNoStatus();
310 }
311
317 template <typename TIter>
318 void writeNoStatus(TIter& iter) const
319 {
320 m_intValue.writeNoStatus(iter);
321 }
322
324 constexpr bool valid() const
325 {
326 return m_intValue.valid();
327 }
328
331 bool refresh()
332 {
333 return m_intValue.refresh();
334 }
335
339 bool hasAllBitsSet(ValueType mask) const
340 {
341 return (value() & mask) == mask;
342 }
343
347 bool hasAnyBitsSet(ValueType mask) const
348 {
349 return (value() & mask) != 0;
350 }
351
356 {
357 value() |= mask;
358 }
359
364 {
365 value() &= static_cast<ValueType>(~mask);
366 }
367
369 bool getBitValue(unsigned bitNum) const
370 {
371 return hasAllBitsSet(
372 static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum));
373 }
374
376 void setBitValue(unsigned bitNum, bool val)
377 {
378 auto mask = static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum);
379 if (val) {
380 setBits(mask);
381 }
382 else {
383 clearBits(mask);
384 }
385 }
386
388 static constexpr bool isVersionDependent()
389 {
391 }
392
394 static constexpr bool hasNonDefaultRefresh()
395 {
396 return BaseImpl::hasNonDefaultRefresh();
397 }
398
402 {
403 return m_intValue.getVersion();
404 }
405
409 {
410 return m_intValue.setVersion(version);
411 }
412
420 void setForcedLength(int len)
421 {
422 m_intValue.setForcedLength(len);
423 }
424
427 int getForcedLength() const
428 {
429 return m_intValue.getForcedLength();
430 }
431
432protected:
433 using BaseImpl::readData;
434 using BaseImpl::writeData;
435
436private:
437
438 static_assert(!ParsedOptions::HasSerOffset,
439 "comms::option::def::NumValueSerOffset option is not applicable to BitmaskValue field");
440 static_assert(!ParsedOptions::HasVarLengthLimits,
441 "comms::option::def::VarLength option is not applicable to BitmaskValue field");
442 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
443 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to BitmaskValue field");
444 static_assert(!ParsedOptions::HasSequenceSizeForcing,
445 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to BitmaskValue field");
446 static_assert(!ParsedOptions::HasSequenceLengthForcing,
447 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to BitmaskValue field");
448 static_assert(!ParsedOptions::HasSequenceFixedSize,
449 "comms::option::def::SequenceFixedSize option is not applicable to BitmaskValue field");
450 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
451 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to BitmaskValue field");
452 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
453 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to BitmaskValue field");
454 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
455 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to BitmaskValue field");
456 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
457 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to BitmaskValue field");
458 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
459 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to BitmaskValue field");
460 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
461 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to BitmaskValue field");
462 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
463 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to BitmaskValue field");
464 static_assert(!ParsedOptions::HasFixedSizeStorage,
465 "comms::option::app::FixedSizeStorage option is not applicable to BitmaskValue field");
466 static_assert(!ParsedOptions::HasCustomStorageType,
467 "comms::option::app::CustomStorageType option is not applicable to BitmaskValue field");
468 static_assert(!ParsedOptions::HasScalingRatio,
469 "comms::option::def::ScalingRatio option is not applicable to BitmaskValue field");
470 static_assert(!ParsedOptions::HasUnits,
471 "comms::option::def::Units option is not applicable to BitmaskValue field");
472 static_assert(!ParsedOptions::HasOrigDataView,
473 "comms::option::app::OrigDataView option is not applicable to BitmaskValue field");
474 static_assert(!ParsedOptions::HasMultiRangeValidation,
475 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to BitmaskValue field");
476 static_assert(!ParsedOptions::HasVersionsRange,
477 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to BitmaskValue field");
478 static_assert(!ParsedOptions::HasInvalidByDefault,
479 "comms::option::def::InvalidByDefault option is not applicable to BitmaskValue field");
480 static_assert(!ParsedOptions::HasMissingOnReadFail,
481 "comms::option::def::MissingOnReadFail option is not applicable to BitmaskValue field");
482 static_assert(!ParsedOptions::HasMissingOnInvalid,
483 "comms::option::def::MissingOnInvalid option is not applicable to BitmaskValue field");
484 IntValueField m_intValue;
485};
486
487// Implementation
488
494template <typename TFieldBase, typename... TOptions>
497 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
498{
499 return field1.value() == field2.value();
500}
501
507template <typename TFieldBase, typename... TOptions>
510 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
511{
512 return field1.value() != field2.value();
513}
514
520template <typename TFieldBase, typename... TOptions>
523 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
524{
525 return field1.value() < field2.value();
526}
527
533template <typename T>
534constexpr bool isBitmaskValue()
535{
536 return std::is_same<typename T::CommsTag, tag::Bitmask>::value;
537}
538
542template <typename TFieldBase, typename... TOptions>
543inline
544BitmaskValue<TFieldBase, TOptions...>&
546{
547 return field;
548}
549
553template <typename TFieldBase, typename... TOptions>
554inline
555const BitmaskValue<TFieldBase, TOptions...>&
557{
558 return field;
559}
560
609#define COMMS_BITMASK_BITS(...) COMMS_DEFINE_ENUM(BitIdx, __VA_ARGS__)
610
703#define COMMS_BITMASK_BITS_ACCESS(...) \
704 COMMS_AS_BITMASK_FUNC { \
705 return comms::field::toFieldBase(*this); \
706 }\
707 COMMS_AS_BITMASK_CONST_FUNC { \
708 return comms::field::toFieldBase(*this); \
709 } \
710 COMMS_DO_BIT_ACC_FUNC(asBitmask(), __VA_ARGS__)
711
722#define COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(...) \
723 COMMS_DO_BIT_ACC_FUNC((*this), __VA_ARGS__)
724
794#define COMMS_BITMASK_BITS_SEQ(...) \
795 COMMS_BITMASK_BITS(__VA_ARGS__) \
796 COMMS_BITMASK_BITS_ACCESS(__VA_ARGS__)
797
809#define COMMS_BITMASK_BITS_SEQ_NOTEMPLATE(...) \
810 COMMS_BITMASK_BITS(__VA_ARGS__) \
811 COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(__VA_ARGS__)
812
813} // namespace field
814
815} // namespace comms
816
Contains definition of comms::Field class.
Contains definition of comms::field::IntValue.
Bitmask value field.
Definition BitmaskValue.h:109
const ValueType & value() const
Get access to underlying mask value storage.
Definition BitmaskValue.h:213
void setForcedLength(int len)
Force serialization length of the field.
Definition BitmaskValue.h:420
void clearBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:363
int getForcedLength() const
Get forced serialization length.
Definition BitmaskValue.h:427
void setValue(U &&val)
Set value.
Definition BitmaskValue.h:233
VersionType getVersion() const
Get version of the field.
Definition BitmaskValue.h:401
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition BitmaskValue.h:265
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition BitmaskValue.h:199
bool operator<(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition BitmaskValue.h:521
typename IntValueField::ValueType ValueType
Type of underlying integral value.
Definition BitmaskValue.h:142
bool getBitValue(unsigned bitNum) const
Get bit value.
Definition BitmaskValue.h:369
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition BitmaskValue.h:307
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition BitmaskValue.h:388
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition BitmaskValue.h:272
constexpr bool valid() const
Check validity of the field value.
Definition BitmaskValue.h:324
typename BaseImpl::VersionType VersionType
Version type.
Definition BitmaskValue.h:131
bool setVersion(VersionType version)
Default implementation of version update.
Definition BitmaskValue.h:408
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition BitmaskValue.h:283
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition BitmaskValue.h:185
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition BitmaskValue.h:254
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition BitmaskValue.h:206
constexpr bool isBitmaskValue()
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue.
Definition BitmaskValue.h:534
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition BitmaskValue.h:192
void setBitValue(unsigned bitNum, bool val)
Set bit value.
Definition BitmaskValue.h:376
ValueType & value()
Get access to underlying mask value storage.
Definition BitmaskValue.h:220
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition BitmaskValue.h:289
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition BitmaskValue.h:178
tag::Bitmask CommsTag
Tag indicating type of the field.
Definition BitmaskValue.h:137
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:556
~BitmaskValue() noexcept=default
Destructor.
bool hasAnyBitsSet(ValueType mask) const
Check whether any bits from provided mask are set.
Definition BitmaskValue.h:347
bool refresh()
Refresh contents of the field.
Definition BitmaskValue.h:331
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition BitmaskValue.h:147
OptionsBundle ParsedOptions
All the options provided to this class bundled into struct.
Definition BitmaskValue.h:134
BitmaskValue(const ValueType &val)
Constructor.
Definition BitmaskValue.h:155
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition BitmaskValue.h:300
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition BitmaskValue.h:240
BitmaskValue(const BitmaskValue &)=default
Copy constructor.
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition BitmaskValue.h:125
bool operator!=(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition BitmaskValue.h:508
const ValueType & getValue() const
Get value.
Definition BitmaskValue.h:226
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition BitmaskValue.h:247
void setBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:355
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition BitmaskValue.h:171
BitmaskValue()=default
Default constructor.
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition BitmaskValue.h:394
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:545
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition BitmaskValue.h:318
bool operator==(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equality comparison operator.
Definition BitmaskValue.h:495
bool hasAllBitsSet(ValueType mask) const
Check whether all bits from provided mask are set.
Definition BitmaskValue.h:339
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition BitmaskValue.h:128
typename BaseImpl::ValueType ValueType
Type of underlying integral value.
Definition IntValue.h:100
bool setVersion(VersionType version)
Default implementation of version update.
Definition IntValue.h:429
bool valid() const
Check validity of the field value.
Definition IntValue.h:332
int getForcedLength() const
Get forced serialization length.
Definition IntValue.h:448
VersionType getVersion() const
Get version of the field.
Definition IntValue.h:422
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition IntValue.h:312
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition IntValue.h:326
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition IntValue.h:368
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition IntValue.h:319
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition IntValue.h:350
void setForcedLength(int len)
Force serialization length of the field.
Definition IntValue.h:441
void setValue(U &&val)
Set value.
Definition IntValue.h:276
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition IntValue.h:385
const ValueType & value() const
Get access to integral value storage.
Definition IntValue.h:255
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition IntValue.h:403
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition IntValue.h:409
bool refresh()
Refresh the field's value.
Definition IntValue.h:339
const ValueType & getValue() const
Get value.
Definition IntValue.h:268
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:19
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:66