#include "comms/field/Bitfield.h"
template<typename TFieldBase, typename TMembers, typename... TOptions>
class comms::field::Bitfield< TFieldBase, TMembers, TOptions >
Bitfield field.
Sometimes one or several bytes can be logically split into two or more independent values, which are packed together to save some space. For example, one enum type that has only 4 possible values, i.e only two bits are needed to encode such value. It would be a waste to allocate full byte for it. Instead, it is packed with some other, say unsigned counter that requires up to 6 bits to encode its valid range of values. The following code defines such field:
enum class MyEnumType : std::uint8_t
{
Value1,
Value2,
Value3,
Value4
};
using MyField =
MyFieldBase,
std::tuple<
MyFieldBase,
MyEnumType,
>,
MyFieldBase,
std::uint8_t,
>
>
>;
Base class to all the field classes.
Definition Field.h:33
Bitfield field.
Definition Bitfield.h:98
Enumerator value field.
Definition EnumValue.h:73
Field that represent integral value.
Definition IntValue.h:72
Option used to specify number of bits that is used for field serialisation when a field is a member o...
Definition options.h:310
Note, that bitfield members fields specify their length in bits using comms::option::def::FixedBitLength option. Also note, that all bitfield member's lengths in bits combined create a round number of bytes, i.e all the bits must sum up to 8, 16, 24, 32, ... bits.
Refer to Bitfield Fields for tutorial and usage examples.
- Template Parameters
-
TFieldBase | Base class for this field, expected to be a variant of comms::Field. |
TMembers | All member fields bundled together in std::tuple. |
TOptions | Zero or more options that modify/refine default behaviour of the field.
Supported options are:
|
- Precondition
- TMember is a variant of std::tuple, that contains other fields.
-
Every field member specifies its length in bits using comms::option::def::FixedBitLength option.
- See also
- COMMS_FIELD_MEMBERS_NAMES()
-
COMMS_FIELD_MEMBERS_ACCESS()
-
COMMS_FIELD_MEMBERS_ACCESS_NOTEMPLATE()
-
COMMS_FIELD_ALIAS()
|
using | CommsTag = typename BaseImpl::CommsTag |
| 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 = details::OptionsParser< TOptions... > |
| All the options provided to this class bundled into struct.
|
|
using | ValueType = typename BaseImpl::ValueType |
| Value type.
|
|
using | VersionType = typename BaseImpl::VersionType |
| Version type.
|
|
|
| Bitfield ()=default |
| Default constructor.
|
|
| Bitfield (const ValueType &val) |
| Constructor.
|
|
| Bitfield (ValueType &&val) |
| Constructor.
|
|
bool | canWrite () const |
| Check of whether the field has a consistent value for writing.
|
|
const ValueType & | getValue () const |
| Get value.
|
|
VersionType | getVersion () const |
| Get version of the field.
|
|
constexpr std::size_t | length () const |
| Get length required to serialise the current field value.
|
|
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 the field's contents.
|
|
template<typename U > |
void | setValue (U &&val) |
| Set value.
|
|
bool | setVersion (VersionType version) |
| Default implementation of version update.
|
|
bool | valid () const |
| Check validity of the field value.
|
|
ValueType & | value () |
| Get access to the stored tuple of fields.
|
|
const ValueType & | value () const |
| Get access to the stored tuple of fields.
|
|
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 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.
|
|
template<std::size_t TIdx> |
static constexpr std::size_t | memberBitLength () |
| Retrieve number of bits specified member field consumes.
|
|
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.
|
|
|
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.
|
|
|
(Note that these are not member symbols.)
|
template<typename T > |
constexpr bool | isBitfield () |
| Compile time check function of whether a provided type is any variant of comms::field::Bitfield.
|
|
template<typename TFieldBase , typename TMembers , typename... TOptions> |
bool | operator!= (const Bitfield< TFieldBase, TMembers, TOptions... > &field1, const Bitfield< TFieldBase, TMembers, TOptions... > &field2) noexcept |
| Non-equality comparison operator.
|
|
template<typename TFieldBase , typename TMembers , typename... TOptions> |
bool | operator< (const Bitfield< TFieldBase, TMembers, TOptions... > &field1, const Bitfield< TFieldBase, TMembers, TOptions... > &field2) noexcept |
| Equivalence comparison operator.
|
|
template<typename TFieldBase , typename TMembers , typename... TOptions> |
bool | operator== (const Bitfield< TFieldBase, TMembers, TOptions... > &field1, const Bitfield< TFieldBase, TMembers, TOptions... > &field2) noexcept |
| Equality comparison operator.
|
|
template<typename TFieldBase , typename TMembers , typename... TOptions> |
Bitfield< TFieldBase, TMembers, TOptions... > & | toFieldBase (Bitfield< TFieldBase, TMembers, TOptions... > &field) |
| Upcast type of the field definition to its parent comms::field::Bitfield type in order to have access to its internal types.
|
|
template<typename TFieldBase , typename TMembers , typename... TOptions> |
const Bitfield< TFieldBase, TMembers, TOptions... > & | toFieldBase (const Bitfield< TFieldBase, TMembers, TOptions... > &field) |
| Upcast type of the field definition to its parent comms::field::Bitfield type in order to have access to its internal types.
|
|
◆ FieldType
template<typename TFieldBase , typename TMembers , typename... TOptions>
◆ ValueType
template<typename TFieldBase , typename TMembers , typename... TOptions>
Value type.
Same as TMemebers template argument, i.e. it is std::tuple of all the member fields.
◆ Bitfield() [1/3]
template<typename TFieldBase , typename TMembers , typename... TOptions>
Default constructor.
All field members are initialised using their default constructors.
◆ Bitfield() [2/3]
template<typename TFieldBase , typename TMembers , typename... TOptions>
Constructor.
- Parameters
-
[in] | val | Value of the field to initialise it with. |
◆ Bitfield() [3/3]
template<typename TFieldBase , typename TMembers , typename... TOptions>
Constructor.
- Parameters
-
[in] | val | Value of the field to initialise it with. |
◆ canWrite()
template<typename... TOptions>
static constexpr bool comms::Field< TOptions >::canWrite |
( |
| ) |
|
|
staticconstexprinherited |
Default check of whether the field has a consistent value for writing.
- Returns
- Always true.
◆ getValue()
template<typename TFieldBase , typename TMembers , typename... TOptions>
Get value.
Implemented by calling value(), but can be overriden in the derived class
◆ getVersion()
template<typename TFieldBase , typename TMembers , typename... TOptions>
◆ hasVarLength()
template<typename... TOptions>
static constexpr bool comms::Field< TOptions >::hasVarLength |
( |
| ) |
|
|
staticconstexprinherited |
◆ length()
template<typename TFieldBase , typename TMembers , typename... TOptions>
Get length required to serialise the current field value.
- Returns
- Number of bytes it will take to serialise the field value.
◆ maxLength()
template<typename TFieldBase , typename TMembers , typename... TOptions>
Get maximal length that is required to serialise field of this type.
- Returns
- Maximal number of bytes required serialise the field value.
◆ memberBitLength()
template<typename TFieldBase , typename TMembers , typename... TOptions>
template<std::size_t TIdx>
Retrieve number of bits specified member field consumes.
- Template Parameters
-
TIdx | Index of the member field. |
- Returns
- Number of bits, specified with comms::option::def::FixedBitLength option used with the requested member.
◆ minLength()
template<typename TFieldBase , typename TMembers , typename... TOptions>
Get minimal length that is required to serialise field of this type.
- Returns
- Minimal number of bytes required serialise the field value.
◆ read()
template<typename TFieldBase , typename TMembers , typename... TOptions>
template<typename TIter >
Read field value from input data sequence.
- Parameters
-
[in,out] | iter | Iterator to read the data. |
[in] | size | Number of bytes available for reading. |
- Returns
- Status of read operation.
- Postcondition
- Iterator is advanced.
◆ readData() [1/2]
template<typename... TOptions>
template<typename T , typename TIter >
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.
- Template Parameters
-
T | Return type |
TIter | Type of input iterator |
- Parameters
-
[in,out] | iter | Input iterator. |
- Returns
- The integral type value.
- Precondition
- TSize <= sizeof(T)
-
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
- Postcondition
- The iterator is advanced.
- Note
- Thread safety: Safe for distinct stream buffers, unsafe otherwise.
◆ readData() [2/2]
template<typename... TOptions>
template<typename T , std::size_t TSize, typename TIter >
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.
- Template Parameters
-
T | Return type |
TSize | number of bytes to read |
TIter | Type of input iterator |
- Parameters
-
[in,out] | iter | Input iterator. |
- Returns
- The integral type value.
- Precondition
- TSize <= sizeof(T)
-
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
- Postcondition
- The internal pointer of the stream buffer is advanced.
- Note
- Thread safety: Safe for distinct stream buffers, unsafe otherwise.
◆ readNoStatus()
template<typename TFieldBase , typename TMembers , typename... TOptions>
template<typename TIter >
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.
- Parameters
-
[in,out] | iter | Iterator to read the data. |
- Postcondition
- Iterator is advanced.
◆ refresh()
template<typename TFieldBase , typename TMembers , typename... TOptions>
Refresh the field's contents.
Calls refresh() member function on every member field, will return true if any of the calls returns true.
◆ setValue()
template<typename TFieldBase , typename TMembers , typename... TOptions>
template<typename U >
Set value.
Implemented as re-assigning to value(), but can be overriden in the derived class.
◆ setVersion()
template<typename TFieldBase , typename TMembers , typename... TOptions>
Default implementation of version update.
- Returns
- true in case the field contents have changed, false otherwise
◆ valid()
template<typename... TOptions>
Default validity check.
Always returns true, can be overriden by the derived class
- Returns
- Always true
◆ value() [1/2]
template<typename TFieldBase , typename TMembers , typename... TOptions>
Get access to the stored tuple of fields.
- Returns
- Reference to the underlying stored value.
◆ value() [2/2]
template<typename TFieldBase , typename TMembers , typename... TOptions>
Get access to the stored tuple of fields.
- Returns
- Const reference to the underlying stored value.
◆ write()
template<typename TFieldBase , typename TMembers , typename... TOptions>
template<typename TIter >
Write current field value to output data sequence.
- Parameters
-
[in,out] | iter | Iterator to write the data. |
[in] | size | Maximal number of bytes that can be written. |
- Returns
- Status of write operation.
- Postcondition
- Iterator is advanced.
◆ writeData() [1/2]
template<typename... TOptions>
template<typename T , typename TIter >
static void comms::Field< TOptions >::writeData |
( |
T |
value, |
|
|
TIter & |
iter |
|
) |
| |
|
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.
- Template Parameters
-
T | Type of the value to write. Must be integral. |
Type | of output iterator |
- Parameters
-
[in] | value | Integral type value to be written. |
[in,out] | iter | Output iterator. |
- Precondition
- The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
- Postcondition
- The iterator is advanced.
- Note
- Thread safety: Safe for distinct buffers, unsafe otherwise.
◆ writeData() [2/2]
template<typename... TOptions>
template<std::size_t TSize, typename T , typename TIter >
static void comms::Field< TOptions >::writeData |
( |
T |
value, |
|
|
TIter & |
iter |
|
) |
| |
|
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.
- Template Parameters
-
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 |
- Parameters
-
[in] | value | Integral type value to be written. |
[in,out] | iter | Output iterator. |
- Precondition
- TSize <= sizeof(T)
-
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
- Postcondition
- The iterator is advanced.
- Note
- Thread safety: Safe for distinct buffers, unsafe otherwise.
◆ writeNoStatus()
template<typename TFieldBase , typename TMembers , typename... TOptions>
template<typename TIter >
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.
- Parameters
-
[in,out] | iter | Iterator to write the data. |
- Postcondition
- Iterator is advanced.
◆ isBitfield()
template<typename T >
constexpr bool isBitfield |
( |
| ) |
|
|
related |
Compile time check function of whether a provided type is any variant of comms::field::Bitfield.
- Template Parameters
-
- Returns
- true in case provided type is any variant of Bitfield
◆ operator!=()
template<typename TFieldBase , typename TMembers , typename... TOptions>
bool operator!= |
( |
const Bitfield< TFieldBase, TMembers, TOptions... > & |
field1, |
|
|
const Bitfield< TFieldBase, TMembers, TOptions... > & |
field2 |
|
) |
| |
|
related |
Non-equality comparison operator.
- Parameters
-
[in] | field1 | First field. |
[in] | field2 | Second field. |
- Returns
- true in case fields are NOT equal, false otherwise.
◆ operator<()
template<typename TFieldBase , typename TMembers , typename... TOptions>
bool operator< |
( |
const Bitfield< TFieldBase, TMembers, TOptions... > & |
field1, |
|
|
const Bitfield< TFieldBase, TMembers, TOptions... > & |
field2 |
|
) |
| |
|
related |
Equivalence comparison operator.
- Parameters
-
[in] | field1 | First field. |
[in] | field2 | Second field. |
- Returns
- true in case value of the first field is lower than than the value of the second.
◆ operator==()
template<typename TFieldBase , typename TMembers , typename... TOptions>
bool operator== |
( |
const Bitfield< TFieldBase, TMembers, TOptions... > & |
field1, |
|
|
const Bitfield< TFieldBase, TMembers, TOptions... > & |
field2 |
|
) |
| |
|
related |
Equality comparison operator.
- Parameters
-
[in] | field1 | First field. |
[in] | field2 | Second field. |
- Returns
- true in case fields are equal, false otherwise.
The documentation for this class was generated from the following file: