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#pragma once
19
20#include "cc_tools_qt/details/ToolsNumericFieldImpl.h"
21#include "cc_tools_qt/field/ToolsFloatField.h"
22
23#include "comms/field/FloatValue.h"
24
25#include <cstdint>
26#include <cassert>
27#include <cmath>
28#include <limits>
29
30namespace cc_tools_qt
31{
32
33namespace details
34{
35
36template <typename TField>
37class ToolsFloatFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsFloatField, TField>
38{
39 using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsFloatField, TField>;
40 using Field = TField;
41 static_assert(comms::field::isFloatValue<Field>(), "Must be of FloatValueField type");
42
43public:
44 using UnderlyingType = typename Base::UnderlyingType;
45 using Ptr = typename Base::Ptr;
46 using ActPtr = typename Base::ActPtr;
47 using SpecialsList = typename Base::SpecialsList;
48
49 explicit ToolsFloatFieldImpl(Field& fieldRef)
50 : Base(fieldRef)
51 {
52 }
53
54 ToolsFloatFieldImpl(const ToolsFloatFieldImpl&) = default;
55 ToolsFloatFieldImpl(ToolsFloatFieldImpl&&) = default;
56 virtual ~ToolsFloatFieldImpl() noexcept = default;
57
58 ToolsFloatFieldImpl& operator=(const ToolsFloatFieldImpl&) = delete;
59
60protected:
61 virtual Ptr cloneImpl() override
62 {
63 return ActPtr(new ToolsFloatFieldImpl<TField>(Base::field()));
64 }
65
66 virtual bool isNanImpl() const override
67 {
68 return std::isnan(Base::field().getValue());
69 }
70
71 virtual void setNanImpl() override
72 {
73 setFieldValueInternal(std::numeric_limits<typename TField::ValueType>::quiet_NaN());
74 }
75
76 virtual bool isInfImpl() const override
77 {
78 return std::isinf(Base::field().getValue()) && (0 < Base::field().getValue());
79 }
80
81 virtual void setInfImpl() override
82 {
83 setFieldValueInternal(std::numeric_limits<typename TField::ValueType>::infinity());
84 }
85
86 virtual bool isMinusInfImpl() const override
87 {
88 return std::isinf(Base::field().getValue()) && (Base::field().getValue() < 0);
89 }
90
91 virtual void setMinusInfImpl() override
92 {
93 setFieldValueInternal(-std::numeric_limits<typename TField::ValueType>::infinity());
94 }
95
96 virtual double getEpsilonImpl() const override
97 {
98 return static_cast<double>(std::numeric_limits<typename TField::ValueType>::epsilon());
99 }
100
101 virtual const SpecialsList& specialsImpl() const override
102 {
103 using Tag =
104 std::conditional_t<
105 Field::hasSpecials(),
106 HasFeatureTag,
107 NoFeatureTag
108 >;
109
110 return specialsInternal(Tag());
111 }
112
113 virtual int decimalsImpl() const override
114 {
115 return Field::displayDecimals();
116 }
117
118private:
119 struct HasFeatureTag{};
120 struct NoFeatureTag{};
121
122 void setFieldValueInternal(typename TField::ValueType val)
123 {
124 using Tag =
125 std::conditional_t<
126 Field::hasFixedValue(),
127 NoFeatureTag,
128 HasFeatureTag
129 >;
130
131 setFieldValueInternal(val, Tag());
132 }
133
134 void setFieldValueInternal(typename TField::ValueType val, HasFeatureTag)
135 {
136 Base::field().setValue(val);
137 }
138
139 void setFieldValueInternal([[maybe_unused]] typename TField::ValueType val, NoFeatureTag)
140 {
141 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
142 assert(Must_not_be_called);
143 }
144
145 static const SpecialsList& specialsInternal(HasFeatureTag)
146 {
147 static const SpecialsList List = createSpecialsList();
148 return List;
149 }
150
151 static const SpecialsList& specialsInternal(NoFeatureTag)
152 {
153 static const SpecialsList List;
154 return List;
155 }
156
157 static SpecialsList createSpecialsList()
158 {
159 SpecialsList result;
160 auto mapInfo = Field::specialNamesMap();
161 for (auto idx = 0U; idx < mapInfo.second; ++idx) {
162 auto& sInfo = mapInfo.first[idx];
163 result.append(qMakePair(sInfo.second, static_cast<UnderlyingType>(sInfo.first)));
164 }
165
166 return result;
167 }
168};
169
170template <typename TField>
171auto makeFloatField(TField& field)
172{
173 return std::make_unique<ToolsFloatFieldImpl<TField>>(field);
174}
175
176} // namespace details
177
178} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.