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)
70template <
typename TFieldBase,
typename T,
typename... TOptions>
71class IntValue :
public details::AdaptBasicFieldT<basic::IntValue<TFieldBase, T>, TOptions...>
73 using BaseImpl = details::AdaptBasicFieldT<basic::IntValue<TFieldBase, T>, TOptions...>;
80 using Endian =
typename BaseImpl::Endian;
97 using UnitsType =
typename ParsedOptions::UnitsType;
133 static constexpr
bool hasFailOnInvalid()
135 return ParsedOptions::HasFailOnInvalid;
142 return ParsedOptions::HasIgnoreInvalid;
149 return ParsedOptions::HasEmptySerialization;
156 return ParsedOptions::HasUnits;
163 return ParsedOptions::HasScalingRatio && !std::is_same<ScalingRatio, std::ratio<1, 1> >::value;
170 return ParsedOptions::HasFieldType;
177 return ParsedOptions::HasVarLengthLimits;
187 template <
typename TRet>
191 typename comms::util::LazyShallowConditional<
192 ParsedOptions::HasScalingRatio
198 return scaleAsInternal<TRet>(TagTmp());
202 template <
typename TRet>
205 return getScaled<TRet>();
212 template <
typename TScaled>
216 typename comms::util::LazyShallowConditional<
217 ParsedOptions::HasScalingRatio
223 return setScaledInternal(val, TagTmp());
229 return BaseImpl::value();
235 return BaseImpl::value();
242 return BaseImpl::getValue();
247 template <
typename U>
250 BaseImpl::setValue(std::forward<U>(val));
256 return std::numeric_limits<ValueType>::max();
262 return std::numeric_limits<ValueType>::min();
269 return BaseImpl::length();
276 return BaseImpl::minLength();
283 return BaseImpl::maxLength();
289 return BaseImpl::valid();
296 return BaseImpl::refresh();
304 template <
typename TIter>
307 return BaseImpl::read(iter, size);
314 return BaseImpl::hasReadNoStatus();
322 template <
typename TIter>
325 BaseImpl::readNoStatus(iter);
331 return BaseImpl::canWrite();
339 template <
typename TIter>
342 return BaseImpl::write(iter, size);
349 return BaseImpl::hasWriteNoStatus();
357 template <
typename TIter>
360 BaseImpl::writeNoStatus(iter);
366 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
372 return BaseImpl::hasNonDefaultRefresh();
379 return BaseImpl::getVersion();
386 return BaseImpl::setVersion(
version);
398 BaseImpl::setForcedLength(len);
405 return BaseImpl::getForcedLength();
409 using BaseImpl::readData;
410 using BaseImpl::writeData;
413 template <
typename... TParams>
414 using HasScalingRatioTag = comms::details::tag::Tag1<>;
416 template <
typename... TParams>
417 using NoScalingRatioTag = comms::details::tag::Tag2<>;
419 template <
typename... TParams>
420 using ScaleAsFpTag = comms::details::tag::Tag3<>;
422 template <
typename... TParams>
423 using ScaleAsIntTag = comms::details::tag::Tag4<>;
425 template <
typename TRet,
typename... TParams>
426 TRet scaleAsInternal(HasScalingRatioTag<TParams...>)
const
429 typename comms::util::LazyShallowConditional<
430 std::is_floating_point<TRet>::value
436 return scaleAsInternal<TRet>(TagTmp());
439 template <
typename TRet,
typename... TParams>
440 TRet scaleAsInternal(ScaleAsFpTag<TParams...>)
const
442 static_assert(std::is_floating_point<TRet>::value,
443 "TRet is expected to be floating point type");
444 return static_cast<TRet
>(BaseImpl::value()) * (
static_cast<TRet
>(ParsedOptions::ScalingRatio::num) /
static_cast<TRet
>(ParsedOptions::ScalingRatio::den));
447 template <
typename TRet,
typename... TParams>
448 TRet scaleAsInternal(ScaleAsIntTag<TParams...>)
const
450 static_assert(std::is_integral<TRet>::value,
451 "TRet is expected to be integral type");
455 std::is_signed<TRet>::value
463 (
static_cast<CastType
>(BaseImpl::value()) * ParsedOptions::ScalingRatio::num) / ParsedOptions::ScalingRatio::den);
466 template <
typename TRet,
typename... TParams>
467 TRet scaleAsInternal(NoScalingRatioTag<TParams...>)
const
469 return static_cast<TRet
>(BaseImpl::value());
472 template <
typename TScaled,
typename... TParams>
473 void setScaledInternal(TScaled val, HasScalingRatioTag<TParams...>)
476 typename comms::util::LazyShallowConditional<
477 std::is_floating_point<
typename std::decay<
decltype(val)>::type>::value
483 setScaledInternal(val, TagTmp());
486 template <
typename TScaled,
typename... TParams>
487 void setScaledInternal(TScaled val, ScaleAsFpTag<TParams...>)
489 using DecayedType =
typename std::decay<
decltype(val)>::type;
490 auto epsilon = DecayedType(0);
491 if (ParsedOptions::ScalingRatio::num < ParsedOptions::ScalingRatio::den) {
492 epsilon =
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::num) /
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::den + 1);
495 if (epsilon < DecayedType(0)) {
499 if (val < DecayedType(0)) {
504 static_cast<ValueType
>(
505 ((val + epsilon) *
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::den)) /
static_cast<DecayedType
>(ParsedOptions::ScalingRatio::num));
508 template <
typename TScaled,
typename... TParams>
509 void setScaledInternal(TScaled val, ScaleAsIntTag<TParams...>)
513 std::is_signed<
typename std::decay<
decltype(val)>::type>::value
520 static_cast<ValueType
>(
521 (
static_cast<CastType
>(val) * ParsedOptions::ScalingRatio::den) /
static_cast<CastType
>(ParsedOptions::ScalingRatio::num));
524 template <
typename TScaled,
typename... TParams>
525 void setScaledInternal(TScaled val, NoScalingRatioTag<TParams...>)
527 BaseImpl::value() =
static_cast<ValueType
>(val);
530 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
531 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to IntValue field");
532 static_assert(!ParsedOptions::HasSequenceSizeForcing,
533 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to IntValue field");
534 static_assert(!ParsedOptions::HasSequenceLengthForcing,
535 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to IntValue field");
536 static_assert(!ParsedOptions::HasSequenceFixedSize,
537 "comms::option::def::SequenceFixedSize option is not applicable to IntValue field");
538 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
539 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to IntValue field");
540 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
541 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to IntValue field");
542 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
543 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to IntValue field");
544 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
545 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to IntValue field");
546 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
547 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to IntValue field");
548 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
549 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to IntValue field");
550 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
551 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to IntValue field");
552 static_assert(!ParsedOptions::HasFixedSizeStorage,
553 "comms::option::app::FixedSizeStorage option is not applicable to IntValue field");
554 static_assert(!ParsedOptions::HasCustomStorageType,
555 "comms::option::app::CustomStorageType option is not applicable to IntValue field");
556 static_assert(!ParsedOptions::HasOrigDataView,
557 "comms::option::app::OrigDataView option is not applicable to IntValue field");
558 static_assert(!ParsedOptions::HasVersionsRange,
559 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to IntValue field");
560 static_assert(!ParsedOptions::HasMissingOnReadFail,
561 "comms::option::def::MissingOnReadFail option is not applicable to IntValue field");
562 static_assert(!ParsedOptions::HasMissingOnInvalid,
563 "comms::option::def::MissingOnInvalid option is not applicable to IntValue field");
572template <
typename TFieldBase,
typename T,
typename... TOptions>
577 return field1.value() == field2.value();
585template <
typename TFieldBase,
typename T,
typename... TOptions>
590 return field1.value() != field2.value();
598template <
typename TFieldBase,
typename T,
typename... TOptions>
603 return field1.value() < field2.value();
614 return std::is_same<typename T::CommsTag, tag::Int>::value;
620template <
typename TFieldBase,
typename T,
typename... TOptions>
622IntValue<TFieldBase, T, TOptions...>&
631template <
typename TFieldBase,
typename T,
typename... TOptions>
633const IntValue<TFieldBase, T, TOptions...>&
643COMMS_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:72
static constexpr ValueType minValue()
Get minimal numeric value the field can hold.
Definition IntValue.h:260
typename BaseImpl::ValueType ValueType
Type of underlying integral value.
Definition IntValue.h:93
IntValue()=default
Default constructor.
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition IntValue.h:168
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition IntValue.h:86
void setScaled(TScaled val)
Opposite operation to getScaled().
Definition IntValue.h:213
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:623
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition IntValue.h:89
~IntValue() noexcept=default
Destructor.
typename BaseImpl::VersionType VersionType
Version type.
Definition IntValue.h:83
bool setVersion(VersionType version)
Default implementation of version update.
Definition IntValue.h:384
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition IntValue.h:110
IntValue(const IntValue &)=default
Copy constructor.
bool valid() const
Check validity of the field value.
Definition IntValue.h:287
static constexpr bool hasScaling()
Compile time inquiry of whether scaling ratio has been provided via comms::option::def::ScalingRatio ...
Definition IntValue.h:161
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition IntValue.h:97
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition IntValue.h:347
int getForcedLength() const
Get forced serialization length.
Definition IntValue.h:403
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition IntValue.h:140
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:634
bool operator==(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition IntValue.h:573
typename ParsedOptions::ScalingRatio ScalingRatio
Scaling ratio defined by the comms::option::def::ScalingRatio option.
Definition IntValue.h:101
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition IntValue.h:105
VersionType getVersion() const
Get version of the field.
Definition IntValue.h:377
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition IntValue.h:267
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition IntValue.h:281
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition IntValue.h:323
ValueType & value()
Get access to integral value storage.
Definition IntValue.h:233
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition IntValue.h:147
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition IntValue.h:274
bool operator<(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition IntValue.h:599
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition IntValue.h:77
bool operator!=(const IntValue< TFieldBase, T, TOptions... > &field1, const IntValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition IntValue.h:586
static constexpr ValueType maxValue()
Get maximal numeric value the field can hold.
Definition IntValue.h:254
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition IntValue.h:312
constexpr TRet getScaled() const
Scales value according to ratio specified in provided comms::option::def::ScalingRatio option.
Definition IntValue.h:188
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition IntValue.h:305
void setForcedLength(int len)
Force serialization length of the field.
Definition IntValue.h:396
void setValue(U &&val)
Set value.
Definition IntValue.h:248
constexpr TRet scaleAs() const
Same as getScaled()
Definition IntValue.h:203
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition IntValue.h:340
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition IntValue.h:370
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:154
const ValueType & value() const
Get access to integral value storage.
Definition IntValue.h:227
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition IntValue.h:80
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition IntValue.h:358
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition IntValue.h:364
bool refresh()
Refresh the field's value.
Definition IntValue.h:294
IntValue(const ValueType &val)
Constructor.
Definition IntValue.h:117
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition IntValue.h:329
static constexpr bool hasVarLength()
Compile time inquiry of whether comms::option::def::VarLength option has been used.
Definition IntValue.h:175
constexpr bool isIntValue()
Compile time check function of whether a provided type is any variant of comms::field::IntValue.
Definition IntValue.h:612
const ValueType & getValue() const
Get value.
Definition IntValue.h:240
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.