Add the qtprotobufdefs_p.h

The header should contain the useful for the implementation definitions
and constants.

This adds the ProtobufFieldNumMin and ProtobufFieldNumMax constants
which reflect the minimum and maximum field numbers for the protobuf
messages.

Use the newly introduced constants in the places where values were
duplicated.

Pick-to: 6.8
Change-Id: I27c253dc4102eea04379204f72c0d6d2376fdfbb
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
This commit is contained in:
Alexey Edelev 2024-06-14 14:16:47 +02:00
parent b30c6ee79d
commit 437f489ab6
4 changed files with 33 additions and 3 deletions

View File

@ -16,6 +16,7 @@ qt_internal_add_module(Protobuf
qprotobufpropertyordering.cpp qprotobufpropertyordering.h qprotobufpropertyordering.cpp qprotobufpropertyordering.h
qtprotobuftypes.cpp qtprotobuftypes.h qtprotobuftypes.cpp qtprotobuftypes.h
qprotobufoneof.cpp qprotobufoneof.h qprotobufoneof.cpp qprotobufoneof.h
qtprotobufdefs_p.h
EXTRA_CMAKE_FILES EXTRA_CMAKE_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/QtProtobufProperties.cmake.in" "${CMAKE_CURRENT_SOURCE_DIR}/QtProtobufProperties.cmake.in"
PUBLIC_LIBRARIES PUBLIC_LIBRARIES

View File

@ -4,6 +4,7 @@
#include "qprotobufjsonserializer.h" #include "qprotobufjsonserializer.h"
#include "qprotobufserializer_p.h" #include "qprotobufserializer_p.h"
#include "qprotobufregistration.h" #include "qprotobufregistration.h"
#include "qtprotobufdefs_p.h"
#include <QtCore/qcoreapplication.h> #include <QtCore/qcoreapplication.h>
#include <QtCore/qjsonarray.h> #include <QtCore/qjsonarray.h>
@ -341,7 +342,7 @@ public:
Q_ASSERT(ordering != nullptr); Q_ASSERT(ordering != nullptr);
for (int index = 0; index < ordering->fieldCount(); ++index) { for (int index = 0; index < ordering->fieldCount(); ++index) {
int fieldIndex = ordering->getFieldNumber(index); int fieldIndex = ordering->getFieldNumber(index);
Q_ASSERT_X(fieldIndex < 536870912 && fieldIndex > 0, Q_ASSERT_X(fieldIndex <= ProtobufFieldNumMax && fieldIndex >= ProtobufFieldNumMin,
"", "",
"fieldIndex is out of range"); "fieldIndex is out of range");
QProtobufFieldInfo fieldInfo(*ordering, index); QProtobufFieldInfo fieldInfo(*ordering, index);
@ -587,7 +588,8 @@ public:
std::map<QString, QProtobufFieldInfo> msgContainer; // map<key, fieldInfo> std::map<QString, QProtobufFieldInfo> msgContainer; // map<key, fieldInfo>
for (int index = 0; index < ordering->fieldCount(); ++index) { for (int index = 0; index < ordering->fieldCount(); ++index) {
int fieldIndex = ordering->getFieldNumber(index); int fieldIndex = ordering->getFieldNumber(index);
Q_ASSERT_X(fieldIndex < 536870912 && fieldIndex > 0, "", "fieldIndex is out of range"); Q_ASSERT_X(fieldIndex <= ProtobufFieldNumMax && fieldIndex >= ProtobufFieldNumMin, "",
"fieldIndex is out of range");
QProtobufFieldInfo fieldInfo(*ordering, index); QProtobufFieldInfo fieldInfo(*ordering, index);
QString key = fieldInfo.getJsonName().toString(); QString key = fieldInfo.getJsonName().toString();
msgContainer.insert(std::pair<QString, QProtobufFieldInfo>(key, fieldInfo)); msgContainer.insert(std::pair<QString, QProtobufFieldInfo>(key, fieldInfo));

View File

@ -5,6 +5,7 @@
#include "qprotobufserializer.h" #include "qprotobufserializer.h"
#include "qprotobufserializer_p.h" #include "qprotobufserializer_p.h"
#include "qprotobufregistration.h" #include "qprotobufregistration.h"
#include "qtprotobufdefs_p.h"
#include "qtprotobuftypes.h" #include "qtprotobuftypes.h"
#include <QtCore/qmetatype.h> #include <QtCore/qmetatype.h>
@ -266,7 +267,8 @@ void QProtobufSerializerPrivate::serializeMessage(const QProtobufMessage *messag
for (int index = 0; index < ordering->fieldCount(); ++index) { for (int index = 0; index < ordering->fieldCount(); ++index) {
int fieldIndex = ordering->getFieldNumber(index); int fieldIndex = ordering->getFieldNumber(index);
Q_ASSERT_X(fieldIndex < 536870912 && fieldIndex > 0, "", "fieldIndex is out of range"); Q_ASSERT_X(fieldIndex <= ProtobufFieldNumMax && fieldIndex >= ProtobufFieldNumMin, "",
"fieldIndex is out of range");
QProtobufFieldInfo fieldInfo(*ordering, index); QProtobufFieldInfo fieldInfo(*ordering, index);
QVariant propertyValue = message->property(fieldInfo); QVariant propertyValue = message->property(fieldInfo);
serializeProperty(propertyValue, fieldInfo); serializeProperty(propertyValue, fieldInfo);

View File

@ -0,0 +1,25 @@
// 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
#ifndef QTPROTOBUFDEFS_P_H
#define QTPROTOBUFDEFS_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
QT_BEGIN_NAMESPACE
constexpr int ProtobufFieldNumMin = 1;
constexpr int ProtobufFieldNumMax = 536870911;
QT_END_NAMESPACE
#endif // QTPROTOBUFDEFS_P_H