From e099255b92634d2d50b54056ec2bf068d078d566 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 8 Dec 2020 17:59:59 +0100 Subject: [PATCH] Generate a unique ID and store the new component --- src/wine-host/bridges/vst3.cpp | 22 ++++++++++------------ src/wine-host/bridges/vst3.h | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/wine-host/bridges/vst3.cpp b/src/wine-host/bridges/vst3.cpp index 791810b8..618b41c9 100644 --- a/src/wine-host/bridges/vst3.cpp +++ b/src/wine-host/bridges/vst3.cpp @@ -56,20 +56,14 @@ void Vst3Bridge::run() { Steinberg::IPtr component = module->getFactory() .createInstance(args.cid); - - // TODO: Next steps are: - // - Generate a new unique ID using an atomic size_t and - // fetch-and-add. - // - Add an `std::map` - // to this class and add `component` with the generated - // ID to that. - // - Add that ID to `YaComponent` and set it in the object - // we create here. if (component) { - // TODO: Generate a unique instance ID + std::lock_guard lock(component_instances_mutex); + + const size_t instance_id = generate_instance_id(); + component_instances[instance_id] = std::move(component); + return std::make_optional( - component, 420691337); + component, instance_id); } else { return std::nullopt; } @@ -78,3 +72,7 @@ void Vst3Bridge::run() { return *plugin_factory; }}); } + +size_t Vst3Bridge::generate_instance_id() { + return current_instance_id.fetch_add(1); +} diff --git a/src/wine-host/bridges/vst3.h b/src/wine-host/bridges/vst3.h index c5f52d5c..161e030c 100644 --- a/src/wine-host/bridges/vst3.h +++ b/src/wine-host/bridges/vst3.h @@ -59,6 +59,13 @@ class Vst3Bridge : public HostBridge { void run() override; private: + /** + * Generate a nique instance identifier using an atomic fetch-and-add. This + * is used to be able to refer to specific instances created for + * `IPluginFactory::createInstance()`. + */ + size_t generate_instance_id(); + /** * The IO context used for event handling so that all events and window * message handling can be performed from a single thread, even when hosting @@ -90,4 +97,22 @@ class Vst3Bridge : public HostBridge { * information during its initialization. */ std::unique_ptr plugin_factory; + + /** + * Used to assign unique identifier to instances created for + * `IPluginFactory::createInstance()`. + * + * @related enerate_instance_id + */ + std::atomic_size_t current_instance_id; + + // Below are managed instances we created for + // `IPluginFactory::createInstance()`. The keys in all of these maps are the + // unique identifiers we generated for them so we can identify specific + // instances. The mutexes are used for operations that insert or remove + // items, and not for regular access. + + std::map> + component_instances; + std::mutex component_instances_mutex; };