29#include "comms/comms.h"
31#include "FieldWrapper.h"
36namespace field_wrapper
39class CC_API StringWrapper :
public FieldWrapper
43 typedef std::unique_ptr<StringWrapper> Ptr;
46 virtual ~StringWrapper() noexcept;
48 QString getValue() const;
50 void setValue(const QString& val);
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;
62 void dispatchImpl(FieldWrapperHandler& handler);
65template <typename TField>
66class StringWrapperT : public FieldWrapperT<StringWrapper, TField>
68 using Base = FieldWrapperT<StringWrapper, TField>;
72 using SerialisedSeq =
typename Base::SerialisedSeq;
73 using Ptr =
typename Base::Ptr;
75 explicit StringWrapperT(Field& fieldRef)
80 StringWrapperT(
const StringWrapperT&) =
default;
81 StringWrapperT(StringWrapperT&&) =
default;
82 virtual ~StringWrapperT() noexcept = default;
84 StringWrapperT& operator=(const StringWrapperT&) = delete;
88 virtual QString getValueImpl()
const override
90 auto& strField = Base::field();
91 return QString::fromUtf8(strField.getValue().c_str(),
static_cast<int>(strField.getValue().size()));
94 virtual void setValueImpl(
const QString& val)
override
96 Base::field().setValue(val.toStdString().c_str());
99 virtual bool setSerialisedValueImpl([[maybe_unused]]
const SerialisedSeq& value)
override
101 [[maybe_unused]]
static constexpr bool Must_not_be_called =
false;
102 assert(Must_not_be_called);
106 virtual int maxSizeImpl()
const override
108 return maxSizeInternal(SizeExistanceTag());
111 virtual Ptr cloneImpl()
override
113 return Ptr(
new StringWrapperT<TField>(Base::field()));
117 struct SizeFieldExistsTag {};
118 struct SerLengthFieldExistsTag {};
119 struct NoSizeFieldTag {};
121 typedef typename std::conditional<
122 Field::hasSizeFieldPrefix(),
124 typename std::conditional<
125 Field::hasSerLengthFieldPrefix(),
126 SerLengthFieldExistsTag,
129 >::type SizeExistanceTag;
131 template <
typename TPrefixField>
132 static int maxSizeByPrefix()
134 if (
sizeof(
int) <= TPrefixField::maxLength()) {
135 return std::numeric_limits<int>::max();
139 TPrefixField::maxLength() * std::numeric_limits<std::uint8_t>::digits;
141 return static_cast<int>((1U << shift) - 1);
144 static int maxSizeInternal(SizeFieldExistsTag)
146 typedef typename Field::SizeFieldPrefix SizeField;
147 return maxSizeByPrefix<SizeField>();
150 static int maxSizeInternal(SerLengthFieldExistsTag)
152 typedef typename Field::SerLengthFieldPrefix LengthField;
153 return maxSizeByPrefix<LengthField>();
156 int maxSizeInternal(NoSizeFieldTag)
const
161 static_cast<std::size_t
>(std::numeric_limits<int>::max()),
162 Base::field().getValue().max_size()));
166using StringWrapperPtr = StringWrapper::Ptr;
168template <
typename TField>
170makeStringWrapper(TField& field)
174 new StringWrapperT<TField>(field));