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/field/basic/FloatValue.h"
15#include "comms/field/details/AdaptBasicField.h"
16#include "comms/field/tag.h"
17#include "comms/options.h"
18
19#include <utility>
20
21namespace comms
22{
23
24namespace field
25{
26
62template <typename TFieldBase, typename T, typename... TOptions>
63class FloatValue : public details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>
64{
65 using BaseImpl = details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>;
66public:
68 using FieldBase = TFieldBase;
69
71 using Endian = typename BaseImpl::Endian;
72
74 using VersionType = typename BaseImpl::VersionType;
75
77 using ParsedOptions = details::OptionsParser<TOptions...>;
78
80 using CommsTag = typename BaseImpl::CommsTag;
81
84 using ValueType = typename BaseImpl::ValueType;
85
88 using UnitsType = typename ParsedOptions::UnitsType;
89
92 using UnitsRatio = typename ParsedOptions::UnitsRatio;
93
97 using FieldType = typename ParsedOptions::FieldType;
98
101 FloatValue() = default;
102
104 explicit FloatValue(const ValueType& val)
105 : BaseImpl(val)
106 {
107 }
108
111 static constexpr bool hasFailOnInvalid()
112 {
113 return ParsedOptions::HasFailOnInvalid;
114 }
115
118 static constexpr bool hasIgnoreInvalid()
119 {
120 return ParsedOptions::HasIgnoreInvalid;
121 }
122
125 static constexpr bool hasEmptySerialization()
126 {
127 return ParsedOptions::HasEmptySerialization;
128 }
129
132 static constexpr bool hasUnits()
133 {
134 return ParsedOptions::HasUnits;
135 }
136
139 static constexpr bool hasFieldType()
140 {
141 return ParsedOptions::HasFieldType;
142 }
143
146 static constexpr bool hasFixedValue()
147 {
148 return ParsedOptions::HasFixedValue;
149 }
150
153 static constexpr bool hasName()
154 {
155 return ParsedOptions::HasName;
156 }
157
159 const ValueType& value() const
160 {
161 return BaseImpl::value();
162 }
163
166 {
167 return BaseImpl::value();
168 }
169
172 const ValueType& getValue() const
173 {
174 return BaseImpl::getValue();
175 }
176
179 template <typename U>
180 void setValue(U&& val)
181 {
182 BaseImpl::setValue(std::forward<U>(val));
183 }
184
187 constexpr std::size_t length() const
188 {
189 return BaseImpl::length();
190 }
191
194 static constexpr std::size_t minLength()
195 {
196 return BaseImpl::minLength();
197 }
198
201 static constexpr std::size_t maxLength()
202 {
203 return BaseImpl::maxLength();
204 }
205
207 bool valid() const
208 {
209 return BaseImpl::valid();
210 }
211
214 bool refresh()
215 {
216 return BaseImpl::refresh();
217 }
218
224 template <typename TIter>
225 ErrorStatus read(TIter& iter, std::size_t size)
226 {
227 return BaseImpl::read(iter, size);
228 }
229
232 static constexpr bool hasReadNoStatus()
233 {
234 return BaseImpl::hasReadNoStatus();
235 }
236
242 template <typename TIter>
243 void readNoStatus(TIter& iter)
244 {
245 BaseImpl::readNoStatus(iter);
246 }
247
249 bool canWrite() const
250 {
251 return BaseImpl::canWrite();
252 }
253
259 template <typename TIter>
260 ErrorStatus write(TIter& iter, std::size_t size) const
261 {
262 return BaseImpl::write(iter, size);
263 }
264
267 static constexpr bool hasWriteNoStatus()
268 {
269 return BaseImpl::hasWriteNoStatus();
270 }
271
277 template <typename TIter>
278 void writeNoStatus(TIter& iter) const
279 {
280 BaseImpl::writeNoStatus(iter);
281 }
282
284 static constexpr bool isVersionDependent()
285 {
286 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
287 }
288
290 static constexpr bool hasNonDefaultRefresh()
291 {
292 return BaseImpl::hasNonDefaultRefresh();
293 }
294
298 {
299 return BaseImpl::getVersion();
300 }
301
305 {
306 return BaseImpl::setVersion(version);
307 }
308
309protected:
310 using BaseImpl::readData;
311 using BaseImpl::writeData;
312
313private:
314 static_assert(!ParsedOptions::HasVarLengthLimits,
315 "comms::option::def::VarLength option is not applicable to FloatValue field");
316 static_assert(!ParsedOptions::HasAvailableLengthLimit,
317 "comms::option::def::AvailableLengthLimit option is not applicable to FloatValue field");
318 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
319 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to FloatValue field");
320 static_assert(!ParsedOptions::HasSequenceSizeForcing,
321 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to FloatValue field");
322 static_assert(!ParsedOptions::HasSequenceLengthForcing,
323 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to FloatValue field");
324 static_assert(!ParsedOptions::HasSequenceFixedSize,
325 "comms::option::def::SequenceFixedSize option is not applicable to FloatValue field");
326 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
327 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to FloatValue field");
328 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
329 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to FloatValue field");
330 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
331 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to FloatValue field");
332 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
333 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to FloatValue field");
334 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
335 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to FloatValue field");
336 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
337 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to FloatValue field");
338 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
339 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to FloatValue field");
340 static_assert(!ParsedOptions::HasFixedSizeStorage,
341 "comms::option::app::FixedSizeStorage option is not applicable to FloatValue field");
342 static_assert(!ParsedOptions::HasCustomStorageType,
343 "comms::option::app::CustomStorageType option is not applicable to FloatValue field");
344 static_assert(!ParsedOptions::HasOrigDataView,
345 "comms::option::app::OrigDataView option is not applicable to FloatValue field");
346 static_assert(!ParsedOptions::HasVersionsRange,
347 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to FloatValue field");
348 static_assert(!ParsedOptions::HasMissingOnReadFail,
349 "comms::option::def::MissingOnReadFail option is not applicable to FloatValue field");
350 static_assert(!ParsedOptions::HasMissingOnInvalid,
351 "comms::option::def::MissingOnInvalid option is not applicable to FloatValue field");
352};
353
359template <typename TFieldBase, typename T, typename... TOptions>
362 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
363{
364 return field1.value() == field2.value();
365}
366
372template <typename TFieldBase, typename T, typename... TOptions>
375 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
376{
377 return field1.value() != field2.value();
378}
379
385template <typename TFieldBase, typename T, typename... TOptions>
388 const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
389{
390 return field1.value() < field2.value();
391}
392
398template <typename T>
399constexpr bool isFloatValue()
400{
401 return std::is_same<typename T::CommsTag, tag::Float>::value;
402}
403
407template <typename TFieldBase, typename T, typename... TOptions>
408inline
409FloatValue<TFieldBase, T, TOptions...>&
411{
412 return field;
413}
414
418template <typename TFieldBase, typename T, typename... TOptions>
419inline
420const FloatValue<TFieldBase, T, TOptions...>&
422{
423 return field;
424}
425
426} // namespace field
427
428} // namespace comms
429
430
431
This file contain definition of error statuses used by comms module.
Field that represent floating point value.
Definition FloatValue.h:64
bool setVersion(VersionType version)
Default implementation of version update.
Definition FloatValue.h:304
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition FloatValue.h:92
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition FloatValue.h:290
typename BaseImpl::ValueType ValueType
Type of underlying floating point value.
Definition FloatValue.h:84
constexpr bool isFloatValue()
Compile time check function of whether a provided type is any variant of comms::field::FloatValue.
Definition FloatValue.h:399
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition FloatValue.h:80
bool valid() const
Check validity of the field value.
Definition FloatValue.h:207
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:410
bool operator!=(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition FloatValue.h:373
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition FloatValue.h:153
ValueType & value()
Get access to floating point value storage.
Definition FloatValue.h:165
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition FloatValue.h:232
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition FloatValue.h:88
bool operator==(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition FloatValue.h:360
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition FloatValue.h:71
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition FloatValue.h:125
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition FloatValue.h:139
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition FloatValue.h:267
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition FloatValue.h:260
const ValueType & value() const
Get access to floating point value storage.
Definition FloatValue.h:159
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition FloatValue.h:225
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition FloatValue.h:243
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition FloatValue.h:146
bool refresh()
Refresh the field's value.
Definition FloatValue.h:214
void setValue(U &&val)
Set value.
Definition FloatValue.h:180
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition FloatValue.h:77
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition FloatValue.h:278
FloatValue()=default
Default constructor.
const ValueType & getValue() const
Get value.
Definition FloatValue.h:172
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition FloatValue.h:111
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition FloatValue.h:187
FloatValue(const ValueType &val)
Constructor.
Definition FloatValue.h:104
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition FloatValue.h:194
typename BaseImpl::VersionType VersionType
Version type.
Definition FloatValue.h:74
VersionType getVersion() const
Get version of the field.
Definition FloatValue.h:297
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition FloatValue.h:68
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition FloatValue.h:284
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition FloatValue.h:201
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:132
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition FloatValue.h:118
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition FloatValue.h:249
bool operator<(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition FloatValue.h:386
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:421
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition FloatValue.h:97
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.