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 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include "comms/ErrorStatus.h"
16#include "comms/field/basic/FloatValue.h"
17#include "comms/field/details/AdaptBasicField.h"
18#include "comms/field/tag.h"
19#include "comms/options.h"
20
21#include <cstddef>
22#include <type_traits>
23#include <utility>
24
25namespace comms
26{
27
28namespace field
29{
30
66template <typename TFieldBase, typename T, typename... TOptions>
67class FloatValue : public details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>
68{
69 using BaseImpl = details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>;
70public:
72 using FieldBase = TFieldBase;
73
75 using Endian = typename BaseImpl::Endian;
76
78 using VersionType = typename BaseImpl::VersionType;
79
81 using ParsedOptions = details::OptionsParser<TOptions...>;
82
84 using CommsTag = typename BaseImpl::CommsTag;
85
88 using ValueType = typename BaseImpl::ValueType;
89
92 using UnitsType = typename ParsedOptions::UnitsType;
93
96 using UnitsRatio = typename ParsedOptions::UnitsRatio;
97
101 using FieldType = typename ParsedOptions::FieldType;
102
105 FloatValue() = default;
106
108 explicit FloatValue(const ValueType& val)
109 : BaseImpl(val)
110 {
111 }
112
115 static constexpr bool hasFailOnInvalid()
116 {
117 return ParsedOptions::HasFailOnInvalid;
118 }
119
122 static constexpr bool hasIgnoreInvalid()
123 {
124 return ParsedOptions::HasIgnoreInvalid;
125 }
126
129 static constexpr bool hasEmptySerialization()
130 {
131 return ParsedOptions::HasEmptySerialization;
132 }
133
136 static constexpr bool hasUnits()
137 {
138 return ParsedOptions::HasUnits;
139 }
140
143 static constexpr bool hasFieldType()
144 {
145 return ParsedOptions::HasFieldType;
146 }
147
150 static constexpr bool hasFixedValue()
151 {
152 return ParsedOptions::HasFixedValue;
153 }
154
157 static constexpr bool hasName()
158 {
159 return ParsedOptions::HasName;
160 }
161
163 const ValueType& value() const
164 {
165 return BaseImpl::value();
166 }
167
170 {
171 return BaseImpl::value();
172 }
173
176 const ValueType& getValue() const
177 {
178 return BaseImpl::getValue();
179 }
180
183 template <typename U>
184 void setValue(U&& val)
185 {
186 BaseImpl::setValue(std::forward<U>(val));
187 }
188
191 constexpr std::size_t length() const
192 {
193 return BaseImpl::length();
194 }
195
198 static constexpr std::size_t minLength()
199 {
200 return BaseImpl::minLength();
201 }
202
205 static constexpr std::size_t maxLength()
206 {
207 return BaseImpl::maxLength();
208 }
209
211 bool valid() const
212 {
213 return BaseImpl::valid();
214 }
215
218 bool refresh()
219 {
220 return BaseImpl::refresh();
221 }
222
228 template <typename TIter>
229 ErrorStatus read(TIter& iter, std::size_t size)
230 {
231 return BaseImpl::read(iter, size);
232 }
233
236 static constexpr bool hasReadNoStatus()
237 {
238 return BaseImpl::hasReadNoStatus();
239 }
240
246 template <typename TIter>
247 void readNoStatus(TIter& iter)
248 {
249 BaseImpl::readNoStatus(iter);
250 }
251
253 bool canWrite() const
254 {
255 return BaseImpl::canWrite();
256 }
257
263 template <typename TIter>
264 ErrorStatus write(TIter& iter, std::size_t size) const
265 {
266 return BaseImpl::write(iter, size);
267 }
268
271 static constexpr bool hasWriteNoStatus()
272 {
273 return BaseImpl::hasWriteNoStatus();
274 }
275
281 template <typename TIter>
282 void writeNoStatus(TIter& iter) const
283 {
284 BaseImpl::writeNoStatus(iter);
285 }
286
288 static constexpr bool isVersionDependent()
289 {
290 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
291 }
292
294 static constexpr bool hasNonDefaultRefresh()
295 {
296 return BaseImpl::hasNonDefaultRefresh();
297 }
298
302 {
303 return BaseImpl::getVersion();
304 }
305
309 {
310 return BaseImpl::setVersion(version);
311 }
312
313protected:
314 using BaseImpl::readData;
315 using BaseImpl::writeData;
316
317private:
318 static_assert(!ParsedOptions::HasVarLengthLimits,
319 "comms::option::def::VarLength option is not applicable to FloatValue field");
320 static_assert(!ParsedOptions::HasAvailableLengthLimit,
321 "comms::option::def::AvailableLengthLimit option is not applicable to FloatValue field");
322 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
323 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to FloatValue field");
324 static_assert(!ParsedOptions::HasSequenceSizeForcing,
325 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to FloatValue field");
326 static_assert(!ParsedOptions::HasSequenceLengthForcing,
327 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to FloatValue field");
328 static_assert(!ParsedOptions::HasSequenceFixedSize,
329 "comms::option::def::SequenceFixedSize option is not applicable to FloatValue field");
330 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
331 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to FloatValue field");
332 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
333 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to FloatValue field");
334 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
335 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to FloatValue field");
336 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
337 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to FloatValue field");
338 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
339 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to FloatValue field");
340 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
341 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to FloatValue field");
342 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
343 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to FloatValue field");
344 static_assert(!ParsedOptions::HasFixedSizeStorage,
345 "comms::option::app::FixedSizeStorage option is not applicable to FloatValue field");
346 static_assert(!ParsedOptions::HasCustomStorageType,
347 "comms::option::app::CustomStorageType option is not applicable to FloatValue field");
348 static_assert(!ParsedOptions::HasOrigDataView,
349 "comms::option::app::OrigDataView option is not applicable to FloatValue field");
350 static_assert(!ParsedOptions::HasVersionsRange,
351 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to FloatValue field");
352 static_assert(!ParsedOptions::HasMissingOnReadFail,
353 "comms::option::def::MissingOnReadFail option is not applicable to FloatValue field");
354 static_assert(!ParsedOptions::HasMissingOnInvalid,
355 "comms::option::def::MissingOnInvalid option is not applicable to FloatValue field");
356};
357
363template <typename TFieldBase, typename T, typename... TOptions>
366 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
367{
368 return field1.value() == field2.value();
369}
370
376template <typename TFieldBase, typename T, typename... TOptions>
379 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
380{
381 return field1.value() != field2.value();
382}
383
389template <typename TFieldBase, typename T, typename... TOptions>
392 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
393{
394 return field1.value() < field2.value();
395}
396
402template <typename T>
403constexpr bool isFloatValue()
404{
405 return std::is_same<typename T::CommsTag, tag::Float>::value;
406}
407
411template <typename TFieldBase, typename T, typename... TOptions>
412inline
413FloatValue<TFieldBase, T, TOptions...>&
415{
416 return field;
417}
418
422template <typename TFieldBase, typename T, typename... TOptions>
423inline
424const FloatValue<TFieldBase, T, TOptions...>&
426{
427 return field;
428}
429
430} // namespace field
431
432} // namespace comms
433
This file contain definition of error statuses used by comms module.
Field that represent floating point value.
Definition FloatValue.h:68
bool setVersion(VersionType version)
Default implementation of version update.
Definition FloatValue.h:308
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition FloatValue.h:96
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition FloatValue.h:294
typename BaseImpl::ValueType ValueType
Type of underlying floating point value.
Definition FloatValue.h:88
constexpr bool isFloatValue()
Compile time check function of whether a provided type is any variant of comms::field::FloatValue.
Definition FloatValue.h:403
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition FloatValue.h:84
bool valid() const
Check validity of the field value.
Definition FloatValue.h:211
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:414
bool operator!=(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition FloatValue.h:377
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition FloatValue.h:157
ValueType & value()
Get access to floating point value storage.
Definition FloatValue.h:169
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition FloatValue.h:236
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition FloatValue.h:92
bool operator==(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition FloatValue.h:364
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition FloatValue.h:75
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition FloatValue.h:129
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition FloatValue.h:143
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition FloatValue.h:271
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition FloatValue.h:264
const ValueType & value() const
Get access to floating point value storage.
Definition FloatValue.h:163
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition FloatValue.h:229
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition FloatValue.h:247
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition FloatValue.h:150
bool refresh()
Refresh the field's value.
Definition FloatValue.h:218
void setValue(U &&val)
Set value.
Definition FloatValue.h:184
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition FloatValue.h:81
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition FloatValue.h:282
FloatValue()=default
Default constructor.
const ValueType & getValue() const
Get value.
Definition FloatValue.h:176
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition FloatValue.h:115
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition FloatValue.h:191
FloatValue(const ValueType &val)
Constructor.
Definition FloatValue.h:108
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition FloatValue.h:198
typename BaseImpl::VersionType VersionType
Version type.
Definition FloatValue.h:78
VersionType getVersion() const
Get version of the field.
Definition FloatValue.h:301
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition FloatValue.h:72
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition FloatValue.h:288
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition FloatValue.h:205
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:136
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition FloatValue.h:122
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition FloatValue.h:253
bool operator<(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition FloatValue.h:390
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:425
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition FloatValue.h:101
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: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.