13#include "comms/details/macro_common.h"
14#include "comms/details/variant_access.h"
16#include "comms/field/basic/Variant.h"
17#include "comms/field/details/AdaptBasicField.h"
73template <
typename TFieldBase,
typename TMembers,
typename... TOptions>
75 details::AdaptBasicFieldT<
78 details::OptionsParser<TOptions...>::ForcedMembersVersionDependency,
84 details::AdaptBasicFieldT<
87 details::OptionsParser<TOptions...>::ForcedMembersVersionDependency,
93 "TMembers is expected to be a tuple of std::tuple<...>");
96 1U <= std::tuple_size<TMembers>::value,
97 "Number of members is expected to be at least 1.");
104 using Endian =
typename BaseImpl::Endian;
142 : BaseImpl(
std::move(val))
150 return ParsedOptions::HasFailOnInvalid;
157 return ParsedOptions::HasIgnoreInvalid;
164 return ParsedOptions::HasEmptySerialization;
171 return ParsedOptions::HasFieldType;
178 return ParsedOptions::HasFixedValue;
185 return ParsedOptions::HasName;
192 return BaseImpl::value();
199 return BaseImpl::value();
206 return BaseImpl::getValue();
211 template <
typename U>
214 BaseImpl::setValue(std::forward<U>(val));
224 return BaseImpl::length();
231 return BaseImpl::minLength();
238 return BaseImpl::maxLength();
248 template <
typename TIter>
251 return BaseImpl::read(iter, size);
262 template <
typename TIter>
268 return BaseImpl::canWrite();
279 template <
typename TIter>
282 return BaseImpl::write(iter, size);
289 return BaseImpl::hasWriteNoStatus();
297 template <
typename TIter>
300 BaseImpl::writeNoStatus(iter);
307 return BaseImpl::valid();
316 return BaseImpl::refresh();
324 return BaseImpl::currentField();
336 BaseImpl::selectField(idx);
374 template <
typename TFunc>
377 BaseImpl::currentFieldExec(std::forward<TFunc>(func));
397 template <
typename TFunc>
400 BaseImpl::currentFieldExec(std::forward<TFunc>(func));
416 template <std::size_t TIdx,
typename... TArgs>
417 typename std::tuple_element<TIdx, Members>::type&
initField(TArgs&&... args)
419 return BaseImpl::template initField<TIdx>(std::forward<TArgs>(args)...);
429 template <std::
size_t TIdx>
432 BaseImpl::template deinitField<TIdx>();
440 template <std::
size_t TIdx>
443 return BaseImpl::template accessField<TIdx>();
451 template <std::
size_t TIdx>
452 const typename std::tuple_element<TIdx, Members>::type&
accessField()
const
454 return BaseImpl::template accessField<TIdx>();
462 return BaseImpl::currentFieldValid();
475 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
481 return BaseImpl::hasNonDefaultRefresh();
489 return BaseImpl::getVersion();
496 return BaseImpl::setVersion(
version);
500 using BaseImpl::readData;
501 using BaseImpl::writeData;
504 static_assert(!ParsedOptions::HasSerOffset,
505 "comms::option::def::NumValueSerOffset option is not applicable to Variant field");
506 static_assert(!ParsedOptions::HasFixedLengthLimit,
507 "comms::option::def::FixedLength option is not applicable to Variant field");
508 static_assert(!ParsedOptions::HasFixedBitLengthLimit,
509 "comms::option::def::FixedBitLength option is not applicable to Variant field");
510 static_assert(!ParsedOptions::HasVarLengthLimits,
511 "comms::option::def::VarLength option is not applicable to Variant field");
512 static_assert(!ParsedOptions::HasAvailableLengthLimit,
513 "comms::option::def::AvailableLengthLimit option is not applicable to Variant field");
514 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
515 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to Variant field");
516 static_assert(!ParsedOptions::HasSequenceSizeForcing,
517 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to Variant field");
518 static_assert(!ParsedOptions::HasSequenceLengthForcing,
519 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to Variant field");
520 static_assert(!ParsedOptions::HasSequenceFixedSize,
521 "comms::option::def::SequenceFixedSize option is not applicable to Variant field");
522 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
523 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to Variant field");
524 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
525 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to Variant field");
526 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
527 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to Variant field");
528 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
529 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to Variant field");
530 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
531 "comms::option::def::SequenceElemFixedSerLengthFieldPrefix option is not applicable to Variant field");
532 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
533 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to Variant field");
534 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
535 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to Variant field");
536 static_assert(!ParsedOptions::HasFixedSizeStorage,
537 "comms::option::app::FixedSizeStorage option is not applicable to Variant field");
538 static_assert(!ParsedOptions::HasCustomStorageType,
539 "comms::option::app::CustomStorageType option is not applicable to Variant field");
540 static_assert(!ParsedOptions::HasScalingRatio,
541 "comms::option::def::ScalingRatio option is not applicable to Variant field");
542 static_assert(!ParsedOptions::HasUnits,
543 "comms::option::def::Units option is not applicable to Variant field");
544 static_assert(!ParsedOptions::HasOrigDataView,
545 "comms::option::app::OrigDataView option is not applicable to Variant field");
546 static_assert(!ParsedOptions::HasMultiRangeValidation,
547 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to Variant field");
548 static_assert(!ParsedOptions::HasVersionsRange,
549 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to Variant field");
550 static_assert(!ParsedOptions::HasInvalidByDefault,
551 "comms::option::def::InvalidByDefault option is not applicable to Variant field");
552 static_assert(!ParsedOptions::HasMissingOnReadFail,
553 "comms::option::def::MissingOnReadFail option is not applicable to Variant field");
554 static_assert(!ParsedOptions::HasMissingOnInvalid,
555 "comms::option::def::MissingOnInvalid option is not applicable to Variant field");
561template <
typename TVar>
562class VariantEqualityCompHelper
565 VariantEqualityCompHelper(
const TVar& other,
bool& result)
570 template <std::
size_t TIdx,
typename TField>
571 void operator()(
const TField& field)
573 m_result = (field == m_other.template accessField<TIdx>());
582template <
typename TVar>
583VariantEqualityCompHelper<TVar> makeVariantEqualityCompHelper(TVar& other,
bool& result)
585 return VariantEqualityCompHelper<TVar>(other, result);
588template <
typename TVar>
589class VariantLessCompHelper
592 VariantLessCompHelper(
const TVar& other,
bool& result)
597 template <std::
size_t TIdx,
typename TField>
598 void operator()(
const TField& field)
600 m_result = (field < m_other.template accessField<TIdx>());
608template <
typename TVar>
609VariantLessCompHelper<TVar> makeVariantLessCompHelper(TVar& other,
bool& result)
611 return VariantLessCompHelper<TVar>(other, result);
621template <
typename TFieldBase,
typename TMembers,
typename... TOptions>
626 if (&field1 == &field2) {
643 field1.
currentFieldExec(details::makeVariantEqualityCompHelper(field2, result));
652template <
typename TFieldBase,
typename TMembers,
typename... TOptions>
657 return !(field1 == field2);
664template <
typename TFieldBase,
typename TMembers,
typename... TOptions>
685 if (&field1 == &field2) {
690 field1.
currentFieldExec(details::makeVariantLessCompHelper(field2, result));
702 return std::is_same<typename T::CommsTag, tag::Variant>::value;
708template <
typename TFieldBase,
typename TMembers,
typename... TOptions>
710Variant<TFieldBase, TMembers, TOptions...>&
719template <
typename TFieldBase,
typename TMembers,
typename... TOptions>
721const Variant<TFieldBase, TMembers, TOptions...>&
763#define COMMS_VARIANT_MEMBERS_ACCESS(...) \
764 COMMS_EXPAND(COMMS_DEFINE_FIELD_ENUM(__VA_ARGS__)) \
765 COMMS_AS_VARIANT_FUNC { \
766 auto& var = comms::field::toFieldBase(*this); \
767 using Var = typename std::decay<decltype(var)>::type; \
768 static_assert(std::tuple_size<typename Var::Members>::value == FieldIdx_numOfValues, \
769 "Invalid number of names for variant field"); \
772 COMMS_AS_VARIANT_CONST_FUNC { \
773 auto& var = comms::field::toFieldBase(*this); \
774 using Var = typename std::decay<decltype(var)>::type; \
775 static_assert(std::tuple_size<typename Var::Members>::value == FieldIdx_numOfValues, \
776 "Invalid number of names for variant field"); \
779 COMMS_DO_VARIANT_MEM_ACC_FUNC(asVariant(), __VA_ARGS__)
792#define COMMS_VARIANT_MEMBERS_ACCESS_NOTEMPLATE(...) \
793 COMMS_EXPAND(COMMS_DEFINE_FIELD_ENUM(__VA_ARGS__)) \
794 COMMS_DO_VARIANT_MEM_ACC_FUNC_NOTEMPLATE(__VA_ARGS__)
946#define COMMS_VARIANT_MEMBERS_NAMES(...) \
947 COMMS_EXPAND(COMMS_VARIANT_MEMBERS_ACCESS(__VA_ARGS__)) \
948 COMMS_EXPAND(COMMS_DO_FIELD_TYPEDEF(typename Base::Members, Field_, FieldIdx_, __VA_ARGS__))
This file contain definition of error statuses used by comms module.
Contains various tuple type manipulation classes and functions.
Defines a "variant" field, that can contain any of the provided ones.
Definition Variant.h:82
bool setVersion(VersionType version)
Default implementation of version update.
Definition Variant.h:494
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition Variant.h:128
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition Variant.h:479
void deinitField()
Destruct previously initialised (via initField()) contained field.
Definition Variant.h:430
void currentFieldExec(TFunc &&func) const
Execute provided function object with current field as parameter (const variant).
Definition Variant.h:398
void readNoStatus(TIter &iter)=delete
Read operation without error check and status report is not supported.
const std::tuple_element< TIdx, Members >::type & accessField() const
Access already constructed field at specifed index (known at compile time).
Definition Variant.h:452
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition Variant.h:101
const Variant< TFieldBase, TMembers, TOptions... > & toFieldBase(const Variant< TFieldBase, TMembers, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::Variant type in order to have access ...
Definition Variant.h:722
std::tuple_element< TIdx, Members >::type & accessField()
Access already constructed field at specifed index (known at compile time).
Definition Variant.h:441
std::size_t currentField() const
Get index of the current field (within the Members tuple).
Definition Variant.h:322
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition Variant.h:266
void selectField(std::size_t idx)
Select type of the variant field.
Definition Variant.h:334
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition Variant.h:280
bool refresh()
Refresh the field's value.
Definition Variant.h:314
bool valid() const
Check validity of all the contained field.
Definition Variant.h:305
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition Variant.h:287
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition Variant.h:473
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition Variant.h:176
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition Variant.h:148
void currentFieldExec(TFunc &&func)
Execute provided function object with current field as parameter.
Definition Variant.h:375
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition Variant.h:113
std::size_t length() const
Get length required to serialise contained fields.
Definition Variant.h:222
Variant(const ValueType &val)
Constructor.
Definition Variant.h:135
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition Variant.h:162
void setValue(U &&val)
Set value.
Definition Variant.h:212
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition Variant.h:104
Variant()=default
Default constructor.
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise all possible contained fields.
Definition Variant.h:236
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition Variant.h:110
typename BaseImpl::VersionType VersionType
Version type.
Definition Variant.h:107
ValueType & value()
Get access to the internal storage buffer.
Definition Variant.h:190
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition Variant.h:256
bool operator<(const Variant< TFieldBase, TMembers, TOptions... > &field1, const Variant< TFieldBase, TMembers, TOptions... > &field2)
Order comparison operator.
Definition Variant.h:665
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition Variant.h:298
std::tuple_element< TIdx, Members >::type & initField(TArgs &&... args)
Construct and initialise specified contained field in the internal buffer.
Definition Variant.h:417
constexpr bool isVariant()
Compile time check function of whether a provided type is any variant of comms::field::Variant.
Definition Variant.h:700
typename BaseImpl::Members Members
All the supported types.
Definition Variant.h:123
void reset()
Invalidate current state.
Definition Variant.h:467
const ValueType & getValue() const
Get value.
Definition Variant.h:204
Variant(ValueType &&val)
Constructor.
Definition Variant.h:141
const ValueType & value() const
Get access to the internal storage buffer.
Definition Variant.h:197
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition Variant.h:169
bool operator!=(const Variant< TFieldBase, TMembers, TOptions... > &field1, const Variant< TFieldBase, TMembers, TOptions... > &field2)
Non-equality comparison operator.
Definition Variant.h:653
static constexpr std::size_t minLength()
Get minimal length that is required to serialise all possible contained fields.
Definition Variant.h:229
bool operator==(const Variant< TFieldBase, TMembers, TOptions... > &field1, const Variant< TFieldBase, TMembers, TOptions... > &field2)
Equality comparison operator.
Definition Variant.h:622
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition Variant.h:183
typename BaseImpl::ValueType ValueType
Value type.
Definition Variant.h:118
VersionType getVersion() const
Get version of the field.
Definition Variant.h:487
bool currentFieldValid() const
Check whether the field contains a valid instance of other field.
Definition Variant.h:460
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition Variant.h:155
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition Variant.h:249
Variant< TFieldBase, TMembers, TOptions... > & toFieldBase(Variant< TFieldBase, TMembers, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::Variant type in order to have access ...
Definition Variant.h:711
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
Contains definition of all the options used by the COMMS library.
Check whether provided type is a variant of std::tuple.
Definition Tuple.h:36