From 8ea40cd9f909c9e7ad4d9a84ad2ac3c5ec237584 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Dec 2020 15:09:02 +0100 Subject: [PATCH] Rework Vst3MessageHandler::receive_messages This now takes a regular overloaded function and the visiting is done in `receive_messages()` itself. This way we can use templates to ensure that the return type is correct. Otherwise auto will cause issues in the future when we want to return multiple concrete types from a function that takes a single variant. The alternative would be both receiving a variant as a parameter and then returning another variant as a result, but that is much less type safe. --- src/common/communication/vst3.h | 17 +++++++++++------ src/common/serialization/vst3.h | 4 ++-- src/plugin/bridges/vst3.cpp | 9 ++------- src/wine-host/bridges/vst3.cpp | 12 +++++------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/common/communication/vst3.h b/src/common/communication/vst3.h index 4b2c783e..0f97478b 100644 --- a/src/common/communication/vst3.h +++ b/src/common/communication/vst3.h @@ -152,11 +152,9 @@ class Vst3MessageHandler : public AdHocSocketHandler { * @param callback The function used to generate a response out of the * request. See the definition of `F` for more information. * - * @tparam F A function type in the form of `T::Response(Request(T))`. C++ - * doesn't have syntax for this, but the function receives a `Request` - * variant containing a `T`, and the function should return a `T::Reponse` - * object. This way we can directly deserialize into a `T::Reponse` on the - * side that called `send_object(T)`. + * @tparam F A function type in the form of `T::Response(T)` for every `T` + * in `Request`. This way we can directly deserialize into a `T::Response` + * on the side that called `receive_into(T, T::Response&)`. * * @relates Vst3MessageHandler::send_event */ @@ -178,7 +176,14 @@ class Vst3MessageHandler : public AdHocSocketHandler { request); } - const auto& response = callback(request); + // We do the visiting here using a templated lambda. This way we + // always know for sure that the function returns the correct + // type, and we can scrap a lot of boilerplate elsewhere. + const auto& response = std::visit( + [&](const T object) -> + typename T::Response { return callback(object); }, + request); + if (logging) { auto [logger, is_host_vst] = *logging; logger.log_response(!is_host_vst, response); diff --git a/src/common/serialization/vst3.h b/src/common/serialization/vst3.h index e6880d76..dc15b1c8 100644 --- a/src/common/serialization/vst3.h +++ b/src/common/serialization/vst3.h @@ -58,7 +58,7 @@ struct WantsPluginFactory { /** * When we send a control message from the plugin to the Wine VST host, this * encodes the information we request or the operation we want to perform. A - * request of type `ControlRequest(T)` should send back a `T::Reponse`. + * request of type `ControlRequest(T)` should send back a `T::Response`. */ using ControlRequest = std::variant; @@ -70,7 +70,7 @@ void serialize(S& s, ControlRequest& payload) { /** * When we do a callback from the Wine VST host to the plugin, this encodes the * information we want or the operation we want to perform. A request of type - * `CallbackRequest(T)` should send back a `T::Reponse`. + * `CallbackRequest(T)` should send back a `T::Response`. */ using CallbackRequest = std::variant; diff --git a/src/plugin/bridges/vst3.cpp b/src/plugin/bridges/vst3.cpp index 167b7ede..3575fbd4 100644 --- a/src/plugin/bridges/vst3.cpp +++ b/src/plugin/bridges/vst3.cpp @@ -78,13 +78,8 @@ Vst3PluginBridge::Vst3PluginBridge() host_callback_handler = std::jthread([&]() { sockets.vst_host_callback.receive_messages( std::pair(logger, false), - [&](CallbackRequest request) -> auto { - return std::visit(overload{[&](const WantsConfiguration&) - -> WantsConfiguration::Response { - return config; - }}, - request); - }); + overload{[&](const WantsConfiguration&) + -> WantsConfiguration::Response { return config; }}); }); // Set up the plugin factory, since this is the first thing the host will diff --git a/src/wine-host/bridges/vst3.cpp b/src/wine-host/bridges/vst3.cpp index 9920bf2c..150cc16d 100644 --- a/src/wine-host/bridges/vst3.cpp +++ b/src/wine-host/bridges/vst3.cpp @@ -49,11 +49,9 @@ Vst3Bridge::Vst3Bridge(MainContext& main_context, void Vst3Bridge::run() { sockets.host_vst_control.receive_messages( - std::nullopt, [&](ControlRequest request) -> auto& { - return std::visit(overload{[&](const WantsPluginFactory&) - -> WantsPluginFactory::Response { - return *plugin_factory; - }}, - request); - }); + std::nullopt, + overload{ + [&](const WantsPluginFactory&) -> WantsPluginFactory::Response { + return *plugin_factory; + }}); }