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 "comms/details/bits_access.h"
14#include "comms/details/gen_enum.h"
15#include "comms/Field.h"
16#include "comms/field/details/AdaptBasicField.h"
17#include "comms/field/details/OptionsParser.h"
19#include "comms/field/tag.h"
20#include "comms/util/SizeToType.h"
21
22#include <limits>
23#include <utility>
24
25namespace comms
26{
27
28namespace field
29{
30
31namespace details
32{
33
34template <bool THasFixedLength>
35struct BitmaskUndertlyingType;
36
37template <>
38struct BitmaskUndertlyingType<true>
39{
40 template <typename TOptionsBundle>
41 using Type = typename comms::util::SizeToType<TOptionsBundle::FixedLength, false>::Type;
42};
43
44template <>
45struct BitmaskUndertlyingType<false>
46{
47 template <typename TOptionsBundle>
48 using Type = unsigned;
49};
50
51template <typename TOptionsBundle>
52using BitmaskUndertlyingTypeT =
53 typename BitmaskUndertlyingType<TOptionsBundle::HasFixedLengthLimit>::template Type<TOptionsBundle>;
54
55
56} // namespace details
57
104template <typename TFieldBase, typename... TOptions>
105class BitmaskValue : public TFieldBase
106{
107 using BaseImpl = TFieldBase;
108
109 using OptionsBundle = details::OptionsParser<TOptions...>;
110
111 using IntValueType = details::BitmaskUndertlyingTypeT<OptionsBundle>;
112
113 using IntValueField =
114 IntValue<
115 TFieldBase,
116 IntValueType,
117 TOptions...
118 >;
119
120public:
122 using FieldBase = TFieldBase;
123
125 using Endian = typename BaseImpl::Endian;
126
128 using VersionType = typename BaseImpl::VersionType;
129
131 using ParsedOptions = OptionsBundle;
132
134 using CommsTag = tag::Bitmask;
135
140
144 using FieldType = typename ParsedOptions::FieldType;
145
148 BitmaskValue() = default;
149
152 explicit BitmaskValue(const ValueType& val)
153 : m_intValue(val)
154 {
155 }
156
158 BitmaskValue(const BitmaskValue&) = default;
159
161 ~BitmaskValue() noexcept = default;
162
164 BitmaskValue& operator=(const BitmaskValue&) = default;
165
168 static constexpr bool hasFailOnInvalid()
169 {
170 return ParsedOptions::HasFailOnInvalid;
171 }
172
175 static constexpr bool hasIgnoreInvalid()
176 {
177 return ParsedOptions::HasIgnoreInvalid;
178 }
179
182 static constexpr bool hasEmptySerialization()
183 {
184 return ParsedOptions::HasEmptySerialization;
185 }
186
189 static constexpr bool hasFieldType()
190 {
191 return ParsedOptions::HasFieldType;
192 }
193
196 static constexpr bool hasFixedValue()
197 {
198 return ParsedOptions::HasFixedValue;
199 }
200
203 static constexpr bool hasName()
204 {
205 return ParsedOptions::HasName;
206 }
207
210 const ValueType& value() const
211 {
212 return m_intValue.value();
213 }
214
218 {
219 return m_intValue.value();
220 }
221
223 const ValueType& getValue() const
224 {
225 return m_intValue.getValue();
226 }
227
229 template <typename U>
230 void setValue(U&& val)
231 {
232 m_intValue.setValue(std::forward<U>(val));
233 }
234
237 constexpr std::size_t length() const
238 {
239 return m_intValue.length();
240 }
241
244 static constexpr std::size_t maxLength()
245 {
247 }
248
251 static constexpr std::size_t minLength()
252 {
254 }
255
261 template <typename TIter>
262 ErrorStatus read(TIter& iter, std::size_t size)
263 {
264 return m_intValue.read(iter, size);
265 }
266
269 static constexpr bool hasReadNoStatus()
270 {
271 return BaseImpl::hasReadNoStatus();
272 }
273
279 template <typename TIter>
280 void readNoStatus(TIter& iter)
281 {
282 m_intValue.readNoStatus(iter);
283 }
284
286 bool canWrite() const
287 {
288 return BaseImpl::canWrite();
289 }
290
296 template <typename TIter>
297 ErrorStatus write(TIter& iter, std::size_t size) const
298 {
299 return m_intValue.write(iter, size);
300 }
301
304 static constexpr bool hasWriteNoStatus()
305 {
306 return BaseImpl::hasWriteNoStatus();
307 }
308
314 template <typename TIter>
315 void writeNoStatus(TIter& iter) const
316 {
317 m_intValue.writeNoStatus(iter);
318 }
319
321 constexpr bool valid() const
322 {
323 return m_intValue.valid();
324 }
325
328 bool refresh()
329 {
330 return m_intValue.refresh();
331 }
332
336 bool hasAllBitsSet(ValueType mask) const
337 {
338 return (value() & mask) == mask;
339 }
340
344 bool hasAnyBitsSet(ValueType mask) const
345 {
346 return (value() & mask) != 0;
347 }
348
353 {
354 value() |= mask;
355 }
356
361 {
362 value() &= static_cast<ValueType>(~mask);
363 }
364
366 bool getBitValue(unsigned bitNum) const
367 {
368 return hasAllBitsSet(
369 static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum));
370 }
371
373 void setBitValue(unsigned bitNum, bool val)
374 {
375 auto mask = static_cast<ValueType>(static_cast<ValueType>(1U) << bitNum);
376 if (val) {
377 setBits(mask);
378 }
379 else {
380 clearBits(mask);
381 }
382 }
383
385 static constexpr bool isVersionDependent()
386 {
388 }
389
391 static constexpr bool hasNonDefaultRefresh()
392 {
393 return BaseImpl::hasNonDefaultRefresh();
394 }
395
399 {
400 return m_intValue.getVersion();
401 }
402
406 {
407 return m_intValue.setVersion(version);
408 }
409
417 void setForcedLength(int len)
418 {
419 m_intValue.setForcedLength(len);
420 }
421
424 int getForcedLength() const
425 {
426 return m_intValue.getForcedLength();
427 }
428
429protected:
430 using BaseImpl::readData;
431 using BaseImpl::writeData;
432
433private:
434
435 static_assert(!ParsedOptions::HasSerOffset,
436 "comms::option::def::NumValueSerOffset option is not applicable to BitmaskValue field");
437 static_assert(!ParsedOptions::HasVarLengthLimits,
438 "comms::option::def::VarLength option is not applicable to BitmaskValue field");
439 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
440 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to BitmaskValue field");
441 static_assert(!ParsedOptions::HasSequenceSizeForcing,
442 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to BitmaskValue field");
443 static_assert(!ParsedOptions::HasSequenceLengthForcing,
444 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to BitmaskValue field");
445 static_assert(!ParsedOptions::HasSequenceFixedSize,
446 "comms::option::def::SequenceFixedSize option is not applicable to BitmaskValue field");
447 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
448 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to BitmaskValue field");
449 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
450 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to BitmaskValue field");
451 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
452 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to BitmaskValue field");
453 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
454 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to BitmaskValue field");
455 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
456 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to BitmaskValue field");
457 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
458 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to BitmaskValue field");
459 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
460 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to BitmaskValue field");
461 static_assert(!ParsedOptions::HasFixedSizeStorage,
462 "comms::option::app::FixedSizeStorage option is not applicable to BitmaskValue field");
463 static_assert(!ParsedOptions::HasCustomStorageType,
464 "comms::option::app::CustomStorageType option is not applicable to BitmaskValue field");
465 static_assert(!ParsedOptions::HasScalingRatio,
466 "comms::option::def::ScalingRatio option is not applicable to BitmaskValue field");
467 static_assert(!ParsedOptions::HasUnits,
468 "comms::option::def::Units option is not applicable to BitmaskValue field");
469 static_assert(!ParsedOptions::HasOrigDataView,
470 "comms::option::app::OrigDataView option is not applicable to BitmaskValue field");
471 static_assert(!ParsedOptions::HasMultiRangeValidation,
472 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to BitmaskValue field");
473 static_assert(!ParsedOptions::HasVersionsRange,
474 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to BitmaskValue field");
475 static_assert(!ParsedOptions::HasInvalidByDefault,
476 "comms::option::def::InvalidByDefault option is not applicable to BitmaskValue field");
477 static_assert(!ParsedOptions::HasMissingOnReadFail,
478 "comms::option::def::MissingOnReadFail option is not applicable to BitmaskValue field");
479 static_assert(!ParsedOptions::HasMissingOnInvalid,
480 "comms::option::def::MissingOnInvalid option is not applicable to BitmaskValue field");
481 IntValueField m_intValue;
482};
483
484// Implementation
485
491template <typename TFieldBase, typename... TOptions>
494 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
495{
496 return field1.value() == field2.value();
497}
498
504template <typename TFieldBase, typename... TOptions>
507 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
508{
509 return field1.value() != field2.value();
510}
511
517template <typename TFieldBase, typename... TOptions>
520 const BitmaskValue<TFieldBase, TOptions...>& field2) noexcept
521{
522 return field1.value() < field2.value();
523}
524
530template <typename T>
531constexpr bool isBitmaskValue()
532{
533 return std::is_same<typename T::CommsTag, tag::Bitmask>::value;
534}
535
539template <typename TFieldBase, typename... TOptions>
540inline
541BitmaskValue<TFieldBase, TOptions...>&
543{
544 return field;
545}
546
550template <typename TFieldBase, typename... TOptions>
551inline
552const BitmaskValue<TFieldBase, TOptions...>&
554{
555 return field;
556}
557
558
607#define COMMS_BITMASK_BITS(...) COMMS_DEFINE_ENUM(BitIdx, __VA_ARGS__)
608
701#define COMMS_BITMASK_BITS_ACCESS(...) \
702 COMMS_AS_BITMASK_FUNC { \
703 return comms::field::toFieldBase(*this); \
704 }\
705 COMMS_AS_BITMASK_CONST_FUNC { \
706 return comms::field::toFieldBase(*this); \
707 } \
708 COMMS_DO_BIT_ACC_FUNC(asBitmask(), __VA_ARGS__)
709
720#define COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(...) \
721 COMMS_DO_BIT_ACC_FUNC((*this), __VA_ARGS__)
722
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
813} // namespace field
814
815} // namespace comms
816
817
Contains definition of comms::Field class.
Contains definition of comms::field::IntValue.
Bitmask value field.
Definition BitmaskValue.h:106
const ValueType & value() const
Get access to underlying mask value storage.
Definition BitmaskValue.h:210
void setForcedLength(int len)
Force serialization length of the field.
Definition BitmaskValue.h:417
void clearBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:360
int getForcedLength() const
Get forced serialization length.
Definition BitmaskValue.h:424
void setValue(U &&val)
Set value.
Definition BitmaskValue.h:230
VersionType getVersion() const
Get version of the field.
Definition BitmaskValue.h:398
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition BitmaskValue.h:262
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition BitmaskValue.h:196
bool operator<(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition BitmaskValue.h:518
typename IntValueField::ValueType ValueType
Type of underlying integral value.
Definition BitmaskValue.h:139
bool getBitValue(unsigned bitNum) const
Get bit value.
Definition BitmaskValue.h:366
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition BitmaskValue.h:304
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition BitmaskValue.h:385
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition BitmaskValue.h:269
constexpr bool valid() const
Check validity of the field value.
Definition BitmaskValue.h:321
typename BaseImpl::VersionType VersionType
Version type.
Definition BitmaskValue.h:128
bool setVersion(VersionType version)
Default implementation of version update.
Definition BitmaskValue.h:405
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition BitmaskValue.h:280
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition BitmaskValue.h:182
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition BitmaskValue.h:251
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition BitmaskValue.h:203
constexpr bool isBitmaskValue()
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue.
Definition BitmaskValue.h:531
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition BitmaskValue.h:189
void setBitValue(unsigned bitNum, bool val)
Set bit value.
Definition BitmaskValue.h:373
ValueType & value()
Get access to underlying mask value storage.
Definition BitmaskValue.h:217
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition BitmaskValue.h:286
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition BitmaskValue.h:175
tag::Bitmask CommsTag
Tag indicating type of the field.
Definition BitmaskValue.h:134
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:553
~BitmaskValue() noexcept=default
Destructor.
bool hasAnyBitsSet(ValueType mask) const
Check whether any bits from provided mask are set.
Definition BitmaskValue.h:344
bool refresh()
Refresh contents of the field.
Definition BitmaskValue.h:328
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition BitmaskValue.h:144
OptionsBundle ParsedOptions
All the options provided to this class bundled into struct.
Definition BitmaskValue.h:131
BitmaskValue(const ValueType &val)
Constructor.
Definition BitmaskValue.h:152
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition BitmaskValue.h:297
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition BitmaskValue.h:237
BitmaskValue(const BitmaskValue &)=default
Copy constructor.
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition BitmaskValue.h:122
bool operator!=(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition BitmaskValue.h:505
const ValueType & getValue() const
Get value.
Definition BitmaskValue.h:223
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition BitmaskValue.h:244
void setBits(ValueType mask)
Set all the provided bits.
Definition BitmaskValue.h:352
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition BitmaskValue.h:168
BitmaskValue()=default
Default constructor.
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition BitmaskValue.h:391
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:542
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition BitmaskValue.h:315
bool operator==(const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept
Equality comparison operator.
Definition BitmaskValue.h:492
bool hasAllBitsSet(ValueType mask) const
Check whether all bits from provided mask are set.
Definition BitmaskValue.h:336
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition BitmaskValue.h:125
typename BaseImpl::ValueType ValueType
Type of underlying integral value.
Definition IntValue.h:97
bool setVersion(VersionType version)
Default implementation of version update.
Definition IntValue.h:426
bool valid() const
Check validity of the field value.
Definition IntValue.h:329
int getForcedLength() const
Get forced serialization length.
Definition IntValue.h:445
VersionType getVersion() const
Get version of the field.
Definition IntValue.h:419
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition IntValue.h:309
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition IntValue.h:323
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition IntValue.h:365
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition IntValue.h:316
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition IntValue.h:347
void setForcedLength(int len)
Force serialization length of the field.
Definition IntValue.h:438
void setValue(U &&val)
Set value.
Definition IntValue.h:273
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition IntValue.h:382
const ValueType & value() const
Get access to integral value storage.
Definition IntValue.h:252
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition IntValue.h:400
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition IntValue.h:406
bool refresh()
Refresh the field's value.
Definition IntValue.h:336
const ValueType & getValue() const
Get value.
Definition IntValue.h:265
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