Add a CLAP EventList serialization wrapper

This can read events from the host and write back events to the host.
This commit is contained in:
Robbert van der Helm
2022-10-01 16:28:10 +02:00
parent 7e5bc6b07b
commit ede80eab66
3 changed files with 78 additions and 1 deletions
+31
View File
@@ -128,5 +128,36 @@ const clap_event_header_t* Event::get() const {
payload);
}
EventList::EventList() noexcept {}
void EventList::repopulate(const clap_input_events_t& in_events) {
events_.clear();
const uint32_t num_events = in_events.size(&in_events);
for (uint32_t i = 0; i < num_events; i++) {
const clap_event_header_t* event = in_events.get(&in_events, i);
assert(event);
if (std::optional<Event> parsed_event = Event::parse(*event);
parsed_event) {
events_.emplace_back(std::move(*parsed_event));
}
}
}
void EventList::clear() noexcept {
events_.clear();
}
void EventList::write_back_outputs(
const clap_output_events_t& out_events) const {
for (const auto& event : events_) {
// We'll ignore the result here, we can't handle it anyways and maybe
// some hosts will return `false` for events they don't recognize
// instead of only when out of memory
out_events.try_push(&out_events, event.get());
}
}
} // namespace events
} // namespace clap
+46
View File
@@ -21,9 +21,11 @@
#include <bitsery/traits/string.h>
#include <clap/events.h>
#include <llvm/small-vector.h>
#include "../bitsery/ext/in-place-variant.h"
#include "../bitsery/ext/native-pointer.h"
#include "../bitsery/traits/small-vector.h"
#include "../common.h"
namespace clap {
@@ -275,6 +277,50 @@ struct alignas(16) Event {
}
};
/**
* A list storing one or more CLAP events. Can be used for both input and output
* events.
*/
class EventList {
public:
/**
* We only provide a default constructor here, because we need to fill the
* existing object with new events every processing cycle to avoid
* reallocating a new object every time.
*/
EventList() noexcept;
/**
* Read data from a `clap_input_events_t` into this existing object. This
* minimizes reallocations by keeping `events_` as is.
*/
void repopulate(const clap_input_events_t& in_events);
/**
* Remove all events. Used at the start of the process function for the
* output event list.
*/
void clear() noexcept;
/**
* Write the stored events to the host's `clap_output_events_t`.
*/
void write_back_outputs(const clap_output_events_t& out_events) const;
/**
* Return the number of events we store. Used in debug logs.
*/
inline size_t size() const noexcept { return events_.size(); }
template <typename S>
void serialize(S& s) {
s.container(events_, 1 << 16);
}
private:
llvm::SmallVector<Event, 64> events_;
};
} // namespace events
} // namespace clap
+1 -1
View File
@@ -249,7 +249,7 @@ class YaEventList : public Steinberg::Vst::IEventList {
size_t num_events() const noexcept;
/**
* Write these events an output events queue on the `ProcessData` object
* Write these events to an output events queue on the `ProcessData` object
* provided by the host.
*/
void write_back_outputs(Steinberg::Vst::IEventList& output_events) const;