From 357e2f802071c2383cc407edc359352d2ccc0f9b Mon Sep 17 00:00:00 2001 From: hotwatermorning Date: Sun, 25 Aug 2019 18:07:32 +0900 Subject: [PATCH] [plugin/vst3] Use Vst3PluginDestructionListener instead of on_destruction function object. --- src/plugin/vst3/Vst3Plugin.cpp | 20 +++++++++++++------- src/plugin/vst3/Vst3Plugin.hpp | 22 ++++++++++++++++++---- src/plugin/vst3/Vst3PluginFactory.cpp | 21 +++++++++++---------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/plugin/vst3/Vst3Plugin.cpp b/src/plugin/vst3/Vst3Plugin.cpp index c99118e..78637cd 100644 --- a/src/plugin/vst3/Vst3Plugin.cpp +++ b/src/plugin/vst3/Vst3Plugin.cpp @@ -10,22 +10,24 @@ NS_HWM_BEGIN using namespace Steinberg; Vst3Plugin::Vst3Plugin(std::unique_ptr pimpl, - std::unique_ptr host_context, - std::function on_destruction) + std::unique_ptr host_context) : pimpl_(std::move(pimpl)) , host_context_(std::move(host_context)) { host_context_->SetVst3Plugin(this); - on_destruction_ = on_destruction; } Vst3Plugin::~Vst3Plugin() { + vpdls_.Invoke([this](auto *li) { + li->OnBeforeDestruction(this); + }); + assert(IsEditorOpened() == false); pimpl_.reset(); + host_context_.reset(); - on_destruction_(this); } FactoryInfo const & Vst3Plugin::GetFactoryInfo() const @@ -296,12 +298,11 @@ void Vst3Plugin::LoadData(DumpData const &dump) std::unique_ptr CreatePlugin(IPluginFactory *factory, FactoryInfo const &factory_info, - ClassInfo const &class_info, - std::function on_destruction) + ClassInfo const &class_info) { auto host_context = std::make_unique(kAppName); auto impl = std::make_unique(factory, factory_info, class_info, host_context->unknownCast()); - auto plugin = std::make_unique(std::move(impl), std::move(host_context), on_destruction); + auto plugin = std::make_unique(std::move(impl), std::move(host_context)); return plugin; } @@ -311,4 +312,9 @@ Vst3Plugin::Vst3PluginListenerService & Vst3Plugin::GetVst3PluginListenerService return host_context_->vpls_; } +Vst3Plugin::Vst3PluginDestructionListenerService & Vst3Plugin::GetVst3PluginDestructionListenerService() +{ + return vpdls_; +} + NS_HWM_END diff --git a/src/plugin/vst3/Vst3Plugin.hpp b/src/plugin/vst3/Vst3Plugin.hpp index d2ecca1..5c3e746 100644 --- a/src/plugin/vst3/Vst3Plugin.hpp +++ b/src/plugin/vst3/Vst3Plugin.hpp @@ -64,7 +64,19 @@ struct Vst3PluginListener {} virtual void OnNotifyUnitByBusChange(Vst3Plugin *plugin) - {} + {} +}; + +struct Vst3PluginDestructionListener +: IListenerBase +{ +protected: + Vst3PluginDestructionListener() + {} + +public: + virtual void OnBeforeDestruction(Vst3Plugin *plugin) + {} }; //! VST3のプラグインを表すクラス @@ -177,8 +189,7 @@ class Vst3Plugin public: Vst3Plugin(std::unique_ptr pimpl, - std::unique_ptr host_context, - std::function on_destruction); + std::unique_ptr host_context); virtual ~Vst3Plugin(); @@ -278,10 +289,13 @@ class Vst3Plugin using Vst3PluginListenerService = IListenerService; Vst3PluginListenerService & GetVst3PluginListenerService(); + using Vst3PluginDestructionListenerService = IListenerService; + Vst3PluginDestructionListenerService & GetVst3PluginDestructionListenerService(); + private: std::unique_ptr pimpl_; std::unique_ptr host_context_; - std::function on_destruction_; + ListenerService vpdls_; }; NS_HWM_END diff --git a/src/plugin/vst3/Vst3PluginFactory.cpp b/src/plugin/vst3/Vst3PluginFactory.cpp index 2418be8..8f451ab 100644 --- a/src/plugin/vst3/Vst3PluginFactory.cpp +++ b/src/plugin/vst3/Vst3PluginFactory.cpp @@ -21,8 +21,7 @@ extern std::unique_ptr CreatePlugin(IPluginFactory *factory, FactoryInfo const &factory_info, - ClassInfo const &info, - std::function on_destruction + ClassInfo const &info ); FactoryInfo::FactoryInfo(PFactoryInfo const &info) @@ -137,6 +136,7 @@ bool ClassInfo::is_instrument() const } class Vst3PluginFactory::Impl +: public Vst3PluginDestructionListener { public: Impl(String module_path); @@ -155,14 +155,16 @@ class Vst3PluginFactory::Impl return factory_.get(); } - void OnVst3PluginIsCreated(Vst3Plugin const *p) { - loaded_plugins_.push_back(p); + void OnAfterConstruction(Vst3Plugin *plugin) { + loaded_plugins_.push_back(plugin); + plugin->GetVst3PluginDestructionListenerService().AddListener(this); } - - void OnVst3PluginIsDestructed(Vst3Plugin const *p) { - auto found = std::find(loaded_plugins_.begin(), loaded_plugins_.end(), p); + + void OnBeforeDestruction(Vst3Plugin *plugin) override { + auto found = std::find(loaded_plugins_.begin(), loaded_plugins_.end(), plugin); assert(found != loaded_plugins_.end()); loaded_plugins_.erase(found); + plugin->GetVst3PluginDestructionListenerService().RemoveListener(this); } UInt32 GetNumLoadedPlugins() const { @@ -334,11 +336,10 @@ std::unique_ptr { auto p = CreatePlugin(pimpl_->GetFactory(), pimpl_->GetFactoryInfo(), - GetComponentInfo(index), - [this](Vst3Plugin const *p) { pimpl_->OnVst3PluginIsDestructed(p); } + GetComponentInfo(index) ); - pimpl_->OnVst3PluginIsCreated(p.get()); + pimpl_->OnAfterConstruction(p.get()); return p; }