cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsStringFieldImpl.h
1//
2// Copyright 2015 - 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/field/ToolsStringField.h"
23
24#include "comms/field/String.h"
25
26#include <cstdint>
27#include <cassert>
28#include <limits>
29#include <memory>
30#include <type_traits>
31
32namespace cc_tools_qt
33{
34
35namespace details
36{
37
38template <typename TField>
39class ToolsStringFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsStringField, TField>
40{
41 using Base = ToolsFieldBase<cc_tools_qt::field::ToolsStringField, TField>;
42 using Field = TField;
43
44public:
45 using SerialisedSeq = typename Base::SerialisedSeq;
46 using Ptr = typename Base::Ptr;
47 using ActPtr = typename Base::ActPtr;
48
49 explicit ToolsStringFieldImpl(Field& fieldRef)
50 : Base(fieldRef)
51 {
52 }
53
54 ToolsStringFieldImpl(const ToolsStringFieldImpl&) = default;
55 ToolsStringFieldImpl(ToolsStringFieldImpl&&) = default;
56 virtual ~ToolsStringFieldImpl() noexcept = default;
57
58 ToolsStringFieldImpl& operator=(const ToolsStringFieldImpl&) = delete;
59
60protected:
61
62 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 virtual int maxSizeImpl() const override
88 {
89 return maxSizeInternal(SizeExistanceTag());
90 }
91
92 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 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 static int maxSizeInternal(SizeFieldExistsTag)
129 {
130 using SizeField = typename Field::SizeFieldPrefix;
131 return maxSizeByPrefix<SizeField>();
132 }
133
134 static int maxSizeInternal(SerLengthFieldExistsTag)
135 {
136 using LengthField = typename Field::SerLengthFieldPrefix;
137 return maxSizeByPrefix<LengthField>();
138 }
139
140 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>
162auto makeStringField(TField& field)
163{
164 return std::make_unique<ToolsStringFieldImpl<TField>>(field);
165}
166
167} // namespace details
168
169} // namespace cc_tools_qt
170
171
172
Main namespace for all classes / functions of the shared library.