1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtProtobuf/private/qprotobufdeserializerbase_p.h>
#include <QtProtobuf/private/qprotobufregistration_p.h>
#include <QtProtobuf/private/qtprotobuflogging_p.h>
#include <QtProtobuf/private/qtprotobufserializerhelpers_p.h>
QT_BEGIN_NAMESPACE
QProtobufDeserializerBase::QProtobufDeserializerBase()
= default;
QProtobufDeserializerBase::~QProtobufDeserializerBase()
= default;
bool QProtobufDeserializerBase::deserializeMessageField(QProtobufMessage *message)
{
Q_ASSERT(message != nullptr);
auto prevCachedRepeatedIterator = std::move(m_cachedRepeatedIterator);
auto prevCachedPropertyValue = m_cachedPropertyValue;
auto prevCachedIndex = m_cachedIndex;
clearCachedValue();
bool result = deserializeMessage(message);
m_cachedPropertyValue = prevCachedPropertyValue;
m_cachedIndex = prevCachedIndex;
m_cachedRepeatedIterator = std::move(prevCachedRepeatedIterator);
return result;
}
bool QProtobufDeserializerBase::deserializeMessage(QProtobufMessage *message)
{
Q_ASSERT(message != nullptr);
const auto *ordering = message->propertyOrdering();
for (int fieldIndex = nextFieldIndex(message); fieldIndex >= 0;
fieldIndex = nextFieldIndex(message)) {
QtProtobufPrivate::QProtobufFieldInfo fieldInfo(*ordering, fieldIndex);
if (m_cachedIndex != fieldIndex) {
if (!storeCachedValue(message)) {
setError(QAbstractProtobufSerializer::Error::InvalidFormat,
"Unable to store the property in message");
return false;
}
m_cachedPropertyValue = QtProtobufSerializerHelpers::messageProperty(message, fieldInfo,
true);
m_cachedIndex = fieldIndex;
}
QMetaType metaType = m_cachedPropertyValue.metaType();
if (metaType.flags().testFlag(QMetaType::IsPointer)) {
if (!deserializeMessageField(m_cachedPropertyValue.value<QProtobufMessage *>()))
return false;
continue;
}
const auto fieldFlags = fieldInfo.fieldFlags();
if ((fieldFlags.testFlags(RepeatedMessageFlags)
|| fieldFlags.testFlags({ QtProtobufPrivate::FieldFlag::Map }))
&& m_cachedPropertyValue.canView(QMetaType::fromType<QProtobufRepeatedIterator>())) {
if (!m_cachedRepeatedIterator.isValid())
m_cachedRepeatedIterator = m_cachedPropertyValue.view<QProtobufRepeatedIterator>();
if (!deserializeMessageField(m_cachedRepeatedIterator.addNext()))
return false;
m_cachedRepeatedIterator.push();
continue;
}
if (fieldFlags.testFlag(QtProtobufPrivate::FieldFlag::Enum)) {
if (!deserializeEnum(m_cachedPropertyValue, fieldInfo)) {
setError(QAbstractProtobufSerializer::Error::UnknownType,
"Unable to covert enum field to compatible serialization format");
return false;
}
continue;
}
if (deserializeScalarField(m_cachedPropertyValue, fieldInfo)) {
if (!m_cachedPropertyValue.isValid())
return false;
continue;
}
auto handler = QtProtobufPrivate::findHandler(metaType);
if (!handler.deserializer) {
qProtoWarning() << "No deserializer for type" << metaType.name();
setError(QAbstractProtobufSerializer::Error::UnknownType,
QString::fromUtf8("No deserializer is registered for type %1")
.arg(QString::fromUtf8(metaType.name())));
return false;
}
handler.deserializer([this](QProtobufMessage *
message) { return this->deserializeMessageField(message); },
m_cachedPropertyValue.data());
}
if (!storeCachedValue(message)) {
setError(QAbstractProtobufSerializer::Error::InvalidFormat,
"Unable to store the property in message");
return false;
}
return true;
}
bool QProtobufDeserializerBase::storeCachedValue(QProtobufMessage *message)
{
bool ok = true;
if (m_cachedIndex >= 0 && !m_cachedPropertyValue.isNull()) {
const auto *ordering = message->propertyOrdering();
QtProtobufPrivate::QProtobufFieldInfo fieldInfo(*ordering, m_cachedIndex);
ok = QtProtobufSerializerHelpers::setMessageProperty(message, fieldInfo,
m_cachedPropertyValue);
clearCachedValue();
}
return ok;
}
void QProtobufDeserializerBase::clearCachedValue()
{
m_cachedPropertyValue.clear();
m_cachedIndex = -1;
m_cachedRepeatedIterator = {};
}
QT_END_NAMESPACE
|