QGrpc{Call,Channel}Options: provide equality operators

It makes sense to provide the equality operators to these classes.
However, omit the otherwise common 'qHash' functionality as it makes no
sense to hash a options class.

[ChangeLog][QGrpcCallOptions/QGrpcChannelOptions] Made the options
classes equality comparable.

Task-number: QTBUG-128338
Change-Id: Idb7ff6946dfef373eba32839ac4b3faf2ff4e5c2
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Dennis Oberst 2025-09-04 11:07:59 +02:00
parent 30e9066edf
commit 60446214bb
9 changed files with 71 additions and 1 deletions

View File

@ -16,6 +16,7 @@ using namespace Qt::StringLiterals;
/*!
\class QGrpcCallOptions
\inmodule QtGrpc
\compares equality
\brief The QGrpcCallOptions class offers various options for fine-tuning
individual RPCs.
\since 6.6
@ -299,6 +300,13 @@ QGrpcCallOptions &QGrpcCallOptions::setFilterServerMetadata(bool value)
return *this;
}
bool comparesEqual(const QGrpcCallOptions &lhs, const QGrpcCallOptions &rhs)
{
return lhs.deadlineTimeout() == rhs.deadlineTimeout()
&& lhs.filterServerMetadata() == rhs.filterServerMetadata()
&& lhs.metadata(QtGrpc::MultiValue) == rhs.metadata(QtGrpc::MultiValue);
}
#ifndef QT_NO_DEBUG_STREAM
/*!
\since 6.8

View File

@ -7,6 +7,7 @@
#include <QtGrpc/qtgrpcglobal.h>
#include <QtGrpc/qtgrpcnamespace.h>
#include <QtCore/qcompare.h>
#include <QtCore/qhash.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qstringfwd.h>
@ -70,6 +71,10 @@ public:
private:
QExplicitlySharedDataPointer<QGrpcCallOptionsPrivate> d_ptr;
friend Q_GRPC_EXPORT bool comparesEqual(const QGrpcCallOptions &lhs,
const QGrpcCallOptions &rhs);
Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QGrpcCallOptions)
#ifndef QT_NO_DEBUG_STREAM
friend Q_GRPC_EXPORT QDebug operator<<(QDebug debug, const QGrpcCallOptions &callOpts);
#endif

View File

@ -19,6 +19,7 @@ using namespace QtGrpc;
\class QGrpcChannelOptions
\inmodule QtGrpc
\since 6.6
\compares equality
\brief The QGrpcChannelOptions class offers various options for fine-tuning
a gRPC channel.
@ -380,6 +381,18 @@ std::optional<QSslConfiguration> QGrpcChannelOptions::sslConfiguration() const
}
#endif
bool comparesEqual(const QGrpcChannelOptions &lhs, const QGrpcChannelOptions &rhs)
{
return lhs.deadlineTimeout() == rhs.deadlineTimeout()
&& lhs.filterServerMetadata() == rhs.filterServerMetadata()
&& lhs.metadata(QtGrpc::MultiValue) == rhs.metadata(QtGrpc::MultiValue)
&& lhs.serializationFormat() == rhs.serializationFormat()
#if QT_CONFIG(ssl)
&& lhs.sslConfiguration() == rhs.sslConfiguration()
#endif
;
}
#ifndef QT_NO_DEBUG_STREAM
/*!
\since 6.8

View File

@ -11,6 +11,7 @@
# include <QtNetwork/qsslconfiguration.h>
#endif
#include <QtCore/qcompare.h>
#include <QtCore/qhash.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qstringfwd.h>
@ -86,6 +87,10 @@ public:
private:
QExplicitlySharedDataPointer<QGrpcChannelOptionsPrivate> d_ptr;
friend Q_GRPC_EXPORT bool comparesEqual(const QGrpcChannelOptions &lhs,
const QGrpcChannelOptions &rhs);
Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QGrpcChannelOptions)
#ifndef QT_NO_DEBUG_STREAM
friend Q_GRPC_EXPORT QDebug operator<<(QDebug debug, const QGrpcChannelOptions &chOpts);
#endif

View File

@ -15,5 +15,6 @@ qt_internal_add_test(tst_qgrpccalloptions
LIBRARIES
Qt::Core
Qt::Test
Qt::TestPrivate
Qt::Grpc
)

View File

@ -23,6 +23,7 @@ private Q_SLOTS:
void propertyDeadline() const { common.propertyDeadline(); }
void propertyFilterServerMetadata() const { common.propertyFilterServerMetadata(); }
void streamsToDebug() const { common.streamsToDebug(); }
void comparesEqual() const { common.comparesEqual(); }
private:
GrpcCommonOptionsTest<QGrpcCallOptions> common;

View File

@ -15,5 +15,6 @@ qt_internal_add_test(tst_qgrpcchanneloptions
LIBRARIES
Qt::Core
Qt::Test
Qt::TestPrivate
Qt::Grpc
)

View File

@ -26,6 +26,7 @@ private Q_SLOTS:
void propertyDeadline() const { common.propertyDeadline(); }
void propertyFilterServerMetadata() const { common.propertyFilterServerMetadata(); }
void streamsToDebug() const { common.streamsToDebug(); }
void comparesEqual() const { common.comparesEqual(); }
void propertySerializationFormat() const;
#if QT_CONFIG(ssl)

View File

@ -1,13 +1,19 @@
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QtTest/qtest.h>
#include <QtGrpc/qgrpcchanneloptions.h>
#include <QtGrpc/qtgrpcnamespace.h>
#if QT_CONFIG(ssl)
# include <QtNetwork/qsslconfiguration.h>
#endif
#include <QtCore/qdebug.h>
#include <QtCore/qstring.h>
#include <QtCore/qhash.h>
#include <QtCore/qstring.h>
using namespace std::chrono_literals;
@ -253,4 +259,33 @@ QT_WARNING_POP
std::unique_ptr<char[]> ustr(QTest::toString(o));
QCOMPARE_EQ(storage, QString::fromUtf8(ustr.get()));
}
void comparesEqual() const
{
QTestPrivate::testEqualityOperatorsCompile<T>();
T o1;
T o2 = o1;
QT_TEST_EQUALITY_OPS(o1, o2, true);
auto updateComparisonCheck = [&] {
QT_TEST_EQUALITY_OPS(o1, o2, false);
o2 = o1;
QT_TEST_EQUALITY_OPS(o1, o2, true);
};
o1.addMetadata("new", "value");
updateComparisonCheck();
o1.setDeadlineTimeout(1s);
updateComparisonCheck();
o1.setFilterServerMetadata(true);
updateComparisonCheck();
if constexpr (std::is_same_v<T, QGrpcChannelOptions>) {
o1.setSerializationFormat(QtGrpc::SerializationFormat::Protobuf);
updateComparisonCheck();
#if QT_CONFIG(ssl)
o1.setSslConfiguration(QSslConfiguration::defaultConfiguration());
updateComparisonCheck();
#endif
}
}
};