diff --git a/src/common/communication.cpp b/src/common/communication.cpp index 8508e191..78604adf 100644 --- a/src/common/communication.cpp +++ b/src/common/communication.cpp @@ -18,7 +18,7 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, if (c_string[0] != 0) { payload = std::string(c_string); } else { - payload = std::array(); + payload = NeedsBuffer{}; } } diff --git a/src/common/communication.h b/src/common/communication.h index c5ef24d1..7d6f1c93 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -164,14 +164,15 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket, event.payload, event.option); } + // Some buffer for the event to write to if needed. We only pass around a + // marker struct to indicate that this is indeed the case. + std::array buffer; void* data = std::visit( overload{[&](const std::nullptr_t&) -> void* { return nullptr; }, [&](const std::string& s) -> void* { return const_cast(s.c_str()); }, - [&](std::array& buffer) -> void* { - return buffer.data(); - }}, + [&](NeedsBuffer&) -> void* { return buffer.data(); }}, event.payload); const intptr_t return_value = callback(plugin, event.opcode, event.index, event.value, data, event.option); @@ -181,8 +182,7 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket, // because it was not zeroed out by the host) for an event that should // report some data back? const auto response_data = - std::holds_alternative>( - event.payload) + std::holds_alternative(event.payload) ? std::optional(std::string(static_cast(data))) : std::nullopt; diff --git a/src/common/logging.cpp b/src/common/logging.cpp index 77db395c..fbea1107 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -145,9 +145,7 @@ void Logger::log_event(bool is_dispatch, overload{ [&](const std::nullptr_t&) { message << ""; }, [&](const std::string& s) { message << "\"" << s << "\""; }, - [&](const std::array&) { - message << ""; - }}, + [&](const NeedsBuffer&) { message << ""; }}, payload); message << ")"; diff --git a/src/common/serialization.h b/src/common/serialization.h index 7c1057f0..c35537f0 100644 --- a/src/common/serialization.h +++ b/src/common/serialization.h @@ -47,6 +47,13 @@ struct overload : Ts... { template overload(Ts...)->overload; +/** + * Marker struct to indicate that that the event requires some buffer to write + * its results into. This is to prevent us from having to unnecessarily sending + * around empty arrays. + */ +struct NeedsBuffer {}; + /** * VST events are passed a void pointer that can contain a variety of different * data types depending on the event's opcode. This is typically either: @@ -69,14 +76,8 @@ overload(Ts...)->overload; * - Some empty buffer for the plugin to write its own data to, for instance for * a plugin to report its name or the label for a certain parameter. We'll * assume that this is the default if none of the above options apply. - * - * TODO: As a simple optimization we of course wouldn't have to send an entire - * empty array here, this should be replaced by some kind of marker - * struct. This would require some minor modifications in - * `passthrough_event()`. */ -using EventPayload = std:: - variant>; +using EventPayload = std::variant; /** * An event as dispatched by the VST host. These events will get forwarded to @@ -126,9 +127,7 @@ struct Event { [](S& s, std::string& string) { s.text1b(string, max_string_length); }, - [](S& s, std::array& buffer) { - s.container1b(buffer); - }}); + [](S&, NeedsBuffer&) {}}); } };