Generate a unique ID and store the new component

This commit is contained in:
Robbert van der Helm
2020-12-08 17:59:59 +01:00
parent 5eb1fe2de2
commit e099255b92
2 changed files with 35 additions and 12 deletions
+10 -12
View File
@@ -56,20 +56,14 @@ void Vst3Bridge::run() {
Steinberg::IPtr<Steinberg::Vst::IComponent> component =
module->getFactory()
.createInstance<Steinberg::Vst::IComponent>(args.cid);
// TODO: Next steps are:
// - Generate a new unique ID using an atomic size_t and
// fetch-and-add.
// - Add an `std::map<size_t,
// Steinberg::IPtr<Steinberg::Vst::IComponent>`
// 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<YaComponent::Arguments>(
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);
}
+25
View File
@@ -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<YaPluginFactory> 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<size_t, Steinberg::IPtr<Steinberg::Vst::IComponent>>
component_instances;
std::mutex component_instances_mutex;
};