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/FieldOptionsParser.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
110template <typename TFieldBase, typename... TOptions>
111class BitmaskValue : public TFieldBase
112{
113 using BaseImpl = TFieldBase;
114
115 using OptionsBundle = details::FieldOptionsParser<TOptions...>;
116
117 using IntValueType = details::BitmaskUndertlyingTypeT<OptionsBundle>;
118
119 using IntValueField =
120 IntValue<
121 TFieldBase,
122 IntValueType,
123 TOptions...
124 >;
125
126public:
128 using FieldBase = TFieldBase;
129
131 using Endian = typename BaseImpl::Endian;
132
134 using VersionType = typename BaseImpl::VersionType;
135
137 using ParsedOptions = OptionsBundle;
138
140 using CommsTag = tag::Bitmask;
141
146
150 using FieldType = typename ParsedOptions::FieldType;
151
154 BitmaskValue() = default;
155
158 explicit BitmaskValue(const ValueType& val)
159 : m_intValue(val)
160 {
161 }
162
164 BitmaskValue(const BitmaskValue&) = default;
165
167 ~BitmaskValue() noexcept = default;
168
170 BitmaskValue& operator=(const BitmaskValue&) = default;
171
174 static constexpr bool hasFailOnInvalid()
175 {
176 return ParsedOptions::HasFailOnInvalid;
177 }
178
181 static constexpr bool hasIgnoreInvalid()
182 {
183 return ParsedOptions::HasIgnoreInvalid;
184 }
185
188 static constexpr bool hasEmptySerialization()
189 {
190 return ParsedOptions::HasEmptySerialization;
191 }
192
195 static constexpr bool hasFieldType()
196 {
197 return ParsedOptions::HasFieldType;
198 }
199
202 static constexpr bool hasFixedValue()
203 {
204 return ParsedOptions::HasFixedValue;
205 }
206
209 static constexpr bool hasName()
210 {
211 return ParsedOptions::HasName;
212 }
213
216 static constexpr bool hasReadAlign()
217 {
218 return ParsedOptions::HasReadAlign;
219 }
220
223 static constexpr bool hasWritePadAlign()
224 {
225 return ParsedOptions::HasWritePadAlign;
226 }
227
230 const ValueType& value() const
231 {
232 return m_intValue.value();
233 }
234
238 {
239 return m_intValue.value();
240 }
241
243 const ValueType& getValue() const
244 {
245 return m_intValue.getValue();
246 }
247
249 template <typename U>
250 void setValue(U&& val)
251 {
252 m_intValue.setValue(std::forward<U>(val));
253 }
254
257 constexpr std::size_t length() const
258 {
259 return m_intValue.length();
260 }
261
264 static constexpr std::size_t maxLength()
265 {
267 }
268
271 static constexpr std::size_t minLength()
272 {
274 }
275
281 template <typename TIter>
282 ErrorStatus read(TIter& iter, std::size_t size)
283 {
284 return m_intValue.read(iter, size);
285 }
286
289 static constexpr bool hasReadNoStatus()
290 {
291 return BaseImpl::hasReadNoStatus();
292 }
293
299 template <typename TIter>
300 void readNoStatus(TIter& iter)
301 {
302 m_intValue.readNoStatus(iter);
303 }
304
306 bool canWrite() const
307 {
308 return BaseImpl::canWrite();
309 }
310
316 template <typename TIter>
317 ErrorStatus write(TIter& iter, std::size_t size) const
318 {
319 return m_intValue.write(iter, size);
320 }
321
324 static constexpr bool hasWriteNoStatus()
325 {
326 return BaseImpl::hasWriteNoStatus();
327 }
328
334 template <typename TIter>
335 void writeNoStatus(TIter& iter) const
336 {
337 m_intValue.writeNoStatus(iter);
338 }
339
341 constexpr bool valid() const
342 {
343 return m_intValue.valid();
344 }
345
348 bool refresh()
349 {
350 return m_intValue.refresh();
351 }
352
356 bool hasAllBitsSet(ValueType mask) const
357 {
358 return (value() & mask) == mask;
359 }
360
364 bool hasAnyBitsSet(ValueType mask) const
365 {
366 return (value() & mask) != 0;
367 }
368
373 {
374 return (value() & mask) == 0U;
375 }
376
381 {
382 value() |= mask;
383 }
384
389 {
390 value() &= static_cast<ValueType>(~mask);
391 }
392
394 bool getBitValue(unsigned bitNum) const
395 {
396 return hasAllBitsSet(
397 static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum));
398 }
399
401 void setBitValue(unsigned bitNum, bool val)
402 {
403 auto mask = static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum);
404 if (val) {
405 setBits(mask);
406 }
407 else {
408 clearBits(mask);
409 }
410 }
411
413 static constexpr bool isVersionDependent()
414 {
416 }
417
419 static constexpr bool hasNonDefaultRefresh()
420 {
421 return BaseImpl::hasNonDefaultRefresh();
422 }
423
427 {
428 return m_intValue.getVersion();
429 }
430
434 {
435 return m_intValue.setVersion(version);
436 }
437
445 void setForcedLength(int len)
446 {
447 m_intValue.setForcedLength(len);
448 }
449
452 int getForcedLength() const
453 {
454 return m_intValue.getForcedLength();
455 }
456
460 template <typename TBitIdx>
461 static constexpr ValueType bitAsMask(TBitIdx idx)
462 {
463 return static_cast<ValueType>(static_cast<ValueType>(1) << static_cast<unsigned>(idx));
464 }
465
466protected:
467 using BaseImpl::readData;
468 using BaseImpl::writeData;
469
470private:
471
472 static_assert(!ParsedOptions::HasSerOffset,
473 "comms::option::def::NumValueSerOffset option is not applicable to BitmaskValue field");
474 static_assert(!ParsedOptions::HasVarLengthLimits,
475 "comms::option::def::VarLength option is not applicable to BitmaskValue field");
476 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
477 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to BitmaskValue field");
478 static_assert(!ParsedOptions::HasSequenceSizeForcing,
479 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to BitmaskValue field");
480 static_assert(!ParsedOptions::HasSequenceLengthForcing,
481 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to BitmaskValue field");
482 static_assert(!ParsedOptions::HasSequenceFixedSize,
483 "comms::option::def::SequenceFixedSize option is not applicable to BitmaskValue field");
484 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
485 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to BitmaskValue field");
486 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
487 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to BitmaskValue field");
488 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
489 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to BitmaskValue field");
490 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
491 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to BitmaskValue field");
492 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
493 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to BitmaskValue field");
494 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
495 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to BitmaskValue field");
496 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
497 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to BitmaskValue field");
498 static_assert(!ParsedOptions::HasFixedSizeStorage,
499 "comms::option::app::FixedSizeStorage option is not applicable to BitmaskValue field");
500 static_assert(!ParsedOptions::HasCustomStorageType,
501 "comms::option::app::CustomStorageType option is not applicable to BitmaskValue field");
502 static_assert(!ParsedOptions::HasScalingRatio,
503 "comms::option::def::ScalingRatio option is not applicable to BitmaskValue field");
504 static_assert(!ParsedOptions::HasUnits,
505 "comms::option::def::Units option is not applicable to BitmaskValue field");
506 static_assert(!ParsedOptions::HasOrigDataView,
507 "comms::option::app::OrigDataView option is not applicable to BitmaskValue field");
508 static_assert(!ParsedOptions::HasMultiRangeValidation,
509 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to BitmaskValue field");
510 static_assert(!ParsedOptions::HasVersionsRange,
511 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to BitmaskValue field");
512 static_assert(!ParsedOptions::HasInvalidByDefault,
513 "comms::option::def::InvalidByDefault option is not applicable to BitmaskValue field");
514 static_assert(!ParsedOptions::HasMissingOnReadFail,
515 "comms::option::def::MissingOnReadFail option is not applicable to BitmaskValue field");
516 static_assert(!ParsedOptions::HasMissingOnInvalid,
517 "comms::option::def::MissingOnInvalid option is not applicable to BitmaskValue field");
518 IntValueField m_intValue;
519};
520
521// Implementation
522
528template <typename TFieldBase, typename... TOptions>
531 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
532{
533 return field1.value() == field2.value();
534}
535
541template <typename TFieldBase, typename... TOptions>
544 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
545{
546 return field1.value() != field2.value();
547}
548
554template <typename TFieldBase, typename... TOptions>
557 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
558{
559 return field1.value() < field2.value();
560}
561
567template <typename T>
568constexpr bool isBitmaskValue()
569{
570 return std::is_same<typename T::CommsTag, tag::Bitmask>::value;
571}
572
576template <typename TFieldBase, typename... TOptions>
577inline
578BitmaskValue<TFieldBase, TOptions...>&
583
587template <typename TFieldBase, typename... TOptions>
588inline
589const BitmaskValue<TFieldBase, TOptions...>&
594
642#define COMMS_BITMASK_BITS(...) COMMS_DEFINE_ENUM(BitIdx, __VA_ARGS__)
643
735#define COMMS_BITMASK_BITS_ACCESS(...) \
736 COMMS_AS_BITMASK_FUNC { \
737 return comms::field::toFieldBase(*this); \
738 }\
739 COMMS_AS_BITMASK_CONST_FUNC { \
740 return comms::field::toFieldBase(*this); \
741 } \
742 COMMS_DO_BIT_ACC_FUNC(asBitmask(), __VA_ARGS__)
743
753#define COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(...) \
754 COMMS_DO_BIT_ACC_FUNC((*this), __VA_ARGS__)
755
824#define COMMS_BITMASK_BITS_SEQ(...) \
825 COMMS_BITMASK_BITS(__VA_ARGS__) \
826 COMMS_BITMASK_BITS_ACCESS(__VA_ARGS__)
827
838#define COMMS_BITMASK_BITS_SEQ_NOTEMPLATE(...) \
839 COMMS_BITMASK_BITS(__VA_ARGS__) \
840 COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(__VA_ARGS__)
841
893#define COMMS_BITMASK_MASK(...) \
894 COMMS_EXPAND(COMMS_DEFINE_BIT_MASK(__VA_ARGS__)) \
895 COMMS_EXPAND(COMMS_DO_MASK_FUNC(__VA_ARGS__))
896
897} // namespace field
898
899} // namespace comms
900
Contains definition of comms::Field class.
Contains definition of comms::field::IntValue.
Bitmask value field.
Definition BitmaskValue.h:112
const ValueType & value() const
Get access to underlying mask value storage.
Definition BitmaskValue.h:230
static constexpr bool hasWritePadAlign()
Compile time inquiry of whether comms::option::def::WritePadAlign option has been used.
Definition BitmaskValue.h:223
void setForcedLength(int len)
Force serialization length of the field.
Definition BitmaskValue.h:445
void clearBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:388
int getForcedLength() const
Get forced serialization length.
Definition BitmaskValue.h:452
void setValue(U &&val)
Set value.
Definition BitmaskValue.h:250
static constexpr ValueType bitAsMask(TBitIdx idx)
Convert bit index to the mask that can be used with setBits() or clearBits().
Definition BitmaskValue.h:461
VersionType getVersion() const
Get version of the field.
Definition BitmaskValue.h:426
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition BitmaskValue.h:282
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition BitmaskValue.h:202
bool operator<(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition BitmaskValue.h:555
typename IntValueField::ValueType ValueType
Type of underlying integral value.
Definition BitmaskValue.h:145
bool getBitValue(unsigned bitNum) const
Get bit value.
Definition BitmaskValue.h:394
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition BitmaskValue.h:324
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition BitmaskValue.h:413
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition BitmaskValue.h:289
constexpr bool valid() const
Check validity of the field value.
Definition BitmaskValue.h:341
typename BaseImpl::VersionType VersionType
Version type.
Definition BitmaskValue.h:134
bool setVersion(VersionType version)
Default implementation of version update.
Definition BitmaskValue.h:433
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition BitmaskValue.h:300
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition BitmaskValue.h:188
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition BitmaskValue.h:271
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition BitmaskValue.h:209
constexpr bool isBitmaskValue()
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue.
Definition BitmaskValue.h:568
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition BitmaskValue.h:195
void setBitValue(unsigned bitNum, bool val)
Set bit value.
Definition BitmaskValue.h:401
ValueType & value()
Get access to underlying mask value storage.
Definition BitmaskValue.h:237
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition BitmaskValue.h:306
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition BitmaskValue.h:181
tag::Bitmask CommsTag
Tag indicating type of the field.
Definition BitmaskValue.h:140
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:590
~BitmaskValue() noexcept=default
Destructor.
bool hasAnyBitsSet(ValueType mask) const
Check whether any bits from provided mask are set.
Definition BitmaskValue.h:364
bool refresh()
Refresh contents of the field.
Definition BitmaskValue.h:348
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition BitmaskValue.h:150
bool hasAllBitsCleared(ValueType mask) const
Check whether all bits from provided mask are cleared.
Definition BitmaskValue.h:372
OptionsBundle ParsedOptions
All the options provided to this class bundled into struct.
Definition BitmaskValue.h:137
BitmaskValue(const ValueType &val)
Constructor.
Definition BitmaskValue.h:158
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition BitmaskValue.h:317
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition BitmaskValue.h:257
BitmaskValue(const BitmaskValue &)=default
Copy constructor.
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition BitmaskValue.h:128
bool operator!=(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition BitmaskValue.h:542
const ValueType & getValue() const
Get value.
Definition BitmaskValue.h:243
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition BitmaskValue.h:264
void setBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:380
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition BitmaskValue.h:174
BitmaskValue()=default
Default constructor.
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition BitmaskValue.h:419
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:579
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition BitmaskValue.h:335
bool operator==(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equality comparison operator.
Definition BitmaskValue.h:529
bool hasAllBitsSet(ValueType mask) const
Check whether all bits from provided mask are set.
Definition BitmaskValue.h:356
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition BitmaskValue.h:131
static constexpr bool hasReadAlign()
Compile time inquiry of whether comms::option::def::ReadAlign option has been used.
Definition BitmaskValue.h:216
Field that represent integral value.
Definition IntValue.h:82
typename BaseImpl::ValueType ValueType
Definition IntValue.h:103
static constexpr std::size_t maxLength()
Definition IntValue.h:343
static constexpr std::size_t minLength()
Definition IntValue.h:336
static constexpr bool isVersionDependent()
Definition IntValue.h:426
Contains definition of various tag classes.
Namespace that contains definitions of all message fields.
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