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