cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsFloatFieldImpl.h
1//
2// Copyright 2014 - 2025 (C). Alex Robenko. All rights reserved.
3//
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19#pragma once
20
21#include "cc_tools_qt/details/ToolsNumericFieldImpl.h"
22#include "cc_tools_qt/field/ToolsFloatField.h"
23
24#include "comms/field/FloatValue.h"
25
26#include <cstdint>
27#include <cassert>
28#include <cmath>
29#include <limits>
30
31namespace cc_tools_qt
32{
33
34namespace details
35{
36
37template <typename TField>
38class ToolsFloatFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsFloatField, TField>
39{
40 using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsFloatField, TField>;
41 using Field = TField;
42 static_assert(comms::field::isFloatValue<Field>(), "Must be of FloatValueField type");
43
44public:
45 using UnderlyingType = typename Base::UnderlyingType;
46 using Ptr = typename Base::Ptr;
47 using ActPtr = typename Base::ActPtr;
48 using SpecialsList = typename Base::SpecialsList;
49
50 explicit ToolsFloatFieldImpl(Field& fieldRef)
51 : Base(fieldRef)
52 {
53 }
54
55 ToolsFloatFieldImpl(const ToolsFloatFieldImpl&) = default;
56 ToolsFloatFieldImpl(ToolsFloatFieldImpl&&) = default;
57 virtual ~ToolsFloatFieldImpl() noexcept = default;
58
59 ToolsFloatFieldImpl& operator=(const ToolsFloatFieldImpl&) = delete;
60
61protected:
62 virtual Ptr cloneImpl() override
63 {
64 return ActPtr(new ToolsFloatFieldImpl<TField>(Base::field()));
65 }
66
67 virtual bool isNanImpl() const override
68 {
69 return std::isnan(Base::field().getValue());
70 }
71
72 virtual void setNanImpl() override
73 {
74 setFieldValueInternal(std::numeric_limits<typename TField::ValueType>::quiet_NaN());
75 }
76
77 virtual bool isInfImpl() const override
78 {
79 return std::isinf(Base::field().getValue()) && (0 < Base::field().getValue());
80 }
81
82 virtual void setInfImpl() override
83 {
84 setFieldValueInternal(std::numeric_limits<typename TField::ValueType>::infinity());
85 }
86
87 virtual bool isMinusInfImpl() const override
88 {
89 return std::isinf(Base::field().getValue()) && (Base::field().getValue() < 0);
90 }
91
92 virtual void setMinusInfImpl() override
93 {
94 setFieldValueInternal(-std::numeric_limits<typename TField::ValueType>::infinity());
95 }
96
97 virtual double getEpsilonImpl() const override
98 {
99 return static_cast<double>(std::numeric_limits<typename TField::ValueType>::epsilon());
100 }
101
102 virtual const SpecialsList& specialsImpl() const override
103 {
104 using Tag =
105 std::conditional_t<
106 Field::hasSpecials(),
107 HasFeatureTag,
108 NoFeatureTag
109 >;
110
111 return specialsInternal(Tag());
112 }
113
114 virtual int decimalsImpl() const override
115 {
116 return Field::displayDecimals();
117 }
118
119private:
120 struct HasFeatureTag{};
121 struct NoFeatureTag{};
122
123 void setFieldValueInternal(typename TField::ValueType val)
124 {
125 using Tag =
126 std::conditional_t<
127 Field::hasFixedValue(),
128 NoFeatureTag,
129 HasFeatureTag
130 >;
131
132 setFieldValueInternal(val, Tag());
133 }
134
135 void setFieldValueInternal(typename TField::ValueType val, HasFeatureTag)
136 {
137 Base::field().setValue(val);
138 }
139
140 void setFieldValueInternal([[maybe_unused]] typename TField::ValueType val, NoFeatureTag)
141 {
142 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
143 assert(Must_not_be_called);
144 }
145
146 static const SpecialsList& specialsInternal(HasFeatureTag)
147 {
148 static const SpecialsList List = createSpecialsList();
149 return List;
150 }
151
152 static const SpecialsList& specialsInternal(NoFeatureTag)
153 {
154 static const SpecialsList List;
155 return List;
156 }
157
158 static SpecialsList createSpecialsList()
159 {
160 SpecialsList result;
161 auto mapInfo = Field::specialNamesMap();
162 for (auto idx = 0U; idx < mapInfo.second; ++idx) {
163 auto& sInfo = mapInfo.first[idx];
164 result.append(qMakePair(sInfo.second, static_cast<UnderlyingType>(sInfo.first)));
165 }
166
167 return result;
168 }
169};
170
171template <typename TField>
172auto makeFloatField(TField& field)
173{
174 return std::make_unique<ToolsFloatFieldImpl<TField>>(field);
175}
176
177} // namespace details
178
179} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.