COMMS
Template library intended to help with implementation of communication protocols.
|
#include "comms/field/BitmaskValue.h"
Bitmask value field.
Quite often communication protocols specify bitmask values, where any bit has a specific meaning. Although such masks are can be handled as unsigned integer values using comms::field::IntValue field type, using comms::field::Bitmask may be a bit more convenient.
Public Types | |
using | CommsTag = tag::Bitmask |
Tag indicating type of the field. | |
using | Endian = typename BaseImpl::Endian |
Endian used for serialisation. | |
using | FieldBase = TFieldBase |
Base class provided in the first template parameter. | |
using | FieldType = typename ParsedOptions::FieldType |
Type of actual extending field specified via comms::option::def::FieldType. | |
using | ParsedOptions = OptionsBundle |
All the options provided to this class bundled into struct. | |
using | ValueType = typename IntValueField::ValueType |
Type of underlying integral value. | |
using | VersionType = typename BaseImpl::VersionType |
Version type. | |
Public Member Functions | |
BitmaskValue ()=default | |
Default constructor. | |
BitmaskValue (const BitmaskValue &)=default | |
Copy constructor. | |
BitmaskValue (const ValueType &val) | |
Constructor. | |
~BitmaskValue () noexcept=default | |
Destructor. | |
bool | canWrite () const |
Check of whether the field has a consistent value for writing. | |
void | clearBits (ValueType mask) |
Set all the provided bits. | |
bool | getBitValue (unsigned bitNum) const |
Get bit value. | |
int | getForcedLength () const |
Get forced serialization length. | |
const ValueType & | getValue () const |
Get value. | |
VersionType | getVersion () const |
Get version of the field. | |
bool | hasAllBitsSet (ValueType mask) const |
Check whether all bits from provided mask are set. | |
bool | hasAnyBitsSet (ValueType mask) const |
Check whether any bits from provided mask are set. | |
constexpr std::size_t | length () const |
Get length required to serialise the current field value. | |
BitmaskValue & | operator= (const BitmaskValue &)=default |
Copy assignment. | |
template<typename TIter > | |
ErrorStatus | read (TIter &iter, std::size_t size) |
Read field value from input data sequence. | |
template<typename TIter > | |
void | readNoStatus (TIter &iter) |
Read field value from input data sequence without error check and status report. | |
bool | refresh () |
Refresh contents of the field. | |
void | setBits (ValueType mask) |
Set all the provided bits. | |
void | setBitValue (unsigned bitNum, bool val) |
Set bit value. | |
void | setForcedLength (int len) |
Force serialization length of the field. | |
template<typename U > | |
void | setValue (U &&val) |
Set value. | |
bool | setVersion (VersionType version) |
Default implementation of version update. | |
constexpr bool | valid () const |
Check validity of the field value. | |
ValueType & | value () |
Get access to underlying mask value storage. | |
const ValueType & | value () const |
Get access to underlying mask value storage. | |
template<typename TIter > | |
ErrorStatus | write (TIter &iter, std::size_t size) const |
Write current field value to output data sequence. | |
template<typename TIter > | |
void | writeNoStatus (TIter &iter) const |
Write current field value to output data sequence without error check and status report. | |
Static Public Member Functions | |
static constexpr bool | canWrite () |
Default check of whether the field has a consistent value for writing. | |
static constexpr bool | hasEmptySerialization () |
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used. | |
static constexpr bool | hasFailOnInvalid () |
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used. | |
static constexpr bool | hasFieldType () |
Compile time inquiry of whether comms::option::def::FieldType option has been used. | |
static constexpr bool | hasIgnoreInvalid () |
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used. | |
static constexpr bool | hasNonDefaultRefresh () |
Compile time check if this class has non-default refresh functionality. | |
static constexpr bool | hasReadNoStatus () |
Compile time check of whether the field has proper readNoStatus() member function. | |
static constexpr bool | hasVarLength () |
Default check of whether the field has variable length definition via comms::option::def::VarLength option. | |
static constexpr bool | hasWriteNoStatus () |
Compile time check of whether the field has proper writeNoStatus() member function. | |
static constexpr bool | isVersionDependent () |
Compile time check if this class is version dependent. | |
static constexpr std::size_t | maxLength () |
Get maximal length that is required to serialise field of this type. | |
static constexpr std::size_t | minLength () |
Get minimal length that is required to serialise field of this type. | |
static constexpr bool | valid () |
Default validity check. | |
Static Protected Member Functions | |
template<typename T , typename TIter > | |
static T | readData (TIter &iter) |
Read data from input buffer. | |
template<typename T , std::size_t TSize, typename TIter > | |
static T | readData (TIter &iter) |
Read partial data from input buffer. | |
template<typename T , typename TIter > | |
static void | writeData (T value, TIter &iter) |
Write data into the output buffer. | |
template<std::size_t TSize, typename T , typename TIter > | |
static void | writeData (T value, TIter &iter) |
Write partial data into the output buffer. | |
Related Symbols | |
(Note that these are not member symbols.) | |
#define | COMMS_BITMASK_BITS(...) COMMS_DEFINE_ENUM(BitIdx, __VA_ARGS__) |
Provide names for bits in comms::field::BitmaskValue field. | |
#define | COMMS_BITMASK_BITS_ACCESS(...) |
Generate access functions for bits in comms::field::BitmaskValue field. | |
#define | COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE(...) COMMS_DO_BIT_ACC_FUNC((*this), __VA_ARGS__) |
Similar to COMMS_BITMASK_BITS_ACCESS(), but dedicated for non-template classes. | |
#define | COMMS_BITMASK_BITS_SEQ(...) |
Combine usage of COMMS_BITMASK_BITS() and COMMS_BITMASK_BITS_ACCESS(). | |
#define | COMMS_BITMASK_BITS_SEQ_NOTEMPLATE(...) |
Similar to COMMS_BITMASK_BITS_SEQ(), but dedicated for non-template classes. | |
template<typename T > | |
constexpr bool | isBitmaskValue () |
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue. | |
template<typename TFieldBase , typename... TOptions> | |
bool | operator!= (const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept |
Non-equality comparison operator. | |
template<typename TFieldBase , typename... TOptions> | |
bool | operator< (const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept |
Equivalence comparison operator. | |
template<typename TFieldBase , typename... TOptions> | |
bool | operator== (const BitmaskValue< TFieldBase, TOptions... > &field1, const BitmaskValue< TFieldBase, TOptions... > &field2) noexcept |
Equality comparison operator. | |
template<typename TFieldBase , typename... TOptions> | |
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 access to its internal types. | |
template<typename TFieldBase , typename... TOptions> | |
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 access to its internal types. | |
using comms::field::BitmaskValue< TFieldBase, TOptions >::FieldType = typename ParsedOptions::FieldType |
Type of actual extending field specified via comms::option::def::FieldType.
void if comms::option::def::FieldType hasn't been applied.
using comms::field::BitmaskValue< TFieldBase, TOptions >::ValueType = typename IntValueField::ValueType |
Type of underlying integral value.
Unsigned integral type, which depends on the length of the mask determined by the comms::option::def::FixedLength option.
|
default |
Default constructor.
Initial bitmask has all bits cleared (equals 0)
|
explicit |
Constructor.
[in] | val | Value of the field to initialise it with. |
|
staticconstexprinherited |
Default check of whether the field has a consistent value for writing.
void comms::field::BitmaskValue< TFieldBase, TOptions >::clearBits | ( | ValueType | mask | ) |
Set all the provided bits.
Equivalent to
[in] | mask | Mask of bits to clear. |
int comms::field::BitmaskValue< TFieldBase, TOptions >::getForcedLength | ( | ) | const |
Get forced serialization length.
VersionType comms::field::BitmaskValue< TFieldBase, TOptions >::getVersion | ( | ) | const |
Get version of the field.
Exists only if comms::option::def::VersionStorage option has been provided.
bool comms::field::BitmaskValue< TFieldBase, TOptions >::hasAllBitsSet | ( | ValueType | mask | ) | const |
Check whether all bits from provided mask are set.
[in] | mask | Mask to check against |
bool comms::field::BitmaskValue< TFieldBase, TOptions >::hasAnyBitsSet | ( | ValueType | mask | ) | const |
Check whether any bits from provided mask are set.
[in] | mask | Mask to check against |
|
staticconstexprinherited |
Default check of whether the field has variable length definition via comms::option::def::VarLength option.
|
constexpr |
Get length required to serialise the current field value.
|
staticconstexpr |
Get maximal length that is required to serialise field of this type.
|
staticconstexpr |
Get minimal length that is required to serialise field of this type.
ErrorStatus comms::field::BitmaskValue< TFieldBase, TOptions >::read | ( | TIter & | iter, |
std::size_t | size | ||
) |
Read field value from input data sequence.
[in,out] | iter | Iterator to read the data. |
[in] | size | Number of bytes available for reading. |
|
staticprotectedinherited |
Read data from input buffer.
Use this function to read data from the intput buffer maintained by the caller. The endianness of the data will be as specified in options of the class.
T | Return type |
TIter | Type of input iterator |
[in,out] | iter | Input iterator. |
|
staticprotectedinherited |
Read partial data from input buffer.
Use this function to read data from the intput buffer maintained by the caller. The endianness of the data will be as specified in options of the class.
T | Return type |
TSize | number of bytes to read |
TIter | Type of input iterator |
[in,out] | iter | Input iterator. |
void comms::field::BitmaskValue< TFieldBase, TOptions >::readNoStatus | ( | TIter & | iter | ) |
Read field value from input data sequence without error check and status report.
Similar to read(), but doesn't perform any correctness checks and doesn't report any failures.
[in,out] | iter | Iterator to read the data. |
bool comms::field::BitmaskValue< TFieldBase, TOptions >::refresh | ( | ) |
Refresh contents of the field.
void comms::field::BitmaskValue< TFieldBase, TOptions >::setBits | ( | ValueType | mask | ) |
void comms::field::BitmaskValue< TFieldBase, TOptions >::setForcedLength | ( | int | len | ) |
Force serialization length of the field.
Available only when comms::option::def::AvailableLengthLimit option is used for field definition.
[in] | len | Forced serialization length.
|
bool comms::field::BitmaskValue< TFieldBase, TOptions >::setVersion | ( | VersionType | version | ) |
Default implementation of version update.
|
staticconstexprinherited |
Default validity check.
Always returns true, can be overriden by the derived class
ValueType & comms::field::BitmaskValue< TFieldBase, TOptions >::value | ( | ) |
Get access to underlying mask value storage.
const ValueType & comms::field::BitmaskValue< TFieldBase, TOptions >::value | ( | ) | const |
Get access to underlying mask value storage.
ErrorStatus comms::field::BitmaskValue< TFieldBase, TOptions >::write | ( | TIter & | iter, |
std::size_t | size | ||
) | const |
Write current field value to output data sequence.
[in,out] | iter | Iterator to write the data. |
[in] | size | Maximal number of bytes that can be written. |
|
staticprotectedinherited |
Write data into the output buffer.
Use this function to write data to the the buffer maintained by the caller. The endianness of the data will be as specified in the options provided to the class.
T | Type of the value to write. Must be integral. |
Type | of output iterator |
[in] | value | Integral type value to be written. |
[in,out] | iter | Output iterator. |
|
staticprotectedinherited |
Write partial data into the output buffer.
Use this function to write partial data to the buffer maintained by the caller. The endianness of the data will be as specified the class options.
TSize | Length of the value in bytes known in compile time. |
T | Type of the value to write. Must be integral. |
TIter | Type of output iterator |
[in] | value | Integral type value to be written. |
[in,out] | iter | Output iterator. |
void comms::field::BitmaskValue< TFieldBase, TOptions >::writeNoStatus | ( | TIter & | iter | ) | const |
Write current field value to output data sequence without error check and status report.
Similar to write(), but doesn't perform any correctness checks and doesn't report any failures.
[in,out] | iter | Iterator to write the data. |
|
related |
Provide names for bits in comms::field::BitmaskValue field.
Defines BitIdx enum with all the provided values prefixed with "BitIdx_". For example usage of
will generate the following enum type:
NOTE, that provided names first, second, third, and fourth have found their way to the enum BitIdx.
Also note, that there is automatically added BitIdx_numOfValues value to the end of BitIdx enum.
It is possible to assign values to the provided names. It could be useful when skipping some unused bits. For example
will generate the following enum type:
The macro COMMS_BITMASK_BITS() should be used inside definition of the bitmask field to provide names for the bits for external use:
|
related |
Generate access functions for bits in comms::field::BitmaskValue field.
The COMMS_BITMASK_BITS() macro defines BitIdx enum to be able to access internal bits. However, an ability to provide values to the enumeration values using =val suffixes doesn't allow generation of convenience access functions to the bits. That's why COMMS_BITMASK_BITS_ACCESS() macro was introduced. For every name listed in the parameters list, getBitValue_*() and setBitValue_*() functions will be generated. For example, having the following definition
is equivalent to having following functions defined:
NOTE, that generated getBitValue_*() and setBitValue_*() functions use BitIdx_* enum values generated by COMMS_BITMASK_BITS(). It means that COMMS_BITMASK_BITS_ACCESS() macro can NOT be used without COMMS_BITMASK_BITS().
|
related |
Similar to COMMS_BITMASK_BITS_ACCESS(), but dedicated for non-template classes.
The COMMS_BITMASK_BITS_ACCESS() macro is a generic one, which can be used in any class (template, or non-template). However, some compilers (such as g++-4.9 and below, clang-4.0 and below) may fail to compile it even though it uses valid C++11 constructs. If the compilation fails and the class it is being used in is NOT a template one, please use COMMS_BITMASK_BITS_ACCESS_NOTEMPLATE() instead.
|
related |
Combine usage of COMMS_BITMASK_BITS() and COMMS_BITMASK_BITS_ACCESS().
When assigned bit names start at bit 0 and go sequentially without any gaps in the middle, i.e. don't have any =val suffixes, then use COMMS_BITMASK_BITS_SEQ() macro to name the bits. It is defined to use COMMS_BITMASK_BITS() and COMMS_BITMASK_BITS_ACCESS() with the same bit names. For example
is equivalent to having
|
related |
Similar to COMMS_BITMASK_BITS_SEQ(), but dedicated for non-template classes.
The COMMS_BITMASK_BITS_SEQ() macro is a generic one, which can be used in any class (template, or non-template). However, some compilers (such as g++-4.9 and below, clang-4.0 and below) may fail to compile it even though it uses valid C++11 constructs. If the compilation fails and the class it is being used in is NOT a template one, please use COMMS_BITMASK_BITS_SEQ_NOTEMPLATE() instead.
|
related |
Compile time check function of whether a provided type is any variant of comms::field::BitmaskValue.
T | Any type. |
|
related |
Non-equality comparison operator.
[in] | field1 | First field. |
[in] | field2 | Second field. |
|
related |
Equivalence comparison operator.
[in] | field1 | First field. |
[in] | field2 | Second field. |
|
related |
Equality comparison operator.
[in] | field1 | First field. |
[in] | field2 | Second field. |