21#include "basic/IntValue.h"
22#include "details/AdaptBasicField.h"
23#include "comms/details/tag.h"
25COMMS_MSVC_WARNING_PUSH
26COMMS_MSVC_WARNING_DISABLE(4127)
73template <
typename TFieldBase,
typename T,
typename... TOptions>
74class IntValue :
public details::AdaptBasicFieldT<basic::IntValue<TFieldBase, T>, TOptions...>
76 using BaseImpl = details::AdaptBasicFieldT<basic::IntValue<TFieldBase, T>, TOptions...>;
83 using Endian =
typename BaseImpl::Endian;
136 static constexpr
bool hasFailOnInvalid()
138 return ParsedOptions::HasFailOnInvalid;
145 return ParsedOptions::HasIgnoreInvalid;
152 return ParsedOptions::HasEmptySerialization;
159 return ParsedOptions::HasUnits;
166 return ParsedOptions::HasScalingRatio && !std::is_same<ScalingRatio, std::ratio<1, 1> >::value;
173 return ParsedOptions::HasFieldType;
180 return ParsedOptions::HasVarLengthLimits;
187 return ParsedOptions::HasFixedValue;
194 return ParsedOptions::HasDisplayOffset;
201 return ParsedOptions::HasName;
211 template <
typename TRet>
215 typename comms::util::LazyShallowConditional<
216 ParsedOptions::HasScalingRatio
222 return scaleAsInternal<TRet>(TagTmp());
226 template <
typename TRet>
229 return getScaled<TRet>();
236 template <
typename TScaled>
240 typename comms::util::LazyShallowConditional<
241 ParsedOptions::HasScalingRatio
247 return setScaledInternal(val, TagTmp());
253 return BaseImpl::value();
259 return BaseImpl::value();
266 return BaseImpl::getValue();
271 template <
typename U>
274 BaseImpl::setValue(std::forward<U>(val));
282 return static_cast<ValueType>(getValue() + BaseImpl::displayOffset());
288 template <
typename U>
291 setValue(val - BaseImpl::displayOffset());
297 return std::numeric_limits<ValueType>::max();
303 return std::numeric_limits<ValueType>::min();
310 return BaseImpl::length();
317 return BaseImpl::minLength();
324 return BaseImpl::maxLength();
330 return BaseImpl::valid();
337 return BaseImpl::refresh();
345 template <
typename TIter>
348 return BaseImpl::read(iter, size);
355 return BaseImpl::hasReadNoStatus();
363 template <
typename TIter>
366 BaseImpl::readNoStatus(iter);
372 return BaseImpl::canWrite();
380 template <
typename TIter>
383 return BaseImpl::write(iter, size);
390 return BaseImpl::hasWriteNoStatus();
398 template <
typename TIter>
401 BaseImpl::writeNoStatus(iter);
407 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
413 return BaseImpl::hasNonDefaultRefresh();
420 return BaseImpl::getVersion();
427 return BaseImpl::setVersion(
version);
439 BaseImpl::setForcedLength(len);
446 return BaseImpl::getForcedLength();
450 using BaseImpl::readData;
451 using BaseImpl::writeData;
454 template <
typename... TParams>
455 using HasScalingRatioTag = comms::details::tag::Tag1<>;
457 template <
typename... TParams>
458 using NoScalingRatioTag = comms::details::tag::Tag2<>;
460 template <
typename... TParams>
461 using ScaleAsFpTag = comms::details::tag::Tag3<>;
463 template <
typename... TParams>
464 using ScaleAsIntTag = comms::details::tag::Tag4<>;
466 template <
typename TRet,
typename... TParams>
467 TRet scaleAsInternal(HasScalingRatioTag<TParams...>)
const
470 typename comms::util::LazyShallowConditional<
471 std::is_floating_point<TRet>::value
477 return scaleAsInternal<TRet>(TagTmp());
480 template <
typename TRet,
typename... TParams>
481 TRet scaleAsInternal(ScaleAsFpTag<TParams...>)
const
483 static_assert(std::is_floating_point<TRet>::value,
484 "TRet is expected to be floating point type");
485 return static_cast<TRet
>(BaseImpl::value()) * (
static_cast<TRet
>(ParsedOptions::ScalingRatio::num) /
static_cast<TRet
>(ParsedOptions::ScalingRatio::den));
488 template <
typename TRet,
typename... TParams>
489 TRet scaleAsInternal(ScaleAsIntTag<TParams...>)
const
491 static_assert(std::is_integral<TRet>::value,
492 "TRet is expected to be integral type");
496 std::is_signed<TRet>::value
504 (
static_cast<CastType
>(BaseImpl::value()) * ParsedOptions::ScalingRatio::num) / ParsedOptions::ScalingRatio::den);
507 template <
typename TRet,
typename... TParams>
508 TRet scaleAsInternal(NoScalingRatioTag<TParams...>)
const
510 return static_cast<TRet
>(BaseImpl::value());
513 template <
typename TScaled,
typename... TParams>
514 void setScaledInternal(TScaled val, HasScalingRatioTag<TParams...>)
517 typename comms::util::LazyShallowConditional<
518 std::is_floating_point<
typename std::decay<
decltype(val)>::type>::value
524 setScaledInternal(val, TagTmp());
527 template <
typename TScaled,
typename... TParams>
528 void setScaledInternal(TScaled val, ScaleAsFpTag<TParams...>)
530 using DecayedType =
typename std::decay<
decltype(val)>::type;
531 auto epsilon = DecayedType(0);
532 if (ParsedOptions::ScalingRatio::num < ParsedOptions::ScalingRatio::den) {
533 epsilon =
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::num) /
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::den + 1);
536 if (epsilon < DecayedType(0)) {
540 if (val < DecayedType(0)) {
545 static_cast<ValueType
>(
546 ((val + epsilon) *
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::den)) /
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::num));
549 template <
typename TScaled,
typename... TParams>
550 void setScaledInternal(TScaled val, ScaleAsIntTag<TParams...>)
554 std::is_signed<
typename std::decay<
decltype(val)>::type>::value
561 static_cast<ValueType
>(
562 (
static_cast<CastType
>(val) * ParsedOptions::ScalingRatio::den) /
static_cast<CastType
>(ParsedOptions::ScalingRatio::num));
565 template <
typename TScaled,
typename... TParams>
566 void setScaledInternal(TScaled val, NoScalingRatioTag<TParams...>)
568 BaseImpl::value() =
static_cast<ValueType
>(val);
571 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
572 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to IntValue field");
573 static_assert(!ParsedOptions::HasSequenceSizeForcing,
574 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to IntValue field");
575 static_assert(!ParsedOptions::HasSequenceLengthForcing,
576 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to IntValue field");
577 static_assert(!ParsedOptions::HasSequenceFixedSize,
578 "comms::option::def::SequenceFixedSize option is not applicable to IntValue field");
579 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
580 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to IntValue field");
581 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
582 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to IntValue field");
583 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
584 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to IntValue field");
585 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
586 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to IntValue field");
587 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
588 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to IntValue field");
589 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
590 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to IntValue field");
591 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
592 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to IntValue field");
593 static_assert(!ParsedOptions::HasFixedSizeStorage,
594 "comms::option::app::FixedSizeStorage option is not applicable to IntValue field");
595 static_assert(!ParsedOptions::HasCustomStorageType,
596 "comms::option::app::CustomStorageType option is not applicable to IntValue field");
597 static_assert(!ParsedOptions::HasOrigDataView,
598 "comms::option::app::OrigDataView option is not applicable to IntValue field");
599 static_assert(!ParsedOptions::HasVersionsRange,
600 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to IntValue field");
601 static_assert(!ParsedOptions::HasMissingOnReadFail,
602 "comms::option::def::MissingOnReadFail option is not applicable to IntValue field");
603 static_assert(!ParsedOptions::HasMissingOnInvalid,
604 "comms::option::def::MissingOnInvalid option is not applicable to IntValue field");
613template <
typename TFieldBase,
typename T,
typename... TOptions>
618 return field1.value() == field2.value();
626template <
typename TFieldBase,
typename T,
typename... TOptions>
631 return field1.value() != field2.value();
639template <
typename TFieldBase,
typename T,
typename... TOptions>
644 return field1.value() < field2.value();
655 return std::is_same<typename T::CommsTag, tag::Int>::value;
661template <
typename TFieldBase,
typename T,
typename... TOptions>
663IntValue<TFieldBase, T, TOptions...>&
672template <
typename TFieldBase,
typename T,
typename... TOptions>
674const IntValue<TFieldBase, T, TOptions...>&
684COMMS_MSVC_WARNING_POP
Contains various compiler related definitions.
This file contain definition of error statuses used by comms module.
Field that represent integral value.
Definition IntValue.h:75
static constexpr ValueType minValue()
Get minimal numeric value the field can hold.
Definition IntValue.h:301
typename BaseImpl::ValueType ValueType
Type of underlying integral value.
Definition IntValue.h:96
IntValue()=default
Default constructor.
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition IntValue.h:171
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition IntValue.h:89
void setScaled(TScaled val)
Opposite operation to getScaled().
Definition IntValue.h:237
IntValue< TFieldBase, T, TOptions... > & toFieldBase(IntValue< TFieldBase, T, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::IntValue type in order to have access...
Definition IntValue.h:664
ValueType getDisplayValue() const
Get display value.
Definition IntValue.h:280
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition IntValue.h:92
~IntValue() noexcept=default
Destructor.
typename BaseImpl::VersionType VersionType
Version type.
Definition IntValue.h:86
bool setVersion(VersionType version)
Default implementation of version update.
Definition IntValue.h:425
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition IntValue.h:113
IntValue(const IntValue &)=default
Copy constructor.
bool valid() const
Check validity of the field value.
Definition IntValue.h:328
static constexpr bool hasScaling()
Compile time inquiry of whether scaling ratio has been provided via comms::option::def::ScalingRatio ...
Definition IntValue.h:164
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition IntValue.h:100
void setDisplayValue(U &&val)
Set display value.
Definition IntValue.h:289
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition IntValue.h:388
int getForcedLength() const
Get forced serialization length.
Definition IntValue.h:444
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition IntValue.h:143
static constexpr bool hasDisplayOffset()
Compile time inquiry of whether comms::option::def::DisplayOffset option has been used.
Definition IntValue.h:192
const IntValue< TFieldBase, T, TOptions... > & toFieldBase(const IntValue< TFieldBase, T, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::IntValue type in order to have access...
Definition IntValue.h:675
bool operator==(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition IntValue.h:614
typename ParsedOptions::ScalingRatio ScalingRatio
Scaling ratio defined by the comms::option::def::ScalingRatio option.
Definition IntValue.h:104
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition IntValue.h:108
VersionType getVersion() const
Get version of the field.
Definition IntValue.h:418
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition IntValue.h:308
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition IntValue.h:322
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition IntValue.h:364
ValueType & value()
Get access to integral value storage.
Definition IntValue.h:257
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition IntValue.h:185
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition IntValue.h:150
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition IntValue.h:315
bool operator<(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition IntValue.h:640
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition IntValue.h:199
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition IntValue.h:80
bool operator!=(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition IntValue.h:627
static constexpr ValueType maxValue()
Get maximal numeric value the field can hold.
Definition IntValue.h:295
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition IntValue.h:353
constexpr TRet getScaled() const
Scales value according to ratio specified in provided comms::option::def::ScalingRatio option.
Definition IntValue.h:212
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition IntValue.h:346
void setForcedLength(int len)
Force serialization length of the field.
Definition IntValue.h:437
void setValue(U &&val)
Set value.
Definition IntValue.h:272
constexpr TRet scaleAs() const
Same as getScaled()
Definition IntValue.h:227
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition IntValue.h:381
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition IntValue.h:411
static constexpr bool hasUnits()
Compile time inquiry of whether units have been set via any of the comms::option::def::Units* options...
Definition IntValue.h:157
const ValueType & value() const
Get access to integral value storage.
Definition IntValue.h:251
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition IntValue.h:83
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition IntValue.h:399
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition IntValue.h:405
bool refresh()
Refresh the field's value.
Definition IntValue.h:335
IntValue(const ValueType &val)
Constructor.
Definition IntValue.h:120
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition IntValue.h:370
static constexpr bool hasVarLength()
Compile time inquiry of whether comms::option::def::VarLength option has been used.
Definition IntValue.h:178
constexpr bool isIntValue()
Compile time check function of whether a provided type is any variant of comms::field::IntValue.
Definition IntValue.h:653
const ValueType & getValue() const
Get value.
Definition IntValue.h:264
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.
Replacement to std::conditional.
Definition type_traits.h:28
Replacement to some types from standard type_traits.