COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
FloatValue.h
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
10#pragma once
11
12#include "comms/ErrorStatus.h"
13#include "comms/field/tag.h"
14#include "comms/util/SizeToType.h"
15
16#include <cstddef>
17#include <ratio>
18#include <type_traits>
19#include <utility>
20
21namespace comms
22{
23
24namespace field
25{
26
27namespace basic
28{
29
30template <typename TFieldBase, typename T>
31class FloatValue : public TFieldBase
32{
33 static_assert(std::is_floating_point<T>::value, "T must be floating point value");
34
35 using BaseImpl = TFieldBase;
36public:
37
38 using ValueType = T;
39 using SerialisedType = typename comms::util::SizeToType<sizeof(ValueType), false>::Type;
40 using ScalingRatio = std::ratio<1, 1>;
41 using CommsTag = comms::field::tag::Float;
42
43 FloatValue() = default;
44
45 explicit FloatValue(ValueType val)
46 : m_value(val)
47 {
48 }
49
50 FloatValue(const FloatValue&) = default;
51 FloatValue(FloatValue&&) = default;
52 ~FloatValue() noexcept = default;
53
54 FloatValue& operator=(const FloatValue&) = default;
55 FloatValue& operator=(FloatValue&&) = default;
56
57 const ValueType& value() const
58 {
59 return m_value;
60 }
61
62 ValueType& value()
63 {
64 return m_value;
65 }
66
67 const ValueType& getValue() const
68 {
69 return value();
70 }
71
72 template <typename U>
73 void setValue(U&& val)
74 {
75 value() = static_cast<ValueType>(std::forward<U>(val));
76 }
77
78 static constexpr std::size_t length()
79 {
80 return sizeof(SerialisedType);
81 }
82
83 static constexpr std::size_t minLength()
84 {
85 return length();
86 }
87
88 static constexpr std::size_t maxLength()
89 {
90 return length();
91 }
92
93 static SerialisedType toSerialised(ValueType val)
94 {
95 CastUnion<> castUnion;
96 castUnion.m_value = val;
97 return castUnion.m_serValue;
98 }
99
100 static ValueType fromSerialised(SerialisedType val)
101 {
102 CastUnion<> castUnion;
103 castUnion.m_serValue = val;
104 return castUnion.m_value;
105 }
106
107 template <typename TIter>
108 ErrorStatus read(TIter& iter, std::size_t size)
109 {
110 if (size < length()) {
112 }
113
114 readNoStatus(iter);
116 }
117
118 template <typename TIter>
119 void readNoStatus(TIter& iter)
120 {
121 auto serialisedValue =
122 BaseImpl::template readData<SerialisedType>(iter);
123 m_value = fromSerialised(serialisedValue);
124 }
125
126 template <typename TIter>
127 ErrorStatus write(TIter& iter, std::size_t size) const
128 {
129 if (size < length()) {
131 }
132
133 writeNoStatus(iter);
135 }
136
137 template <typename TIter>
138 void writeNoStatus(TIter& iter) const
139 {
140 BaseImpl::writeData(toSerialised(m_value), iter);
141 }
142
143private:
144 template<typename...>
145 union CastUnion
146 {
147 ValueType m_value;
148 SerialisedType m_serValue;
149 };
150
151 ValueType m_value = static_cast<ValueType>(0.0);
152};
153
154} // namespace basic
155
156} // namespace field
157
158} // namespace comms
159
This file contain definition of error statuses used by comms module.
Contains definition of various tag classes.
comms::option::def::ScalingRatio< TNum, TDenom > ScalingRatio
Same as comms::option::def::ScalingRatio.
Definition options.h:1557
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:19
@ Success
Used to indicate successful outcome of the operation.