COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
FloatValue.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 2025 (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/FloatValue.h"
16#include "details/AdaptBasicField.h"
17#include "tag.h"
18
19namespace comms
20{
21
22namespace field
23{
24
60template <typename TFieldBase, typename T, typename... TOptions>
61class FloatValue : public details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>
62{
63 using BaseImpl = details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>;
64public:
66 using FieldBase = TFieldBase;
67
69 using Endian = typename BaseImpl::Endian;
70
72 using VersionType = typename BaseImpl::VersionType;
73
75 using ParsedOptions = details::OptionsParser<TOptions...>;
76
78 using CommsTag = typename BaseImpl::CommsTag;
79
82 using ValueType = typename BaseImpl::ValueType;
83
86 using UnitsType = typename ParsedOptions::UnitsType;
87
90 using UnitsRatio = typename ParsedOptions::UnitsRatio;
91
95 using FieldType = typename ParsedOptions::FieldType;
96
99 FloatValue() = default;
100
102 explicit FloatValue(const ValueType& val)
103 : BaseImpl(val)
104 {
105 }
106
109 static constexpr bool hasFailOnInvalid()
110 {
111 return ParsedOptions::HasFailOnInvalid;
112 }
113
116 static constexpr bool hasIgnoreInvalid()
117 {
118 return ParsedOptions::HasIgnoreInvalid;
119 }
120
123 static constexpr bool hasEmptySerialization()
124 {
125 return ParsedOptions::HasEmptySerialization;
126 }
127
130 static constexpr bool hasUnits()
131 {
132 return ParsedOptions::HasUnits;
133 }
134
137 static constexpr bool hasFieldType()
138 {
139 return ParsedOptions::HasFieldType;
140 }
141
144 static constexpr bool hasFixedValue()
145 {
146 return ParsedOptions::HasFixedValue;
147 }
148
151 static constexpr bool hasName()
152 {
153 return ParsedOptions::HasName;
154 }
155
157 const ValueType& value() const
158 {
159 return BaseImpl::value();
160 }
161
164 {
165 return BaseImpl::value();
166 }
167
170 const ValueType& getValue() const
171 {
172 return BaseImpl::getValue();
173 }
174
177 template <typename U>
178 void setValue(U&& val)
179 {
180 BaseImpl::setValue(std::forward<U>(val));
181 }
182
185 constexpr std::size_t length() const
186 {
187 return BaseImpl::length();
188 }
189
192 static constexpr std::size_t minLength()
193 {
194 return BaseImpl::minLength();
195 }
196
199 static constexpr std::size_t maxLength()
200 {
201 return BaseImpl::maxLength();
202 }
203
205 bool valid() const
206 {
207 return BaseImpl::valid();
208 }
209
212 bool refresh()
213 {
214 return BaseImpl::refresh();
215 }
216
222 template <typename TIter>
223 ErrorStatus read(TIter& iter, std::size_t size)
224 {
225 return BaseImpl::read(iter, size);
226 }
227
230 static constexpr bool hasReadNoStatus()
231 {
232 return BaseImpl::hasReadNoStatus();
233 }
234
240 template <typename TIter>
241 void readNoStatus(TIter& iter)
242 {
243 BaseImpl::readNoStatus(iter);
244 }
245
247 bool canWrite() const
248 {
249 return BaseImpl::canWrite();
250 }
251
257 template <typename TIter>
258 ErrorStatus write(TIter& iter, std::size_t size) const
259 {
260 return BaseImpl::write(iter, size);
261 }
262
265 static constexpr bool hasWriteNoStatus()
266 {
267 return BaseImpl::hasWriteNoStatus();
268 }
269
275 template <typename TIter>
276 void writeNoStatus(TIter& iter) const
277 {
278 BaseImpl::writeNoStatus(iter);
279 }
280
282 static constexpr bool isVersionDependent()
283 {
284 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
285 }
286
288 static constexpr bool hasNonDefaultRefresh()
289 {
290 return BaseImpl::hasNonDefaultRefresh();
291 }
292
296 {
297 return BaseImpl::getVersion();
298 }
299
303 {
304 return BaseImpl::setVersion(version);
305 }
306
307protected:
308 using BaseImpl::readData;
309 using BaseImpl::writeData;
310
311private:
312 static_assert(!ParsedOptions::HasVarLengthLimits,
313 "comms::option::def::VarLength option is not applicable to FloatValue field");
314 static_assert(!ParsedOptions::HasAvailableLengthLimit,
315 "comms::option::def::AvailableLengthLimit option is not applicable to FloatValue field");
316 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
317 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to FloatValue field");
318 static_assert(!ParsedOptions::HasSequenceSizeForcing,
319 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to FloatValue field");
320 static_assert(!ParsedOptions::HasSequenceLengthForcing,
321 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to FloatValue field");
322 static_assert(!ParsedOptions::HasSequenceFixedSize,
323 "comms::option::def::SequenceFixedSize option is not applicable to FloatValue field");
324 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
325 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to FloatValue field");
326 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
327 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to FloatValue field");
328 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
329 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to FloatValue field");
330 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
331 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to FloatValue field");
332 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
333 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to FloatValue field");
334 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
335 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to FloatValue field");
336 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
337 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to FloatValue field");
338 static_assert(!ParsedOptions::HasFixedSizeStorage,
339 "comms::option::app::FixedSizeStorage option is not applicable to FloatValue field");
340 static_assert(!ParsedOptions::HasCustomStorageType,
341 "comms::option::app::CustomStorageType option is not applicable to FloatValue field");
342 static_assert(!ParsedOptions::HasOrigDataView,
343 "comms::option::app::OrigDataView option is not applicable to FloatValue field");
344 static_assert(!ParsedOptions::HasVersionsRange,
345 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to FloatValue field");
346 static_assert(!ParsedOptions::HasMissingOnReadFail,
347 "comms::option::def::MissingOnReadFail option is not applicable to FloatValue field");
348 static_assert(!ParsedOptions::HasMissingOnInvalid,
349 "comms::option::def::MissingOnInvalid option is not applicable to FloatValue field");
350};
351
357template <typename TFieldBase, typename T, typename... TOptions>
360 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
361{
362 return field1.value() == field2.value();
363}
364
370template <typename TFieldBase, typename T, typename... TOptions>
373 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
374{
375 return field1.value() != field2.value();
376}
377
383template <typename TFieldBase, typename T, typename... TOptions>
386 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
387{
388 return field1.value() < field2.value();
389}
390
396template <typename T>
397constexpr bool isFloatValue()
398{
399 return std::is_same<typename T::CommsTag, tag::Float>::value;
400}
401
405template <typename TFieldBase, typename T, typename... TOptions>
406inline
407FloatValue<TFieldBase, T, TOptions...>&
409{
410 return field;
411}
412
416template <typename TFieldBase, typename T, typename... TOptions>
417inline
418const FloatValue<TFieldBase, T, TOptions...>&
420{
421 return field;
422}
423
424} // namespace field
425
426} // namespace comms
427
428
429
This file contain definition of error statuses used by comms module.
Field that represent floating point value.
Definition FloatValue.h:62
bool setVersion(VersionType version)
Default implementation of version update.
Definition FloatValue.h:302
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition FloatValue.h:90
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition FloatValue.h:288
typename BaseImpl::ValueType ValueType
Type of underlying floating point value.
Definition FloatValue.h:82
constexpr bool isFloatValue()
Compile time check function of whether a provided type is any variant of comms::field::FloatValue.
Definition FloatValue.h:397
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition FloatValue.h:78
bool valid() const
Check validity of the field value.
Definition FloatValue.h:205
FloatValue< TFieldBase, T, TOptions... > & toFieldBase(FloatValue< TFieldBase, T, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::FloatValue type in order to have acce...
Definition FloatValue.h:408
bool operator!=(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition FloatValue.h:371
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition FloatValue.h:151
ValueType & value()
Get access to floating point value storage.
Definition FloatValue.h:163
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition FloatValue.h:230
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition FloatValue.h:86
bool operator==(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition FloatValue.h:358
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition FloatValue.h:69
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition FloatValue.h:123
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition FloatValue.h:137
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition FloatValue.h:265
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition FloatValue.h:258
const ValueType & value() const
Get access to floating point value storage.
Definition FloatValue.h:157
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition FloatValue.h:223
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition FloatValue.h:241
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition FloatValue.h:144
bool refresh()
Refresh the field's value.
Definition FloatValue.h:212
void setValue(U &&val)
Set value.
Definition FloatValue.h:178
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition FloatValue.h:75
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition FloatValue.h:276
FloatValue()=default
Default constructor.
const ValueType & getValue() const
Get value.
Definition FloatValue.h:170
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition FloatValue.h:109
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition FloatValue.h:185
FloatValue(const ValueType &val)
Constructor.
Definition FloatValue.h:102
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition FloatValue.h:192
typename BaseImpl::VersionType VersionType
Version type.
Definition FloatValue.h:72
VersionType getVersion() const
Get version of the field.
Definition FloatValue.h:295
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition FloatValue.h:66
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition FloatValue.h:282
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition FloatValue.h:199
static constexpr bool hasUnits()
Compile type inquiry of whether units have been set via any of the comms::option::def::Units* options...
Definition FloatValue.h:130
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition FloatValue.h:116
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition FloatValue.h:247
bool operator<(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition FloatValue.h:384
const FloatValue< TFieldBase, T, TOptions... > & toFieldBase(const FloatValue< TFieldBase, T, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::FloatValue type in order to have acce...
Definition FloatValue.h:419
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition FloatValue.h:95
Contains definition of various tag classes.
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.