17#include "comms/details/tag.h"
18#include "comms/field/basic/IntValue.h"
19#include "comms/field/details/AdaptBasicField.h"
29COMMS_MSVC_WARNING_PUSH
30COMMS_MSVC_WARNING_DISABLE(4127)
77template <
typename TFieldBase,
typename T,
typename... TOptions>
78class IntValue :
public details::AdaptBasicFieldT<basic::IntValue<TFieldBase, T>, TOptions...>
80 using BaseImpl = details::AdaptBasicFieldT<basic::IntValue<TFieldBase, T>, TOptions...>;
87 using Endian =
typename BaseImpl::Endian;
140 static constexpr
bool hasFailOnInvalid()
142 return ParsedOptions::HasFailOnInvalid;
149 return ParsedOptions::HasIgnoreInvalid;
156 return ParsedOptions::HasEmptySerialization;
163 return ParsedOptions::HasUnits;
170 return ParsedOptions::HasScalingRatio && !std::is_same<ScalingRatio, std::ratio<1, 1> >::value;
177 return ParsedOptions::HasFieldType;
184 return ParsedOptions::HasVarLengthLimits;
191 return ParsedOptions::HasFixedValue;
198 return ParsedOptions::HasDisplayOffset;
205 return ParsedOptions::HasName;
215 template <
typename TRet>
219 typename comms::util::LazyShallowConditional<
220 ParsedOptions::HasScalingRatio
226 return scaleAsInternal<TRet>(TagTmp());
230 template <
typename TRet>
233 return getScaled<TRet>();
240 template <
typename TScaled>
244 typename comms::util::LazyShallowConditional<
245 ParsedOptions::HasScalingRatio
251 return setScaledInternal(val, TagTmp());
257 return BaseImpl::value();
263 return BaseImpl::value();
270 return BaseImpl::getValue();
275 template <
typename U>
278 BaseImpl::setValue(std::forward<U>(val));
286 return static_cast<ValueType>(getValue() + BaseImpl::displayOffset());
292 template <
typename U>
295 setValue(val - BaseImpl::displayOffset());
301 return std::numeric_limits<ValueType>::max();
307 return std::numeric_limits<ValueType>::min();
314 return BaseImpl::length();
321 return BaseImpl::minLength();
328 return BaseImpl::maxLength();
334 return BaseImpl::valid();
341 return BaseImpl::refresh();
349 template <
typename TIter>
352 return BaseImpl::read(iter, size);
359 return BaseImpl::hasReadNoStatus();
367 template <
typename TIter>
370 BaseImpl::readNoStatus(iter);
376 return BaseImpl::canWrite();
384 template <
typename TIter>
387 return BaseImpl::write(iter, size);
394 return BaseImpl::hasWriteNoStatus();
402 template <
typename TIter>
405 BaseImpl::writeNoStatus(iter);
411 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
417 return BaseImpl::hasNonDefaultRefresh();
424 return BaseImpl::getVersion();
431 return BaseImpl::setVersion(
version);
443 BaseImpl::setForcedLength(len);
450 return BaseImpl::getForcedLength();
454 using BaseImpl::readData;
455 using BaseImpl::writeData;
458 template <
typename... TParams>
459 using HasScalingRatioTag = comms::details::tag::Tag1<>;
461 template <
typename... TParams>
462 using NoScalingRatioTag = comms::details::tag::Tag2<>;
464 template <
typename... TParams>
465 using ScaleAsFpTag = comms::details::tag::Tag3<>;
467 template <
typename... TParams>
468 using ScaleAsIntTag = comms::details::tag::Tag4<>;
470 template <
typename TRet,
typename... TParams>
471 TRet scaleAsInternal(HasScalingRatioTag<TParams...>)
const
474 typename comms::util::LazyShallowConditional<
475 std::is_floating_point<TRet>::value
481 return scaleAsInternal<TRet>(TagTmp());
484 template <
typename TRet,
typename... TParams>
485 TRet scaleAsInternal(ScaleAsFpTag<TParams...>)
const
487 static_assert(std::is_floating_point<TRet>::value,
488 "TRet is expected to be floating point type");
489 return static_cast<TRet
>(BaseImpl::value()) * (
static_cast<TRet
>(ParsedOptions::ScalingRatio::num) /
static_cast<TRet
>(ParsedOptions::ScalingRatio::den));
492 template <
typename TRet,
typename... TParams>
493 TRet scaleAsInternal(ScaleAsIntTag<TParams...>)
const
495 static_assert(std::is_integral<TRet>::value,
496 "TRet is expected to be integral type");
500 std::is_signed<TRet>::value
508 (
static_cast<CastType
>(BaseImpl::value()) * ParsedOptions::ScalingRatio::num) / ParsedOptions::ScalingRatio::den);
511 template <
typename TRet,
typename... TParams>
512 TRet scaleAsInternal(NoScalingRatioTag<TParams...>)
const
514 return static_cast<TRet
>(BaseImpl::value());
517 template <
typename TScaled,
typename... TParams>
518 void setScaledInternal(TScaled val, HasScalingRatioTag<TParams...>)
521 typename comms::util::LazyShallowConditional<
522 std::is_floating_point<
typename std::decay<
decltype(val)>::type>::value
528 setScaledInternal(val, TagTmp());
531 template <
typename TScaled,
typename... TParams>
532 void setScaledInternal(TScaled val, ScaleAsFpTag<TParams...>)
534 using DecayedType =
typename std::decay<
decltype(val)>::type;
535 auto epsilon = DecayedType(0);
536 if (ParsedOptions::ScalingRatio::num < ParsedOptions::ScalingRatio::den) {
537 epsilon =
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::num) /
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::den + 1);
540 if (epsilon < DecayedType(0)) {
544 if (val < DecayedType(0)) {
549 static_cast<ValueType
>(
550 ((val + epsilon) *
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::den)) /
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::num));
553 template <
typename TScaled,
typename... TParams>
554 void setScaledInternal(TScaled val, ScaleAsIntTag<TParams...>)
558 std::is_signed<
typename std::decay<
decltype(val)>::type>::value
565 static_cast<ValueType
>(
566 (
static_cast<CastType
>(val) * ParsedOptions::ScalingRatio::den) /
static_cast<CastType
>(ParsedOptions::ScalingRatio::num));
569 template <
typename TScaled,
typename... TParams>
570 void setScaledInternal(TScaled val, NoScalingRatioTag<TParams...>)
572 BaseImpl::value() =
static_cast<ValueType
>(val);
575 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
576 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to IntValue field");
577 static_assert(!ParsedOptions::HasSequenceSizeForcing,
578 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to IntValue field");
579 static_assert(!ParsedOptions::HasSequenceLengthForcing,
580 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to IntValue field");
581 static_assert(!ParsedOptions::HasSequenceFixedSize,
582 "comms::option::def::SequenceFixedSize option is not applicable to IntValue field");
583 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
584 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to IntValue field");
585 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
586 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to IntValue field");
587 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
588 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to IntValue field");
589 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
590 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to IntValue field");
591 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
592 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to IntValue field");
593 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
594 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to IntValue field");
595 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
596 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to IntValue field");
597 static_assert(!ParsedOptions::HasFixedSizeStorage,
598 "comms::option::app::FixedSizeStorage option is not applicable to IntValue field");
599 static_assert(!ParsedOptions::HasCustomStorageType,
600 "comms::option::app::CustomStorageType option is not applicable to IntValue field");
601 static_assert(!ParsedOptions::HasOrigDataView,
602 "comms::option::app::OrigDataView option is not applicable to IntValue field");
603 static_assert(!ParsedOptions::HasVersionsRange,
604 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to IntValue field");
605 static_assert(!ParsedOptions::HasMissingOnReadFail,
606 "comms::option::def::MissingOnReadFail option is not applicable to IntValue field");
607 static_assert(!ParsedOptions::HasMissingOnInvalid,
608 "comms::option::def::MissingOnInvalid option is not applicable to IntValue field");
616template <
typename TFieldBase,
typename T,
typename... TOptions>
621 return field1.value() == field2.value();
629template <
typename TFieldBase,
typename T,
typename... TOptions>
634 return field1.value() != field2.value();
642template <
typename TFieldBase,
typename T,
typename... TOptions>
647 return field1.value() < field2.value();
658 return std::is_same<typename T::CommsTag, tag::Int>::value;
664template <
typename TFieldBase,
typename T,
typename... TOptions>
666IntValue<TFieldBase, T, TOptions...>&
675template <
typename TFieldBase,
typename T,
typename... TOptions>
677const IntValue<TFieldBase, T, TOptions...>&
687COMMS_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:79
static constexpr ValueType minValue()
Get minimal numeric value the field can hold.
Definition IntValue.h:305
typename BaseImpl::ValueType ValueType
Type of underlying integral value.
Definition IntValue.h:100
IntValue()=default
Default constructor.
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition IntValue.h:175
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition IntValue.h:93
void setScaled(TScaled val)
Opposite operation to getScaled().
Definition IntValue.h:241
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:667
ValueType getDisplayValue() const
Get display value.
Definition IntValue.h:284
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition IntValue.h:96
~IntValue() noexcept=default
Destructor.
typename BaseImpl::VersionType VersionType
Version type.
Definition IntValue.h:90
bool setVersion(VersionType version)
Default implementation of version update.
Definition IntValue.h:429
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition IntValue.h:117
IntValue(const IntValue &)=default
Copy constructor.
bool valid() const
Check validity of the field value.
Definition IntValue.h:332
static constexpr bool hasScaling()
Compile time inquiry of whether scaling ratio has been provided via comms::option::def::ScalingRatio ...
Definition IntValue.h:168
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition IntValue.h:104
void setDisplayValue(U &&val)
Set display value.
Definition IntValue.h:293
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition IntValue.h:392
int getForcedLength() const
Get forced serialization length.
Definition IntValue.h:448
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition IntValue.h:147
static constexpr bool hasDisplayOffset()
Compile time inquiry of whether comms::option::def::DisplayOffset option has been used.
Definition IntValue.h:196
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:678
bool operator==(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition IntValue.h:617
typename ParsedOptions::ScalingRatio ScalingRatio
Scaling ratio defined by the comms::option::def::ScalingRatio option.
Definition IntValue.h:108
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition IntValue.h:112
VersionType getVersion() const
Get version of the field.
Definition IntValue.h:422
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition IntValue.h:312
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition IntValue.h:326
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition IntValue.h:368
ValueType & value()
Get access to integral value storage.
Definition IntValue.h:261
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition IntValue.h:189
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition IntValue.h:154
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition IntValue.h:319
bool operator<(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition IntValue.h:643
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition IntValue.h:203
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition IntValue.h:84
bool operator!=(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition IntValue.h:630
static constexpr ValueType maxValue()
Get maximal numeric value the field can hold.
Definition IntValue.h:299
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition IntValue.h:357
constexpr TRet getScaled() const
Scales value according to ratio specified in provided comms::option::def::ScalingRatio option.
Definition IntValue.h:216
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition IntValue.h:350
void setForcedLength(int len)
Force serialization length of the field.
Definition IntValue.h:441
void setValue(U &&val)
Set value.
Definition IntValue.h:276
constexpr TRet scaleAs() const
Same as getScaled()
Definition IntValue.h:231
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition IntValue.h:385
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition IntValue.h:415
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:161
const ValueType & value() const
Get access to integral value storage.
Definition IntValue.h:255
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition IntValue.h:87
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition IntValue.h:403
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition IntValue.h:409
bool refresh()
Refresh the field's value.
Definition IntValue.h:339
IntValue(const ValueType &val)
Constructor.
Definition IntValue.h:124
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition IntValue.h:374
static constexpr bool hasVarLength()
Compile time inquiry of whether comms::option::def::VarLength option has been used.
Definition IntValue.h:182
constexpr bool isIntValue()
Compile time check function of whether a provided type is any variant of comms::field::IntValue.
Definition IntValue.h:656
const ValueType & getValue() const
Get value.
Definition IntValue.h:268
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:19
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:66
Contains definition of all the options used by the COMMS library.
Replacement to std::conditional.
Definition type_traits.h:32
Replacement to some types from standard type_traits.