cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsNumericFieldImpl.h
1//
2// Copyright 2014 - 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/ToolsFieldBase.h"
23#include "cc_tools_qt/details/ToolsNumericFieldBase.h"
24
25#include <cassert>
26#include <cstddef>
27#include <type_traits>
28
29namespace cc_tools_qt
30{
31
32namespace details
33{
34
35template <typename TBase, typename TField>
36class ToolsNumericFieldImpl : public ToolsFieldBase<TBase, TField>
37{
38 using Base = ToolsFieldBase<TBase, TField>;
39
40public:
41 using UnderlyingType = typename Base::UnderlyingType;
42 using SerialisedSeq = typename Base::SerialisedSeq;
43
44protected:
45 using Field = TField;
46 using ValueType = typename Field::ValueType;
47
48 static_assert(sizeof(ValueType) <= sizeof(UnderlyingType), "This class cannot handle provided field.");
49// static_assert(
50// std::is_signed<ValueType>::value == std::is_signed<UnderlyingType>::value ||
51// (sizeof(ValueType) < sizeof(UnderlyingType)),
52// "This class cannot handle provided field.");
53
54 explicit ToolsNumericFieldImpl(Field& fieldRef)
55 : Base(fieldRef)
56 {
57 static_assert(
58 std::is_base_of_v<ToolsNumericFieldBase<UnderlyingType>, ToolsNumericFieldImpl<TBase, TField>>,
59 "Must inherit from ToolsNumericFieldImpl");
60 }
61
62 ToolsNumericFieldImpl(const ToolsNumericFieldImpl&) = default;
63 ToolsNumericFieldImpl(ToolsNumericFieldImpl&&) = default;
64
65 ToolsNumericFieldImpl& operator=(const ToolsNumericFieldImpl&) = delete;
66
67 virtual ~ToolsNumericFieldImpl() noexcept = default;
68
69 virtual UnderlyingType getValueImpl() const override
70 {
71 return static_cast<UnderlyingType>(Base::field().getValue());
72 }
73
74 virtual void setValueImpl(UnderlyingType value) override
75 {
76 using Tag =
77 std::conditional_t<
78 Field::hasFixedValue(),
79 NoFeatureTag,
80 HasFeatureTag
81 >;
82
83 setValueInternal(value, Tag());
84 }
85
86 virtual std::size_t minLengthImpl() const override
87 {
88 return Base::field().minLength();
89 }
90
91 virtual std::size_t maxLengthImpl() const override
92 {
93 return Base::field().maxLength();
94 }
95
96private:
97 struct HasFeatureTag {};
98 struct NoFeatureTag {};
99
100 void setValueInternal(UnderlyingType value, HasFeatureTag)
101 {
102 Base::field().setValue(value);
103 }
104
105 void setValueInternal([[maybe_unused]] UnderlyingType value, NoFeatureTag)
106 {
107 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
108 assert(Must_not_be_called);
109 }
110};
111
112} // namespace details
113
114} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.