cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsStringFieldImpl.h
1//
2// Copyright 2015 - 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/field/ToolsStringField.h"
24
25#include "comms/field/String.h"
26
27#include <cstdint>
28#include <cassert>
29#include <limits>
30#include <memory>
31#include <type_traits>
32
33namespace cc_tools_qt
34{
35
36namespace details
37{
38
39template <typename TField>
40class ToolsStringFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsStringField, TField>
41{
42 using Base = ToolsFieldBase<cc_tools_qt::field::ToolsStringField, TField>;
43 using Field = TField;
44
45public:
46 using SerialisedSeq = typename Base::SerialisedSeq;
47 using Ptr = typename Base::Ptr;
48 using ActPtr = typename Base::ActPtr;
49
50 explicit ToolsStringFieldImpl(Field& fieldRef)
51 : Base(fieldRef)
52 {
53 }
54
55 ToolsStringFieldImpl(const ToolsStringFieldImpl&) = default;
56 ToolsStringFieldImpl(ToolsStringFieldImpl&&) = default;
57 virtual ~ToolsStringFieldImpl() noexcept = default;
58
59 ToolsStringFieldImpl& operator=(const ToolsStringFieldImpl&) = delete;
60
61protected:
62 [[nodiscard]] virtual QString getValueImpl() const override
63 {
64 auto& strField = Base::field();
65 return QString::fromUtf8(strField.getValue().c_str(), static_cast<int>(strField.getValue().size()));
66 }
67
68 virtual void setValueImpl(const QString& val) override
69 {
70 using Tag =
71 std::conditional_t<
72 Field::hasFixedValue(),
73 NoFeatureTag,
74 HasFeatureTag
75 >;
76
77 setValueInternal(val, Tag());
78 }
79
80 virtual bool setSerialisedValueImpl([[maybe_unused]] const SerialisedSeq& value) override
81 {
82 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
83 assert(Must_not_be_called);
84 return false;
85 }
86
87 [[nodiscard]] virtual int maxSizeImpl() const override
88 {
89 return maxSizeInternal(SizeExistanceTag());
90 }
91
92 [[nodiscard]] virtual Ptr cloneImpl() override
93 {
94 return ActPtr(new ToolsStringFieldImpl<TField>(Base::field()));
95 }
96
97private:
98 struct SizeFieldExistsTag {};
99 struct SerLengthFieldExistsTag {};
100 struct NoSizeFieldTag {};
101 struct HasFeatureTag {};
102 struct NoFeatureTag {};
103
104 using SizeExistanceTag =
105 std::conditional_t<
106 Field::hasSizeFieldPrefix(),
107 SizeFieldExistsTag,
108 std::conditional_t<
109 Field::hasSerLengthFieldPrefix(),
110 SerLengthFieldExistsTag,
111 NoSizeFieldTag
112 >
113 >;
114
115 template <typename TPrefixField>
116 [[nodiscard]] static int maxSizeByPrefix()
117 {
118 if (sizeof(int) <= TPrefixField::maxLength()) {
119 return std::numeric_limits<int>::max();
120 }
121
122 auto shift =
123 TPrefixField::maxLength() * std::numeric_limits<std::uint8_t>::digits;
124
125 return static_cast<int>((1U << shift) - 1);
126 }
127
128 [[nodiscard]] static int maxSizeInternal(SizeFieldExistsTag)
129 {
130 using SizeField = typename Field::SizeFieldPrefix;
131 return maxSizeByPrefix<SizeField>();
132 }
133
134 [[nodiscard]] static int maxSizeInternal(SerLengthFieldExistsTag)
135 {
136 using LengthField = typename Field::SerLengthFieldPrefix;
137 return maxSizeByPrefix<LengthField>();
138 }
139
140 [[nodiscard]] int maxSizeInternal(NoSizeFieldTag) const
141 {
142 return
143 static_cast<int>(
144 std::min(
145 static_cast<std::size_t>(std::numeric_limits<int>::max()),
146 Base::field().getValue().max_size()));
147 }
148
149 void setValueInternal(const QString& val, HasFeatureTag)
150 {
151 Base::field().setValue(val.toStdString().c_str());
152 }
153
154 void setValueInternal([[maybe_unused]] const QString& val, NoFeatureTag)
155 {
156 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
157 assert(Must_not_be_called);
158 }
159};
160
161template <typename TField>
162[[nodiscard]] auto makeStringField(TField& field)
163{
164 return std::make_unique<ToolsStringFieldImpl<TField>>(field);
165}
166
167} // namespace details
168
169} // namespace cc_tools_qt
170
Main namespace for all classes / functions of the shared library.