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