Remove the redundant template arguments from QAbstractGrpcClient

Now we may get the property ordering information from QProtobufMessage
without the need of knowing the exact type of inherited message.
This allows us to remove template arguments from
QAbstractGrpcClient::call and QAbstractGrpcClient::startStream
methods.

Task-number: QTBUG-120931
Change-Id: I497b80f53f5680790211b71a53a9ddc75d113424
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
This commit is contained in:
Alexey Edelev 2024-01-20 07:27:34 +01:00
parent d737f7a5fc
commit 280a255534
6 changed files with 45 additions and 42 deletions

View File

@ -44,7 +44,7 @@ static QString threadSafetyWarning(QLatin1StringView methodName)
*/
/*!
\fn template <typename ParamType, typename StreamType> std::shared_ptr<StreamType> QAbstractGrpcClient::startStream(QLatin1StringView method, const QProtobufMessage &arg, const QGrpcCallOptions &options)
\fn template <typename StreamType> std::shared_ptr<StreamType> QAbstractGrpcClient::startStream(QLatin1StringView method, const QProtobufMessage &arg, const QGrpcCallOptions &options)
Starts the stream \a method of the \e StreamType type with the message
argument \a arg to the attached channel.
@ -193,6 +193,16 @@ void QAbstractGrpcClient::attachChannel(const std::shared_ptr<QAbstractGrpcChann
emit channelChanged();
}
std::shared_ptr<QGrpcCallReply> QAbstractGrpcClient::call(QLatin1StringView method,
const QProtobufMessage &arg,
const QGrpcCallOptions &options)
{
std::optional<QByteArray> argData = trySerialize(arg);
if (!argData)
return {};
return call(method, *argData, options);
}
/*!
\since 6.7
Returns the channel attached to this client.
@ -281,6 +291,18 @@ QAbstractGrpcClient::startBidirStream(QLatin1StringView method, QByteArrayView a
return grpcStream;
}
std::optional<QByteArray> QAbstractGrpcClient::trySerialize(const QProtobufMessage &arg)
{
using namespace Qt::StringLiterals;
auto _serializer = serializer();
if (_serializer == nullptr) {
Q_EMIT errorOccurred({ QGrpcStatus::Unknown,
"Serializing failed. Serializer is not ready."_L1 });
return std::nullopt;
}
return _serializer->serialize(&arg);
}
/*!
Serializer provides assigned to client serializer.
Returns pointer to serializerowned by QProtobufSerializerRegistry.

View File

@ -41,29 +41,22 @@ protected:
explicit QAbstractGrpcClient(QLatin1StringView service, QObject *parent = nullptr);
~QAbstractGrpcClient() override;
template <typename ParamType>
std::shared_ptr<QGrpcCallReply> call(QLatin1StringView method, const QProtobufMessage &arg,
const QGrpcCallOptions &options)
{
std::optional<QByteArray> argData = trySerialize<ParamType>(arg);
if (!argData)
return {};
return call(method, *argData, options);
}
const QGrpcCallOptions &options);
#ifdef Q_QDOC
template <typename ParamType, typename StreamType>
template <typename StreamType>
#else
template <typename ParamType, typename StreamType,
template <typename StreamType,
std::enable_if_t<std::is_same_v<StreamType, QGrpcServerStream>
|| std::is_same_v<StreamType, QGrpcClientStream>
|| std::is_same_v<StreamType, QGrpcBidirStream>,
|| std::is_same_v<StreamType, QGrpcClientStream>
|| std::is_same_v<StreamType, QGrpcBidirStream>,
bool> = true>
#endif
std::shared_ptr<StreamType> startStream(QLatin1StringView method, const QProtobufMessage &arg,
const QGrpcCallOptions &options)
{
std::optional<QByteArray> argData = trySerialize<ParamType>(arg);
std::optional<QByteArray> argData = trySerialize(arg);
if (!argData)
return {};
if constexpr (std::is_same_v<StreamType, QGrpcServerStream>) {
@ -89,19 +82,7 @@ private:
std::shared_ptr<QGrpcBidirStream> startBidirStream(QLatin1StringView method, QByteArrayView arg,
const QGrpcCallOptions &options);
template <typename ParamType>
std::optional<QByteArray> trySerialize(const QProtobufMessage &arg)
{
using namespace Qt::StringLiterals;
auto _serializer = serializer();
if (_serializer == nullptr) {
Q_EMIT errorOccurred({ QGrpcStatus::Unknown,
"Serializing failed. Serializer is not ready."_L1 });
return std::nullopt;
}
return _serializer->serialize(&arg);
}
std::optional<QByteArray> trySerialize(const QProtobufMessage &arg);
std::shared_ptr<QAbstractProtobufSerializer> serializer() const;
Q_DISABLE_COPY_MOVE(QAbstractGrpcClient)

View File

@ -66,7 +66,7 @@ const char *GrpcTemplates::ClientMethodDefinitionAsyncTemplate()
return "\nstd::shared_ptr<QGrpcCallReply> $classname$::$method_name$(const $param_type$ "
"&$param_name$, const QGrpcCallOptions &options)\n"
"{\n"
" return call<$param_type$>(\"$method_name$\"_L1, $param_name$, options);\n"
" return call(\"$method_name$\"_L1, $param_name$, options);\n"
"}\n";
}
@ -76,7 +76,7 @@ const char *GrpcTemplates::ClientMethodDefinitionAsync2Template()
"*context, const std::function<void(std::shared_ptr<QGrpcCallReply>)> &callback, const "
"QGrpcCallOptions &options)\n"
"{\n"
" std::shared_ptr<QGrpcCallReply> reply = call<$param_type$>(\"$method_name$\"_L1, "
" std::shared_ptr<QGrpcCallReply> reply = call(\"$method_name$\"_L1, "
"$param_name$, options);\n"
" QObject::connect(reply.get(), &QGrpcCallReply::finished, context, [reply, "
"callback]() "
@ -104,7 +104,7 @@ const char *GrpcTemplates::ClientMethodDefinitionQmlTemplate()
"from JS engine context\";\n"
" return;\n"
" }\n\n"
" std::shared_ptr<QGrpcCallReply> reply = call<$param_type$>(\"$method_name$\"_L1, "
" std::shared_ptr<QGrpcCallReply> reply = call(\"$method_name$\"_L1, "
"$param_name$, options);\n"
" reply->subscribe(jsEngine, [reply, callback, jsEngine]() {\n"
" auto result = $return_type$(reply->read<$return_type$>());\n"
@ -126,7 +126,7 @@ const char *GrpcTemplates::ClientMethodStreamDefinitionTemplate()
return "std::shared_ptr<$stream_type$> $classname$::stream$method_name_upper$("
"const $param_type$ &$param_name$, const QGrpcCallOptions &options)\n"
"{\n"
" return startStream<$param_type$, $stream_type$>(\"$method_name$\"_L1, "
" return startStream<$stream_type$>(\"$method_name$\"_L1, "
"$param_name$, options);\n"
"}\n\n";
}

View File

@ -14,12 +14,12 @@ Client::Client(QObject *parent)
std::shared_ptr<QGrpcCallReply> Client::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return call<qtgrpc::tests::SimpleStringMessage>("testMethod"_L1, arg, options);
return call("testMethod"_L1, arg, options);
}
void Client::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QObject *context, const std::function<void(std::shared_ptr<QGrpcCallReply>)> &callback, const QGrpcCallOptions &options)
{
std::shared_ptr<QGrpcCallReply> reply = call<qtgrpc::tests::SimpleStringMessage>("testMethod"_L1, arg, options);
std::shared_ptr<QGrpcCallReply> reply = call("testMethod"_L1, arg, options);
QObject::connect(reply.get(), &QGrpcCallReply::finished, context, [reply, callback]() {
callback(reply);
}, Qt::SingleShotConnection);
@ -27,17 +27,17 @@ void Client::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QOb
std::shared_ptr<QGrpcServerStream> Client::streamTestMethodServerStream(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return startStream<qtgrpc::tests::SimpleStringMessage, QGrpcServerStream>("testMethodServerStream"_L1, arg, options);
return startStream<QGrpcServerStream>("testMethodServerStream"_L1, arg, options);
}
std::shared_ptr<QGrpcClientStream> Client::streamTestMethodClientStream(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return startStream<qtgrpc::tests::SimpleStringMessage, QGrpcClientStream>("testMethodClientStream"_L1, arg, options);
return startStream<QGrpcClientStream>("testMethodClientStream"_L1, arg, options);
}
std::shared_ptr<QGrpcBidirStream> Client::streamTestMethodBiStream(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return startStream<qtgrpc::tests::SimpleStringMessage, QGrpcBidirStream>("testMethodBiStream"_L1, arg, options);
return startStream<QGrpcBidirStream>("testMethodBiStream"_L1, arg, options);
}
} // namespace TestService

View File

@ -14,12 +14,12 @@ Client::Client(QObject *parent)
std::shared_ptr<QGrpcCallReply> Client::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return call<qtgrpc::tests::SimpleStringMessage>("testMethod"_L1, arg, options);
return call("testMethod"_L1, arg, options);
}
void Client::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QObject *context, const std::function<void(std::shared_ptr<QGrpcCallReply>)> &callback, const QGrpcCallOptions &options)
{
std::shared_ptr<QGrpcCallReply> reply = call<qtgrpc::tests::SimpleStringMessage>("testMethod"_L1, arg, options);
std::shared_ptr<QGrpcCallReply> reply = call("testMethod"_L1, arg, options);
QObject::connect(reply.get(), &QGrpcCallReply::finished, context, [reply, callback]() {
callback(reply);
}, Qt::SingleShotConnection);
@ -27,17 +27,17 @@ void Client::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const QOb
std::shared_ptr<QGrpcServerStream> Client::streamTestMethodServerStream(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return startStream<qtgrpc::tests::SimpleStringMessage, QGrpcServerStream>("testMethodServerStream"_L1, arg, options);
return startStream<QGrpcServerStream>("testMethodServerStream"_L1, arg, options);
}
std::shared_ptr<QGrpcClientStream> Client::streamTestMethodClientStream(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return startStream<qtgrpc::tests::SimpleStringMessage, QGrpcClientStream>("testMethodClientStream"_L1, arg, options);
return startStream<QGrpcClientStream>("testMethodClientStream"_L1, arg, options);
}
std::shared_ptr<QGrpcBidirStream> Client::streamTestMethodBiStream(const qtgrpc::tests::SimpleStringMessage &arg, const QGrpcCallOptions &options)
{
return startStream<qtgrpc::tests::SimpleStringMessage, QGrpcBidirStream>("testMethodBiStream"_L1, arg, options);
return startStream<QGrpcBidirStream>("testMethodBiStream"_L1, arg, options);
}
} // namespace TestService

View File

@ -25,7 +25,7 @@ void QmlClient::testMethod(const qtgrpc::tests::SimpleStringMessage &arg, const
return;
}
std::shared_ptr<QGrpcCallReply> reply = call<qtgrpc::tests::SimpleStringMessage>("testMethod"_L1, arg, options);
std::shared_ptr<QGrpcCallReply> reply = call("testMethod"_L1, arg, options);
reply->subscribe(jsEngine, [reply, callback, jsEngine]() {
auto result = qtgrpc::tests::SimpleStringMessage(reply->read<qtgrpc::tests::SimpleStringMessage>());
QJSValue(callback).call(QJSValueList{jsEngine->toScriptValue(result)});