From 7e5bc6b07b2b34564e9aca6f6156c7796777dce6 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 1 Oct 2022 15:57:06 +0200 Subject: [PATCH] Add a way to retrieve clap_event_header_t*s This sets the correct pointer in the SysEx events just before returning them. --- src/common/serialization/clap/events.cpp | 22 ++++++++++++++++ src/common/serialization/clap/events.h | 33 ++++++++++++++---------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/common/serialization/clap/events.cpp b/src/common/serialization/clap/events.cpp index 465e0099..3f656f8b 100644 --- a/src/common/serialization/clap/events.cpp +++ b/src/common/serialization/clap/events.cpp @@ -16,6 +16,8 @@ #include "events.h" +#include "../../utils.h" + namespace clap { namespace events { @@ -106,5 +108,25 @@ std::optional Event::parse(const clap_event_header_t& generic_event) { } } +const clap_event_header_t* Event::get() const { + return std::visit( + overload{[](payload::MidiSysex& event) -> const clap_event_header_t* { + // These events contain heap data pointers. We store this + // data using an `std::string` alongside the event struct, + // but we can only set the pointer here just before + // returning the event in case it was moved inbetween + // deserialization and this function being called. + event.event.buffer = + reinterpret_cast(event.buffer.data()); + event.event.size = event.buffer.size(); + + return &event.event.header; + }, + [](const auto& event) -> const clap_event_header_t* { + return &event.event.header; + }}, + payload); +} + } // namespace events } // namespace clap diff --git a/src/common/serialization/clap/events.h b/src/common/serialization/clap/events.h index c0e33389..15002022 100644 --- a/src/common/serialization/clap/events.h +++ b/src/common/serialization/clap/events.h @@ -238,6 +238,12 @@ struct alignas(16) Event { */ static std::optional parse(const clap_event_header_t& generic_event); + /** + * Get the `clap_event_header_t*` representation for this event. The pointer + * is valid as long as this struct isn't moved. + */ + const clap_event_header_t* get() const; + /** * The actual event data. These also contain the header because storing the * entire `clap_event_*_t` struct is the only way to serialize the event @@ -247,19 +253,20 @@ struct alignas(16) Event { * `clap_input_events::get()`, but that would cause unexpected lifetime * issues. */ - std::variant + mutable std::variant< + payload::Note, + payload::NoteExpression, + payload::ParamValue, + payload::ParamMod, + payload::ParamGesture, + // Most events are about the same length, but having the transport in + // here sadly doubles this struct's size + // TODO: Pack the events at some point, this will require special + // handling for SysEx events + payload::Transport, + payload::Midi, + payload::MidiSysex, + payload::Midi2> payload; template