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