COMMS
Template library intended to help with implementation of communication protocols.
Variant.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 - 2024 (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/ErrorStatus.h"
14 #include "comms/options.h"
15 #include "basic/Variant.h"
16 #include "details/AdaptBasicField.h"
17 #include "comms/details/macro_common.h"
18 #include "comms/details/variant_access.h"
19 
20 namespace comms
21 {
22 
23 namespace field
24 {
25 
70 template <typename TFieldBase, typename TMembers, typename... TOptions>
71 class Variant : public
72  details::AdaptBasicFieldT<
73  basic::Variant<
74  TFieldBase,
75  details::OptionsParser<TOptions...>::ForcedMembersVersionDependency,
76  TMembers
77  >,
78  TOptions...>
79 {
80  using BaseImpl =
81  details::AdaptBasicFieldT<
82  basic::Variant<
83  TFieldBase,
84  details::OptionsParser<TOptions...>::ForcedMembersVersionDependency,
85  TMembers
86  >,
87  TOptions...>;
88 
90  "TMembers is expected to be a tuple of std::tuple<...>");
91 
92  static_assert(
93  1U <= std::tuple_size<TMembers>::value,
94  "Number of members is expected to be at least 1.");
95 
96 public:
98  using FieldBase = TFieldBase;
99 
101  using Endian = typename BaseImpl::Endian;
102 
105 
107  using ParsedOptions = details::OptionsParser<TOptions...>;
108 
110  using CommsTag = typename BaseImpl::CommsTag;
111 
115  using ValueType = typename BaseImpl::ValueType;
116 
120  using Members = typename BaseImpl::Members;
121 
126 
129  Variant() = default;
130 
132  explicit Variant(const ValueType& val)
133  : BaseImpl(val)
134  {
135  }
136 
138  explicit Variant(ValueType&& val)
139  : BaseImpl(std::move(val))
140  {
141  }
142 
145  static constexpr bool hasFailOnInvalid()
146  {
147  return ParsedOptions::HasFailOnInvalid;
148  }
149 
152  static constexpr bool hasIgnoreInvalid()
153  {
154  return ParsedOptions::HasIgnoreInvalid;
155  }
156 
159  static constexpr bool hasEmptySerialization()
160  {
161  return ParsedOptions::HasEmptySerialization;
162  }
163 
166  static constexpr bool hasFieldType()
167  {
168  return ParsedOptions::HasFieldType;
169  }
170 
174  {
175  return BaseImpl::value();
176  }
177 
180  const ValueType& value() const
181  {
182  return BaseImpl::value();
183  }
184 
187  const ValueType& getValue() const
188  {
189  return BaseImpl::getValue();
190  }
191 
194  template <typename U>
195  void setValue(U&& val)
196  {
197  BaseImpl::setValue(std::forward<U>(val));
198  }
199 
205  std::size_t length() const
206  {
207  return BaseImpl::length();
208  }
209 
212  static constexpr std::size_t minLength()
213  {
214  return BaseImpl::minLength();
215  }
216 
219  static constexpr std::size_t maxLength()
220  {
221  return BaseImpl::maxLength();
222  }
223 
231  template <typename TIter>
232  ErrorStatus read(TIter& iter, std::size_t size)
233  {
234  return BaseImpl::read(iter, size);
235  }
236 
239  static constexpr bool hasReadNoStatus()
240  {
241  return false;
242  }
243 
245  template <typename TIter>
246  void readNoStatus(TIter& iter) = delete;
247 
249  bool canWrite() const
250  {
251  return BaseImpl::canWrite();
252  }
253 
262  template <typename TIter>
263  ErrorStatus write(TIter& iter, std::size_t size) const
264  {
265  return BaseImpl::write(iter, size);
266  }
267 
270  static constexpr bool hasWriteNoStatus()
271  {
272  return BaseImpl::hasWriteNoStatus();
273  }
274 
280  template <typename TIter>
281  void writeNoStatus(TIter& iter) const
282  {
283  BaseImpl::writeNoStatus(iter);
284  }
285 
288  bool valid() const
289  {
290  return BaseImpl::valid();
291  }
292 
297  bool refresh()
298  {
299  return BaseImpl::refresh();
300  }
301 
305  std::size_t currentField() const
306  {
307  return BaseImpl::currentField();
308  }
309 
317  void selectField(std::size_t idx)
318  {
319  BaseImpl::selectField(idx);
320  }
321 
357  template <typename TFunc>
358  void currentFieldExec(TFunc&& func)
359  {
360  BaseImpl::currentFieldExec(std::forward<TFunc>(func));
361  }
362 
380  template <typename TFunc>
381  void currentFieldExec(TFunc&& func) const
382  {
383  BaseImpl::currentFieldExec(std::forward<TFunc>(func));
384  }
385 
399  template <std::size_t TIdx, typename... TArgs>
400  typename std::tuple_element<TIdx, Members>::type& initField(TArgs&&... args)
401  {
402  return BaseImpl::template initField<TIdx>(std::forward<TArgs>(args)...);
403  }
404 
414  template <std::size_t TIdx>
415  void deinitField()
416  {
417  BaseImpl::template deinitField<TIdx>();
418  }
419 
425  template <std::size_t TIdx>
426  typename std::tuple_element<TIdx, Members>::type& accessField()
427  {
428  return BaseImpl::template accessField<TIdx>();
429  }
430 
436  template <std::size_t TIdx>
437  const typename std::tuple_element<TIdx, Members>::type& accessField() const
438  {
439  return BaseImpl::template accessField<TIdx>();
440  }
441 
445  bool currentFieldValid() const
446  {
447  return BaseImpl::currentFieldValid();
448  }
449 
452  void reset()
453  {
454  BaseImpl::reset();
455  }
456 
458  static constexpr bool isVersionDependent()
459  {
460  return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
461  }
462 
464  static constexpr bool hasNonDefaultRefresh()
465  {
466  return BaseImpl::hasNonDefaultRefresh();
467  }
468 
473  {
474  return BaseImpl::getVersion();
475  }
476 
480  {
481  return BaseImpl::setVersion(version);
482  }
483 
484 protected:
485  using BaseImpl::readData;
486  using BaseImpl::writeData;
487 
488 private:
489  static_assert(!ParsedOptions::HasSerOffset,
490  "comms::option::def::NumValueSerOffset option is not applicable to Variant field");
491  static_assert(!ParsedOptions::HasFixedLengthLimit,
492  "comms::option::def::FixedLength option is not applicable to Variant field");
493  static_assert(!ParsedOptions::HasFixedBitLengthLimit,
494  "comms::option::def::FixedBitLength option is not applicable to Variant field");
495  static_assert(!ParsedOptions::HasVarLengthLimits,
496  "comms::option::def::VarLength option is not applicable to Variant field");
497  static_assert(!ParsedOptions::HasAvailableLengthLimit,
498  "comms::option::def::AvailableLengthLimit option is not applicable to Variant field");
499  static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
500  "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to Variant field");
501  static_assert(!ParsedOptions::HasSequenceSizeForcing,
502  "comms::option::def::SequenceSizeForcingEnabled option is not applicable to Variant field");
503  static_assert(!ParsedOptions::HasSequenceLengthForcing,
504  "comms::option::def::SequenceLengthForcingEnabled option is not applicable to Variant field");
505  static_assert(!ParsedOptions::HasSequenceFixedSize,
506  "comms::option::def::SequenceFixedSize option is not applicable to Variant field");
507  static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
508  "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to Variant field");
509  static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
510  "comms::option::def::SequenceSizeFieldPrefix option is not applicable to Variant field");
511  static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
512  "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to Variant field");
513  static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
514  "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to Variant field");
515  static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
516  "comms::option::def::SequenceElemFixedSerLengthFieldPrefix option is not applicable to Variant field");
517  static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
518  "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to Variant field");
519  static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
520  "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to Variant field");
521  static_assert(!ParsedOptions::HasFixedSizeStorage,
522  "comms::option::app::FixedSizeStorage option is not applicable to Variant field");
523  static_assert(!ParsedOptions::HasCustomStorageType,
524  "comms::option::app::CustomStorageType option is not applicable to Variant field");
525  static_assert(!ParsedOptions::HasScalingRatio,
526  "comms::option::def::ScalingRatio option is not applicable to Variant field");
527  static_assert(!ParsedOptions::HasUnits,
528  "comms::option::def::Units option is not applicable to Variant field");
529  static_assert(!ParsedOptions::HasOrigDataView,
530  "comms::option::app::OrigDataView option is not applicable to Variant field");
531  static_assert(!ParsedOptions::HasMultiRangeValidation,
532  "comms::option::def::ValidNumValueRange (or similar) option is not applicable to Variant field");
533  static_assert(!ParsedOptions::HasVersionsRange,
534  "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to Variant field");
535  static_assert(!ParsedOptions::HasInvalidByDefault,
536  "comms::option::def::InvalidByDefault option is not applicable to Variant field");
537  static_assert(!ParsedOptions::HasMissingOnReadFail,
538  "comms::option::def::MissingOnReadFail option is not applicable to Variant field");
539  static_assert(!ParsedOptions::HasMissingOnInvalid,
540  "comms::option::def::MissingOnInvalid option is not applicable to Variant field");
541 };
542 
543 namespace details
544 {
545 
546 template <typename TVar>
547 class VariantEqualityCompHelper
548 {
549 public:
550  VariantEqualityCompHelper(const TVar& other, bool& result)
551  : other_(other),
552  result_(result)
553  {}
554 
555  template <std::size_t TIdx, typename TField>
556  void operator()(const TField& field)
557  {
558  result_ = (field == other_.template accessField<TIdx>());
559  }
560 
561 private:
562  const TVar& other_;
563  bool& result_;
564 
565 };
566 
567 template <typename TVar>
568 VariantEqualityCompHelper<TVar> makeVariantEqualityCompHelper(TVar& other, bool& result)
569 {
570  return VariantEqualityCompHelper<TVar>(other, result);
571 }
572 
573 template <typename TVar>
574 class VariantLessCompHelper
575 {
576 public:
577  VariantLessCompHelper(const TVar& other, bool& result)
578  : other_(other),
579  result_(result)
580  {}
581 
582  template <std::size_t TIdx, typename TField>
583  void operator()(const TField& field)
584  {
585  result_ = (field < other_.template accessField<TIdx>());
586  }
587 
588 private:
589  const TVar& other_;
590  bool& result_;
591 };
592 
593 template <typename TVar>
594 VariantLessCompHelper<TVar> makeVariantLessCompHelper(TVar& other, bool& result)
595 {
596  return VariantLessCompHelper<TVar>(other, result);
597 }
598 
599 } // namespace details
600 
606 template <typename TFieldBase, typename TMembers, typename... TOptions>
610 {
611  if (&field1 == &field2) {
612  return true;
613  }
614 
615  if (field1.currentFieldValid() != field2.currentFieldValid()) {
616  return false;
617  }
618 
619  if (!field1.currentFieldValid()) {
620  return true;
621  }
622 
623  if (field1.currentField() != field2.currentField()) {
624  return false;
625  }
626 
627  bool result = false;
628  field1.currentFieldExec(details::makeVariantEqualityCompHelper(field2, result));
629  return result;
630 }
631 
637 template <typename TFieldBase, typename TMembers, typename... TOptions>
641 {
642  return !(field1 == field2);
643 }
644 
649 template <typename TFieldBase, typename TMembers, typename... TOptions>
653 {
654  if (!field1.currentFieldValid()) {
655  return (!field2.currentFieldValid());
656  }
657 
658  if (!field2.currentFieldValid()) {
659  return false;
660  }
661 
662  if (field1.currentField() < field2.currentField()) {
663  return true;
664  }
665 
666  if (field1.currentField() != field2.currentField()) {
667  return false;
668  }
669 
670  if (&field1 == &field2) {
671  return false;
672  }
673 
674  bool result = false;
675  field1.currentFieldExec(details::makeVariantLessCompHelper(field2, result));
676  return result;
677 }
678 
684 template <typename T>
685 constexpr bool isVariant()
686 {
687  return std::is_same<typename T::CommsTag, tag::Variant>::value;
688 }
689 
693 template <typename TFieldBase, typename TMembers, typename... TOptions>
694 inline
695 Variant<TFieldBase, TMembers, TOptions...>&
697 {
698  return field;
699 }
700 
704 template <typename TFieldBase, typename TMembers, typename... TOptions>
705 inline
706 const Variant<TFieldBase, TMembers, TOptions...>&
708 {
709  return field;
710 }
711 
748 #define COMMS_VARIANT_MEMBERS_ACCESS(...) \
749  COMMS_EXPAND(COMMS_DEFINE_FIELD_ENUM(__VA_ARGS__)) \
750  COMMS_AS_VARIANT_FUNC { \
751  auto& var = comms::field::toFieldBase(*this); \
752  using Var = typename std::decay<decltype(var)>::type; \
753  static_assert(std::tuple_size<typename Var::Members>::value == FieldIdx_numOfValues, \
754  "Invalid number of names for variant field"); \
755  return var; \
756  }\
757  COMMS_AS_VARIANT_CONST_FUNC { \
758  auto& var = comms::field::toFieldBase(*this); \
759  using Var = typename std::decay<decltype(var)>::type; \
760  static_assert(std::tuple_size<typename Var::Members>::value == FieldIdx_numOfValues, \
761  "Invalid number of names for variant field"); \
762  return var; \
763  } \
764  COMMS_DO_VARIANT_MEM_ACC_FUNC(asVariant(), __VA_ARGS__)
765 
766 
777 #define COMMS_VARIANT_MEMBERS_ACCESS_NOTEMPLATE(...) \
778  COMMS_EXPAND(COMMS_DEFINE_FIELD_ENUM(__VA_ARGS__)) \
779  COMMS_DO_VARIANT_MEM_ACC_FUNC_NOTEMPLATE(__VA_ARGS__)
780 
931 #define COMMS_VARIANT_MEMBERS_NAMES(...) \
932  COMMS_EXPAND(COMMS_VARIANT_MEMBERS_ACCESS(__VA_ARGS__)) \
933  COMMS_EXPAND(COMMS_DO_FIELD_TYPEDEF(typename Base::Members, Field_, FieldIdx_, __VA_ARGS__))
934 
935 
936 } // namespace field
937 
938 } // namespace comms
939 
940 
This file contain definition of error statuses used by comms module.
Defines a "variant" field, that can contain any of the provided ones.
Definition: Variant.h:79
bool setVersion(VersionType version)
Default implementation of version update.
Definition: Variant.h:479
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition: Variant.h:125
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition: Variant.h:464
void deinitField()
Destruct previously initialised (via initField()) contained field.
Definition: Variant.h:415
void currentFieldExec(TFunc &&func) const
Execute provided function object with current field as parameter (const variant).
Definition: Variant.h:381
void readNoStatus(TIter &iter)=delete
Read operation without error check and status report is not supported.
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition: Variant.h:98
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:707
std::size_t currentField() const
Get index of the current field (within the Members tuple).
Definition: Variant.h:305
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition: Variant.h:249
void selectField(std::size_t idx)
Select type of the variant field.
Definition: Variant.h:317
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition: Variant.h:263
bool refresh()
Refresh the field's value.
Definition: Variant.h:297
const std::tuple_element< TIdx, Members >::type & accessField() const
Access already constructed field at specifed index (known at compile time).
Definition: Variant.h:437
bool valid() const
Check validity of all the contained field.
Definition: Variant.h:288
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition: Variant.h:270
std::tuple_element< TIdx, Members >::type & accessField()
Access already constructed field at specifed index (known at compile time).
Definition: Variant.h:426
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition: Variant.h:458
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition: Variant.h:145
void currentFieldExec(TFunc &&func)
Execute provided function object with current field as parameter.
Definition: Variant.h:358
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition: Variant.h:110
std::tuple_element< TIdx, Members >::type & initField(TArgs &&... args)
Construct and initialise specified contained field in the internal buffer.
Definition: Variant.h:400
std::size_t length() const
Get length required to serialise contained fields.
Definition: Variant.h:205
Variant(const ValueType &val)
Constructor.
Definition: Variant.h:132
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition: Variant.h:159
void setValue(U &&val)
Set value.
Definition: Variant.h:195
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition: Variant.h:101
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:219
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition: Variant.h:107
typename BaseImpl::VersionType VersionType
Version type.
Definition: Variant.h:104
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition: Variant.h:239
bool operator<(const Variant< TFieldBase, TMembers, TOptions... > &field1, const Variant< TFieldBase, TMembers, TOptions... > &field2)
Order comparison operator.
Definition: Variant.h:650
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition: Variant.h:281
constexpr bool isVariant()
Compile time check function of whether a provided type is any variant of comms::field::Variant.
Definition: Variant.h:685
typename BaseImpl::Members Members
All the supported types.
Definition: Variant.h:120
void reset()
Invalidate current state.
Definition: Variant.h:452
Variant(ValueType &&val)
Constructor.
Definition: Variant.h:138
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition: Variant.h:166
bool operator!=(const Variant< TFieldBase, TMembers, TOptions... > &field1, const Variant< TFieldBase, TMembers, TOptions... > &field2)
Non-equality comparison operator.
Definition: Variant.h:638
static constexpr std::size_t minLength()
Get minimal length that is required to serialise all possible contained fields.
Definition: Variant.h:212
bool operator==(const Variant< TFieldBase, TMembers, TOptions... > &field1, const Variant< TFieldBase, TMembers, TOptions... > &field2)
Equality comparison operator.
Definition: Variant.h:607
typename BaseImpl::ValueType ValueType
Value type.
Definition: Variant.h:115
VersionType getVersion() const
Get version of the field.
Definition: Variant.h:472
bool currentFieldValid() const
Check whether the field contains a valid instance of other field.
Definition: Variant.h:445
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition: Variant.h:152
const ValueType & value() const
Get access to the internal storage buffer.
Definition: Variant.h:180
const ValueType & getValue() const
Get value.
Definition: Variant.h:187
ValueType & value()
Get access to the internal storage buffer.
Definition: Variant.h:173
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition: Variant.h:232
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:696
comms::option::def::HasCustomVersionUpdate HasCustomVersionUpdate
Same as comms::option::def::HasCustomVersionUpdate.
Definition: options.h:1800
comms::option::def::VersionType< T > VersionType
Same as comms::option::def::VersionType.
Definition: options.h:1797
comms::option::def::FieldType< TMsg > FieldType
Same as comms::option::def::FieldType.
Definition: options.h:1463
comms::option::def::Endian< TEndian > Endian
Same as comms::option::def::Endian.
Definition: options.h:1438
T readData(TIter &iter, const traits::endian::Big &endian)
Same as readBig<T, TIter>()
Definition: access.h:766
void writeData(T value, TIter &iter, const traits::endian::Big &endian)
Same as writeBig<T, TIter>()
Definition: access.h:698
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:35