1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtProtobuf/private/qtprotobufserializerhelpers_p.h>
5#include <QtProtobuf/private/qprotobufmessage_p.h>
6
7QT_BEGIN_NAMESPACE
8
9QVariant
10QtProtobufSerializerHelpers::messageProperty(const QProtobufMessage *message,
11 const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo,
12 bool allowInitialize)
13{
14 const auto *metaObject = QtProtobufSerializerHelpers::messageMetaObject(message);
15 int propertyIndex = fieldInfo.propertyIndex() + metaObject->propertyOffset();
16 QMetaProperty metaProperty = metaObject->property(index: propertyIndex);
17
18 if (!metaProperty.isValid())
19 return {};
20
21 if (fieldInfo.fieldFlags() & QtProtobufPrivate::FieldFlag::ExplicitPresence
22 && !allowInitialize) {
23 int hasPropertyIndex = propertyIndex + 1;
24 QMetaProperty hasProperty = metaObject->property(index: hasPropertyIndex);
25 Q_ASSERT_X(hasProperty.isValid() && hasProperty.metaType().id() == QMetaType::Bool,
26 "QProtobufMessage",
27 "The field with the explicit presence requirement doesn't have the follow 'has' "
28 "property.");
29 if (!hasProperty.readOnGadget(gadget: message).toBool())
30 return QVariant(metaProperty.metaType());
31 }
32
33 QVariant propertyValue = metaProperty.readOnGadget(gadget: message);
34 return propertyValue;
35}
36
37bool QtProtobufSerializerHelpers::setMessageProperty(QProtobufMessage *message,
38 const QtProtobufPrivate::QProtobufFieldInfo
39 &fieldInfo,
40 const QVariant &value)
41{
42 const auto *pMessage = QProtobufMessagePrivate::get(message);
43 const auto mp = pMessage->metaProperty(ord: fieldInfo);
44 if (!mp)
45 return false;
46 return mp->writeOnGadget(gadget: message, value);
47}
48
49bool QtProtobufSerializerHelpers::setMessageProperty(QProtobufMessage *message,
50 const QtProtobufPrivate::QProtobufFieldInfo
51 &fieldInfo,
52 QVariant &&value)
53{
54 const auto *pMessage = QProtobufMessagePrivate::get(message);
55 const auto mp = pMessage->metaProperty(ord: fieldInfo);
56 if (!mp)
57 return false;
58 return mp->writeOnGadget(gadget: message, value: std::move(value));
59}
60
61const QMetaObject *QtProtobufSerializerHelpers::messageMetaObject(const QProtobufMessage *message)
62{
63 Q_ASSERT(message);
64 const auto *pMessage = QProtobufMessagePrivate::get(message);
65 Q_ASSERT(pMessage);
66 return pMessage->metaObject;
67}
68
69QT_END_NAMESPACE
70

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtgrpc/src/protobuf/qtprotobufserializerhelpers.cpp