cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsIntFieldImpl.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/ToolsIntField.h"
22
23#include "comms/field/IntValue.h"
24
25#include <cstdint>
26#include <cassert>
27#include <memory>
28#include <type_traits>
29
30namespace cc_tools_qt
31{
32
33namespace details
34{
35
36template <typename TField>
37class ToolsIntFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsIntField, TField>
38{
39 using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsIntField, TField>;
40 using Field = TField;
41 static_assert(comms::field::isIntValue<Field>(), "Must be of comms::field::IntValue type");
42
43public:
44
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 ToolsIntFieldImpl(Field& fieldRef)
51 : Base(fieldRef)
52 {
53 }
54
55 ToolsIntFieldImpl(const ToolsIntFieldImpl&) = default;
56 ToolsIntFieldImpl(ToolsIntFieldImpl&&) = default;
57 virtual ~ToolsIntFieldImpl() noexcept = default;
58
59 ToolsIntFieldImpl& operator=(const ToolsIntFieldImpl&) = delete;
60
61protected:
62 virtual UnderlyingType minValueImpl() const override
63 {
64 return std::numeric_limits<typename Field::ValueType>::min();
65 }
66
67 virtual UnderlyingType maxValueImpl() const override
68 {
69 return std::numeric_limits<typename Field::ValueType>::max();
70 }
71
72 virtual double getScaledImpl() const override
73 {
74 return Base::field().template scaleAs<double>();
75 }
76
77 virtual void setScaledImpl(double value) override
78 {
79 using Tag =
80 std::conditional_t<
81 Field::hasFixedValue(),
82 NoFeatureTag,
83 HasFeatureTag
84 >;
85
86 setScaledInternal(value, Tag());
87 }
88
89 virtual double scaleValueImpl(UnderlyingType value) const override
90 {
91 using Tag =
92 std::conditional_t<
93 Field::hasFixedValue(),
94 NoFeatureTag,
95 HasFeatureTag
96 >;
97 return scaleValueInternal(value, Tag());
98 }
99
100 virtual bool isSignedImpl() const override
101 {
102 return std::is_signed<typename Field::ValueType>::value;
103 }
104
105 virtual std::size_t valueTypeSizeImpl() const override
106 {
107 return sizeof(typename Field::ValueType);
108 }
109
110 virtual Ptr cloneImpl() override
111 {
112 return ActPtr(new ToolsIntFieldImpl<TField>(Base::field()));
113 }
114
115 virtual const SpecialsList& specialsImpl() const override
116 {
117 using Tag =
118 std::conditional_t<
119 Field::hasSpecials(),
120 HasFeatureTag,
121 NoFeatureTag
122 >;
123
124 return specialsInternal(Tag());
125 }
126
127 virtual int scaledDecimalsImpl() const override
128 {
129 using Tag =
130 std::conditional_t<
131 Field::hasScaling(),
132 HasFeatureTag,
133 NoFeatureTag
134 >;
135
136 return scaledDecimalsInternal(Tag());
137 }
138
139 virtual UnderlyingType getDisplayValueImpl() const override
140 {
141 return static_cast<UnderlyingType>(Base::field().getDisplayValue());
142 }
143
144 virtual void setDisplayValueImpl(UnderlyingType value) override
145 {
146 using Tag =
147 std::conditional_t<
148 Field::hasFixedValue(),
149 NoFeatureTag,
150 HasFeatureTag
151 >;
152
153 setDisplayValueInternal(value, Tag());
154 }
155
156private:
157 struct HasFeatureTag{};
158 struct NoFeatureTag{};
159
160 static const SpecialsList& specialsInternal(HasFeatureTag)
161 {
162 static const SpecialsList List = createSpecialsList();
163 return List;
164 }
165
166 static const SpecialsList& specialsInternal(NoFeatureTag)
167 {
168 static const SpecialsList List;
169 return List;
170 }
171
172 static int scaledDecimalsInternal(HasFeatureTag)
173 {
174 return Field::displayDecimals();
175 }
176
177 static int scaledDecimalsInternal(NoFeatureTag)
178 {
179 return 0;
180 }
181
182 static SpecialsList createSpecialsList()
183 {
184 SpecialsList result;
185 auto mapInfo = Field::specialNamesMap();
186 for (auto idx = 0U; idx < mapInfo.second; ++idx) {
187 auto& sInfo = mapInfo.first[idx];
188 result.append(qMakePair(sInfo.second, static_cast<UnderlyingType>(sInfo.first)));
189 }
190
191 return result;
192 }
193
194 void setScaledInternal(double value, HasFeatureTag)
195 {
196 Base::field().setScaled(value);
197 }
198
199 void setScaledInternal([[maybe_unused]] double value, NoFeatureTag)
200 {
201 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
202 assert(Must_not_be_called);
203 }
204
205 static double scaleValueInternal(UnderlyingType value, HasFeatureTag)
206 {
207 Field fieldTmp;
208 fieldTmp.setValue(value);
209 return fieldTmp.template scaleAs<double>();
210 }
211
212 static double scaleValueInternal([[maybe_unused]] UnderlyingType value, NoFeatureTag)
213 {
214 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
215 assert(Must_not_be_called);
216 return 0.0;
217 }
218
219 void setDisplayValueInternal(UnderlyingType value, HasFeatureTag)
220 {
221 Base::field().setDisplayValue(value);
222 }
223
224 void setDisplayValueInternal([[maybe_unused]] UnderlyingType value, NoFeatureTag)
225 {
226 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
227 assert(Must_not_be_called);
228 }
229
230};
231
232template <typename TField>
233auto makeIntField(TField& field)
234{
235 return std::make_unique<ToolsIntFieldImpl<TField>>(field);
236}
237
238} // namespace details
239
240} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.