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