Pass a marker struct instead of an empty array

This commit is contained in:
Robbert van der Helm
2020-03-08 18:51:16 +01:00
parent d8e39ed16a
commit 5523871fd8
4 changed files with 16 additions and 19 deletions
+1 -1
View File
@@ -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<char, max_string_length>();
payload = NeedsBuffer{};
}
}
+5 -5
View File
@@ -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<char, max_string_length> buffer;
void* data = std::visit(
overload{[&](const std::nullptr_t&) -> void* { return nullptr; },
[&](const std::string& s) -> void* {
return const_cast<char*>(s.c_str());
},
[&](std::array<char, max_string_length>& 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<std::array<char, max_string_length>>(
event.payload)
std::holds_alternative<NeedsBuffer>(event.payload)
? std::optional(std::string(static_cast<char*>(data)))
: std::nullopt;
+1 -3
View File
@@ -145,9 +145,7 @@ void Logger::log_event(bool is_dispatch,
overload{
[&](const std::nullptr_t&) { message << "<nullptr>"; },
[&](const std::string& s) { message << "\"" << s << "\""; },
[&](const std::array<char, max_string_length>&) {
message << "<writeable_buffer>";
}},
[&](const NeedsBuffer&) { message << "<writable_buffer>"; }},
payload);
message << ")";
+9 -10
View File
@@ -47,6 +47,13 @@ struct overload : Ts... {
template <class... Ts>
overload(Ts...)->overload<Ts...>;
/**
* 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<Ts...>;
* - 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<std::nullptr_t, std::string, std::array<char, max_string_length>>;
using EventPayload = std::variant<std::nullptr_t, std::string, NeedsBuffer>;
/**
* 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<char, max_string_length>& buffer) {
s.container1b(buffer);
}});
[](S&, NeedsBuffer&) {}});
}
};