cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsUnsignedLongFieldImpl.h
1//
2// Copyright 2017 - 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/ToolsUnsignedLongField.h"
23
24#include "comms/field/IntValue.h"
25
26#include <cstdint>
27#include <cassert>
28#include <limits>
29#include <memory>
30
31namespace cc_tools_qt
32{
33
34namespace details
35{
36
37template <typename TField>
38class ToolsUnsignedLongFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsUnsignedLongField, TField>
39{
40 using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsUnsignedLongField, TField>;
41 using Field = TField;
42 static_assert(comms::field::isIntValue<Field>(), "Must be of IntValueField 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 ToolsUnsignedLongFieldImpl(Field& fieldRef)
51 : Base(fieldRef)
52 {
53 }
54
55 ToolsUnsignedLongFieldImpl(const ToolsUnsignedLongFieldImpl&) = default;
56 ToolsUnsignedLongFieldImpl(ToolsUnsignedLongFieldImpl&&) = default;
57 virtual ~ToolsUnsignedLongFieldImpl() noexcept = default;
58
59 ToolsUnsignedLongFieldImpl& operator=(const ToolsUnsignedLongFieldImpl&) = 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 Base::field().setScaled(value);
80 }
81
82 virtual double scaleValueImpl(UnderlyingType value) const override
83 {
84 Field fieldTmp;
85 fieldTmp.setValue(value);
86 return fieldTmp.template scaleAs<double>();
87 }
88
89 virtual bool isSignedImpl() const override
90 {
91 return std::is_signed<typename Field::ValueType>::value;
92 }
93
94 virtual std::size_t valueTypeSizeImpl() const override
95 {
96 return sizeof(typename Field::ValueType);
97 }
98
99 virtual Ptr cloneImpl() override
100 {
101 return ActPtr(new ToolsUnsignedLongFieldImpl<TField>(Base::field()));
102 }
103
104 virtual const SpecialsList& specialsImpl() const override
105 {
106 using Tag =
107 std::conditional_t<
108 Field::hasSpecials(),
109 HasFeatureTag,
110 NoFeatureTag
111 >;
112
113 return specialsInternal(Tag());
114 }
115
116 virtual int scaledDecimalsImpl() const override
117 {
118 using Tag =
119 std::conditional_t<
120 Field::hasScaling(),
121 HasFeatureTag,
122 NoFeatureTag
123 >;
124
125 return scaledDecimalsInternal(Tag());
126 }
127
128 virtual UnderlyingType getDisplayValueImpl() const override
129 {
130 return static_cast<UnderlyingType>(Base::field().getDisplayValue());
131 }
132
133 virtual void setDisplayValueImpl(UnderlyingType value) override
134 {
135 using Tag =
136 std::conditional_t<
137 Field::hasFixedValue(),
138 NoFeatureTag,
139 HasFeatureTag
140 >;
141
142 setDisplayValueInternal(value, Tag());
143 }
144
145private:
146 struct HasFeatureTag{};
147 struct NoFeatureTag{};
148
149 static const SpecialsList& specialsInternal(HasFeatureTag)
150 {
151 static const SpecialsList List = createSpecialsList();
152 return List;
153 }
154
155 static const SpecialsList& specialsInternal(NoFeatureTag)
156 {
157 static const SpecialsList List;
158 return List;
159 }
160
161 static int scaledDecimalsInternal(HasFeatureTag)
162 {
163 return Field::displayDecimals();
164 }
165
166 static int scaledDecimalsInternal(NoFeatureTag)
167 {
168 return 0;
169 }
170
171 static SpecialsList createSpecialsList()
172 {
173 SpecialsList result;
174 auto mapInfo = Field::specialNamesMap();
175 for (auto idx = 0U; idx < mapInfo.second; ++idx) {
176 auto& sInfo = mapInfo.first[idx];
177 result.append(qMakePair(sInfo.second, static_cast<UnderlyingType>(sInfo.first)));
178 }
179
180 return result;
181 }
182
183 void setDisplayValueInternal(UnderlyingType value, HasFeatureTag)
184 {
185 Base::field().setDisplayValue(value);
186 }
187
188 void setDisplayValueInternal([[maybe_unused]] UnderlyingType value, NoFeatureTag)
189 {
190 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
191 assert(Must_not_be_called);
192 }
193};
194
195template <typename TField>
196auto makeUnsignedLongField(TField& field)
197{
198 return std::make_unique<ToolsUnsignedLongFieldImpl<TField>>(field);
199}
200
201} // namespace details
202
203} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.