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