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