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 <type_traits>
26#include <utility>
27
28namespace comms
29{
30
31namespace field
32{
33
34namespace details
35{
36
37template <bool THasFixedLength>
38struct BitmaskUndertlyingType;
39
40template <>
41struct BitmaskUndertlyingType<true>
42{
43 template <typename TOptionsBundle>
44 using Type = typename comms::util::SizeToType<TOptionsBundle::FixedLength, false>::Type;
45};
46
47template <>
48struct BitmaskUndertlyingType<false>
49{
50 template <typename TOptionsBundle>
51 using Type = unsigned;
52};
53
54template <typename TOptionsBundle>
55using BitmaskUndertlyingTypeT =
56 typename BitmaskUndertlyingType<TOptionsBundle::HasFixedLengthLimit>::template Type<TOptionsBundle>;
57
58} // namespace details
59
106template <typename TFieldBase, typename... TOptions>
107class BitmaskValue : public TFieldBase
108{
109 using BaseImpl = TFieldBase;
110
111 using OptionsBundle = details::OptionsParser<TOptions...>;
112
113 using IntValueType = details::BitmaskUndertlyingTypeT<OptionsBundle>;
114
115 using IntValueField =
116 IntValue<
117 TFieldBase,
118 IntValueType,
119 TOptions...
120 >;
121
122public:
124 using FieldBase = TFieldBase;
125
127 using Endian = typename BaseImpl::Endian;
128
130 using VersionType = typename BaseImpl::VersionType;
131
133 using ParsedOptions = OptionsBundle;
134
136 using CommsTag = tag::Bitmask;
137
142
146 using FieldType = typename ParsedOptions::FieldType;
147
150 BitmaskValue() = default;
151
154 explicit BitmaskValue(const ValueType& val)
155 : m_intValue(val)
156 {
157 }
158
160 BitmaskValue(const BitmaskValue&) = default;
161
163 ~BitmaskValue() noexcept = default;
164
166 BitmaskValue& operator=(const BitmaskValue&) = default;
167
170 static constexpr bool hasFailOnInvalid()
171 {
172 return ParsedOptions::HasFailOnInvalid;
173 }
174
177 static constexpr bool hasIgnoreInvalid()
178 {
179 return ParsedOptions::HasIgnoreInvalid;
180 }
181
184 static constexpr bool hasEmptySerialization()
185 {
186 return ParsedOptions::HasEmptySerialization;
187 }
188
191 static constexpr bool hasFieldType()
192 {
193 return ParsedOptions::HasFieldType;
194 }
195
198 static constexpr bool hasFixedValue()
199 {
200 return ParsedOptions::HasFixedValue;
201 }
202
205 static constexpr bool hasName()
206 {
207 return ParsedOptions::HasName;
208 }
209
212 const ValueType& value() const
213 {
214 return m_intValue.value();
215 }
216
220 {
221 return m_intValue.value();
222 }
223
225 const ValueType& getValue() const
226 {
227 return m_intValue.getValue();
228 }
229
231 template <typename U>
232 void setValue(U&& val)
233 {
234 m_intValue.setValue(std::forward<U>(val));
235 }
236
239 constexpr std::size_t length() const
240 {
241 return m_intValue.length();
242 }
243
246 static constexpr std::size_t maxLength()
247 {
249 }
250
253 static constexpr std::size_t minLength()
254 {
256 }
257
263 template <typename TIter>
264 ErrorStatus read(TIter& iter, std::size_t size)
265 {
266 return m_intValue.read(iter, size);
267 }
268
271 static constexpr bool hasReadNoStatus()
272 {
273 return BaseImpl::hasReadNoStatus();
274 }
275
281 template <typename TIter>
282 void readNoStatus(TIter& iter)
283 {
284 m_intValue.readNoStatus(iter);
285 }
286
288 bool canWrite() const
289 {
290 return BaseImpl::canWrite();
291 }
292
298 template <typename TIter>
299 ErrorStatus write(TIter& iter, std::size_t size) const
300 {
301 return m_intValue.write(iter, size);
302 }
303
306 static constexpr bool hasWriteNoStatus()
307 {
308 return BaseImpl::hasWriteNoStatus();
309 }
310
316 template <typename TIter>
317 void writeNoStatus(TIter& iter) const
318 {
319 m_intValue.writeNoStatus(iter);
320 }
321
323 constexpr bool valid() const
324 {
325 return m_intValue.valid();
326 }
327
330 bool refresh()
331 {
332 return m_intValue.refresh();
333 }
334
338 bool hasAllBitsSet(ValueType mask) const
339 {
340 return (value() & mask) == mask;
341 }
342
346 bool hasAnyBitsSet(ValueType mask) const
347 {
348 return (value() & mask) != 0;
349 }
350
355 {
356 value() |= mask;
357 }
358
363 {
364 value() &= static_cast<ValueType>(~mask);
365 }
366
368 bool getBitValue(unsigned bitNum) const
369 {
370 return hasAllBitsSet(
371 static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum));
372 }
373
375 void setBitValue(unsigned bitNum, bool val)
376 {
377 auto mask = static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum);
378 if (val) {
379 setBits(mask);
380 }
381 else {
382 clearBits(mask);
383 }
384 }
385
387 static constexpr bool isVersionDependent()
388 {
390 }
391
393 static constexpr bool hasNonDefaultRefresh()
394 {
395 return BaseImpl::hasNonDefaultRefresh();
396 }
397
401 {
402 return m_intValue.getVersion();
403 }
404
408 {
409 return m_intValue.setVersion(version);
410 }
411
419 void setForcedLength(int len)
420 {
421 m_intValue.setForcedLength(len);
422 }
423
426 int getForcedLength() const
427 {
428 return m_intValue.getForcedLength();
429 }
430
431protected:
432 using BaseImpl::readData;
433 using BaseImpl::writeData;
434
435private:
436
437 static_assert(!ParsedOptions::HasSerOffset,
438 "comms::option::def::NumValueSerOffset option is not applicable to BitmaskValue field");
439 static_assert(!ParsedOptions::HasVarLengthLimits,
440 "comms::option::def::VarLength option is not applicable to BitmaskValue field");
441 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
442 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to BitmaskValue field");
443 static_assert(!ParsedOptions::HasSequenceSizeForcing,
444 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to BitmaskValue field");
445 static_assert(!ParsedOptions::HasSequenceLengthForcing,
446 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to BitmaskValue field");
447 static_assert(!ParsedOptions::HasSequenceFixedSize,
448 "comms::option::def::SequenceFixedSize option is not applicable to BitmaskValue field");
449 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
450 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to BitmaskValue field");
451 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
452 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to BitmaskValue field");
453 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
454 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to BitmaskValue field");
455 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
456 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to BitmaskValue field");
457 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
458 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to BitmaskValue field");
459 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
460 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to BitmaskValue field");
461 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
462 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to BitmaskValue field");
463 static_assert(!ParsedOptions::HasFixedSizeStorage,
464 "comms::option::app::FixedSizeStorage option is not applicable to BitmaskValue field");
465 static_assert(!ParsedOptions::HasCustomStorageType,
466 "comms::option::app::CustomStorageType option is not applicable to BitmaskValue field");
467 static_assert(!ParsedOptions::HasScalingRatio,
468 "comms::option::def::ScalingRatio option is not applicable to BitmaskValue field");
469 static_assert(!ParsedOptions::HasUnits,
470 "comms::option::def::Units option is not applicable to BitmaskValue field");
471 static_assert(!ParsedOptions::HasOrigDataView,
472 "comms::option::app::OrigDataView option is not applicable to BitmaskValue field");
473 static_assert(!ParsedOptions::HasMultiRangeValidation,
474 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to BitmaskValue field");
475 static_assert(!ParsedOptions::HasVersionsRange,
476 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to BitmaskValue field");
477 static_assert(!ParsedOptions::HasInvalidByDefault,
478 "comms::option::def::InvalidByDefault option is not applicable to BitmaskValue field");
479 static_assert(!ParsedOptions::HasMissingOnReadFail,
480 "comms::option::def::MissingOnReadFail option is not applicable to BitmaskValue field");
481 static_assert(!ParsedOptions::HasMissingOnInvalid,
482 "comms::option::def::MissingOnInvalid option is not applicable to BitmaskValue field");
483 IntValueField m_intValue;
484};
485
486// Implementation
487
493template <typename TFieldBase, typename... TOptions>
496 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
497{
498 return field1.value() == field2.value();
499}
500
506template <typename TFieldBase, typename... TOptions>
509 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
510{
511 return field1.value() != field2.value();
512}
513
519template <typename TFieldBase, typename... TOptions>
522 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
523{
524 return field1.value() < field2.value();
525}
526
532template <typename T>
533constexpr bool isBitmaskValue()
534{
535 return std::is_same<typename T::CommsTag, tag::Bitmask>::value;
536}
537
541template <typename TFieldBase, typename... TOptions>
542inline
543BitmaskValue<TFieldBase, TOptions...>&
545{
546 return field;
547}
548
552template <typename TFieldBase, typename... TOptions>
553inline
554const BitmaskValue<TFieldBase, TOptions...>&
556{
557 return field;
558}
559
608#define COMMS_BITMASK_BITS(...) COMMS_DEFINE_ENUM(BitIdx, __VA_ARGS__)
609
702#define COMMS_BITMASK_BITS_ACCESS(...) \
703 COMMS_AS_BITMASK_FUNC { \
704 return comms::field::toFieldBase(*this); \
705 }\
706 COMMS_AS_BITMASK_CONST_FUNC { \
707 return comms::field::toFieldBase(*this); \
708 } \
709 COMMS_DO_BIT_ACC_FUNC(asBitmask(), __VA_ARGS__)
710
721#define COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(...) \
722 COMMS_DO_BIT_ACC_FUNC((*this), __VA_ARGS__)
723
793#define COMMS_BITMASK_BITS_SEQ(...) \
794 COMMS_BITMASK_BITS(__VA_ARGS__) \
795 COMMS_BITMASK_BITS_ACCESS(__VA_ARGS__)
796
808#define COMMS_BITMASK_BITS_SEQ_NOTEMPLATE(...) \
809 COMMS_BITMASK_BITS(__VA_ARGS__) \
810 COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(__VA_ARGS__)
811
812} // namespace field
813
814} // namespace comms
815
Contains definition of comms::Field class.
Contains definition of comms::field::IntValue.
Bitmask value field.
Definition BitmaskValue.h:108
const ValueType & value() const
Get access to underlying mask value storage.
Definition BitmaskValue.h:212
void setForcedLength(int len)
Force serialization length of the field.
Definition BitmaskValue.h:419
void clearBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:362
int getForcedLength() const
Get forced serialization length.
Definition BitmaskValue.h:426
void setValue(U &&val)
Set value.
Definition BitmaskValue.h:232
VersionType getVersion() const
Get version of the field.
Definition BitmaskValue.h:400
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition BitmaskValue.h:264
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition BitmaskValue.h:198
bool operator<(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition BitmaskValue.h:520
typename IntValueField::ValueType ValueType
Type of underlying integral value.
Definition BitmaskValue.h:141
bool getBitValue(unsigned bitNum) const
Get bit value.
Definition BitmaskValue.h:368
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition BitmaskValue.h:306
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition BitmaskValue.h:387
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition BitmaskValue.h:271
constexpr bool valid() const
Check validity of the field value.
Definition BitmaskValue.h:323
typename BaseImpl::VersionType VersionType
Version type.
Definition BitmaskValue.h:130
bool setVersion(VersionType version)
Default implementation of version update.
Definition BitmaskValue.h:407
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition BitmaskValue.h:282
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition BitmaskValue.h:184
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition BitmaskValue.h:253
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition BitmaskValue.h:205
constexpr bool isBitmaskValue()
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue.
Definition BitmaskValue.h:533
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition BitmaskValue.h:191
void setBitValue(unsigned bitNum, bool val)
Set bit value.
Definition BitmaskValue.h:375
ValueType & value()
Get access to underlying mask value storage.
Definition BitmaskValue.h:219
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition BitmaskValue.h:288
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition BitmaskValue.h:177
tag::Bitmask CommsTag
Tag indicating type of the field.
Definition BitmaskValue.h:136
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:555
~BitmaskValue() noexcept=default
Destructor.
bool hasAnyBitsSet(ValueType mask) const
Check whether any bits from provided mask are set.
Definition BitmaskValue.h:346
bool refresh()
Refresh contents of the field.
Definition BitmaskValue.h:330
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition BitmaskValue.h:146
OptionsBundle ParsedOptions
All the options provided to this class bundled into struct.
Definition BitmaskValue.h:133
BitmaskValue(const ValueType &val)
Constructor.
Definition BitmaskValue.h:154
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition BitmaskValue.h:299
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition BitmaskValue.h:239
BitmaskValue(const BitmaskValue &)=default
Copy constructor.
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition BitmaskValue.h:124
bool operator!=(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition BitmaskValue.h:507
const ValueType & getValue() const
Get value.
Definition BitmaskValue.h:225
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition BitmaskValue.h:246
void setBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:354
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition BitmaskValue.h:170
BitmaskValue()=default
Default constructor.
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition BitmaskValue.h:393
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:544
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition BitmaskValue.h:317
bool operator==(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equality comparison operator.
Definition BitmaskValue.h:494
bool hasAllBitsSet(ValueType mask) const
Check whether all bits from provided mask are set.
Definition BitmaskValue.h:338
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition BitmaskValue.h:127
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