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