COMMS
Template library intended to help with implementation of communication protocols.
FloatValue.h
Go to the documentation of this file.
1 //
2 // Copyright 2015 - 2024 (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 
19 namespace comms
20 {
21 
22 namespace field
23 {
24 
58 template <typename TFieldBase, typename T, typename... TOptions>
59 class FloatValue : public details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>
60 {
61  using BaseImpl = details::AdaptBasicFieldT<basic::FloatValue<TFieldBase, T>, TOptions...>;
62 public:
64  using FieldBase = TFieldBase;
65 
67  using Endian = typename BaseImpl::Endian;
68 
71 
73  using ParsedOptions = details::OptionsParser<TOptions...>;
74 
76  using CommsTag = typename BaseImpl::CommsTag;
77 
80  using ValueType = typename BaseImpl::ValueType;
81 
84  using UnitsType = typename ParsedOptions::UnitsType;
85 
88  using UnitsRatio = typename ParsedOptions::UnitsRatio;
89 
94 
97  FloatValue() = default;
98 
100  explicit FloatValue(const ValueType& val)
101  : BaseImpl(val)
102  {
103  }
104 
107  static constexpr bool hasFailOnInvalid()
108  {
109  return ParsedOptions::HasFailOnInvalid;
110  }
111 
114  static constexpr bool hasIgnoreInvalid()
115  {
116  return ParsedOptions::HasIgnoreInvalid;
117  }
118 
121  static constexpr bool hasEmptySerialization()
122  {
123  return ParsedOptions::HasEmptySerialization;
124  }
125 
128  static constexpr bool hasUnits()
129  {
130  return ParsedOptions::HasUnits;
131  }
132 
135  static constexpr bool hasFieldType()
136  {
137  return ParsedOptions::HasFieldType;
138  }
139 
141  const ValueType& value() const
142  {
143  return BaseImpl::value();
144  }
145 
148  {
149  return BaseImpl::value();
150  }
151 
154  const ValueType& getValue() const
155  {
156  return BaseImpl::getValue();
157  }
158 
161  template <typename U>
162  void setValue(U&& val)
163  {
164  BaseImpl::setValue(std::forward<U>(val));
165  }
166 
169  constexpr std::size_t length() const
170  {
171  return BaseImpl::length();
172  }
173 
176  static constexpr std::size_t minLength()
177  {
178  return BaseImpl::minLength();
179  }
180 
183  static constexpr std::size_t maxLength()
184  {
185  return BaseImpl::maxLength();
186  }
187 
189  bool valid() const
190  {
191  return BaseImpl::valid();
192  }
193 
196  bool refresh()
197  {
198  return BaseImpl::refresh();
199  }
200 
206  template <typename TIter>
207  ErrorStatus read(TIter& iter, std::size_t size)
208  {
209  return BaseImpl::read(iter, size);
210  }
211 
214  static constexpr bool hasReadNoStatus()
215  {
216  return BaseImpl::hasReadNoStatus();
217  }
218 
224  template <typename TIter>
225  void readNoStatus(TIter& iter)
226  {
227  BaseImpl::readNoStatus(iter);
228  }
229 
231  bool canWrite() const
232  {
233  return BaseImpl::canWrite();
234  }
235 
241  template <typename TIter>
242  ErrorStatus write(TIter& iter, std::size_t size) const
243  {
244  return BaseImpl::write(iter, size);
245  }
246 
249  static constexpr bool hasWriteNoStatus()
250  {
251  return BaseImpl::hasWriteNoStatus();
252  }
253 
259  template <typename TIter>
260  void writeNoStatus(TIter& iter) const
261  {
262  BaseImpl::writeNoStatus(iter);
263  }
264 
266  static constexpr bool isVersionDependent()
267  {
268  return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
269  }
270 
272  static constexpr bool hasNonDefaultRefresh()
273  {
274  return BaseImpl::hasNonDefaultRefresh();
275  }
276 
280  {
281  return BaseImpl::getVersion();
282  }
283 
287  {
288  return BaseImpl::setVersion(version);
289  }
290 
291 protected:
292  using BaseImpl::readData;
293  using BaseImpl::writeData;
294 
295 private:
296  static_assert(!ParsedOptions::HasVarLengthLimits,
297  "comms::option::def::VarLength option is not applicable to FloatValue field");
298  static_assert(!ParsedOptions::HasAvailableLengthLimit,
299  "comms::option::def::AvailableLengthLimit option is not applicable to FloatValue field");
300  static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
301  "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to FloatValue field");
302  static_assert(!ParsedOptions::HasSequenceSizeForcing,
303  "comms::option::def::SequenceSizeForcingEnabled option is not applicable to FloatValue field");
304  static_assert(!ParsedOptions::HasSequenceLengthForcing,
305  "comms::option::def::SequenceLengthForcingEnabled option is not applicable to FloatValue field");
306  static_assert(!ParsedOptions::HasSequenceFixedSize,
307  "comms::option::def::SequenceFixedSize option is not applicable to FloatValue field");
308  static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
309  "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to FloatValue field");
310  static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
311  "comms::option::def::SequenceSizeFieldPrefix option is not applicable to FloatValue field");
312  static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
313  "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to FloatValue field");
314  static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
315  "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to FloatValue field");
316  static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
317  "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to FloatValue field");
318  static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
319  "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to FloatValue field");
320  static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
321  "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to FloatValue field");
322  static_assert(!ParsedOptions::HasFixedSizeStorage,
323  "comms::option::app::FixedSizeStorage option is not applicable to FloatValue field");
324  static_assert(!ParsedOptions::HasCustomStorageType,
325  "comms::option::app::CustomStorageType option is not applicable to FloatValue field");
326  static_assert(!ParsedOptions::HasOrigDataView,
327  "comms::option::app::OrigDataView option is not applicable to FloatValue field");
328  static_assert(!ParsedOptions::HasVersionsRange,
329  "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to FloatValue field");
330  static_assert(!ParsedOptions::HasMissingOnReadFail,
331  "comms::option::def::MissingOnReadFail option is not applicable to FloatValue field");
332  static_assert(!ParsedOptions::HasMissingOnInvalid,
333  "comms::option::def::MissingOnInvalid option is not applicable to FloatValue field");
334 };
335 
341 template <typename TFieldBase, typename T, typename... TOptions>
344  const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
345 {
346  return field1.value() == field2.value();
347 }
348 
354 template <typename TFieldBase, typename T, typename... TOptions>
357  const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
358 {
359  return field1.value() != field2.value();
360 }
361 
367 template <typename TFieldBase, typename T, typename... TOptions>
370  const FloatValue<TFieldBase, T, TOptions...>& field2) noexcept
371 {
372  return field1.value() < field2.value();
373 }
374 
380 template <typename T>
381 constexpr bool isFloatValue()
382 {
383  return std::is_same<typename T::CommsTag, tag::Float>::value;
384 }
385 
389 template <typename TFieldBase, typename T, typename... TOptions>
390 inline
391 FloatValue<TFieldBase, T, TOptions...>&
393 {
394  return field;
395 }
396 
400 template <typename TFieldBase, typename T, typename... TOptions>
401 inline
402 const FloatValue<TFieldBase, T, TOptions...>&
404 {
405  return field;
406 }
407 
408 } // namespace field
409 
410 } // namespace comms
411 
412 
413 
This file contain definition of error statuses used by comms module.
Field that represent floating point value.
Definition: FloatValue.h:60
bool setVersion(VersionType version)
Default implementation of version update.
Definition: FloatValue.h:286
typename ParsedOptions::UnitsRatio UnitsRatio
Scaling ratio determined by the forced units via the comms::option::def::Units* option.
Definition: FloatValue.h:88
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition: FloatValue.h:272
typename BaseImpl::ValueType ValueType
Type of underlying floating point value.
Definition: FloatValue.h:80
constexpr bool isFloatValue()
Compile time check function of whether a provided type is any variant of comms::field::FloatValue.
Definition: FloatValue.h:381
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition: FloatValue.h:76
bool valid() const
Check validity of the field value.
Definition: FloatValue.h:189
const ValueType & getValue() const
Get value.
Definition: FloatValue.h:154
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:392
const ValueType & value() const
Get access to floating point value storage.
Definition: FloatValue.h:141
ValueType & value()
Get access to floating point value storage.
Definition: FloatValue.h:147
bool operator!=(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition: FloatValue.h:355
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition: FloatValue.h:214
typename ParsedOptions::UnitsType UnitsType
Units type defined by any of the comms::option::def::Units* option.
Definition: FloatValue.h:84
bool operator==(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equality comparison operator.
Definition: FloatValue.h:342
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition: FloatValue.h:67
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition: FloatValue.h:121
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition: FloatValue.h:135
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition: FloatValue.h:249
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition: FloatValue.h:242
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition: FloatValue.h:207
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition: FloatValue.h:225
bool refresh()
Refresh the field's value.
Definition: FloatValue.h:196
void setValue(U &&val)
Set value.
Definition: FloatValue.h:162
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition: FloatValue.h:73
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition: FloatValue.h:260
FloatValue()=default
Default constructor.
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition: FloatValue.h:107
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition: FloatValue.h:169
FloatValue(const ValueType &val)
Constructor.
Definition: FloatValue.h:100
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition: FloatValue.h:176
typename BaseImpl::VersionType VersionType
Version type.
Definition: FloatValue.h:70
VersionType getVersion() const
Get version of the field.
Definition: FloatValue.h:279
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition: FloatValue.h:64
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition: FloatValue.h:266
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition: FloatValue.h:183
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:128
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition: FloatValue.h:114
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition: FloatValue.h:231
bool operator<(const FloatValue< TFieldBase, T, TOptions... > &field1, const FloatValue< TFieldBase, T, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition: FloatValue.h:368
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:403
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition: FloatValue.h:93
Contains definition of various tag classes.
comms::option::def::HasCustomVersionUpdate HasCustomVersionUpdate
Same as comms::option::def::HasCustomVersionUpdate.
Definition: options.h:1800
comms::option::def::VersionType< T > VersionType
Same as comms::option::def::VersionType.
Definition: options.h:1797
comms::option::def::FieldType< TMsg > FieldType
Same as comms::option::def::FieldType.
Definition: options.h:1463
comms::option::def::Endian< TEndian > Endian
Same as comms::option::def::Endian.
Definition: options.h:1438
T readData(TIter &iter, const traits::endian::Big &endian)
Same as readBig<T, TIter>()
Definition: access.h:766
void writeData(T value, TIter &iter, const traits::endian::Big &endian)
Same as writeBig<T, TIter>()
Definition: access.h:698
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.