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