changeset 538:8ffb8985ae8f

Add ability to disown a wrapped plugin - making it possible to use the adapters in some situations involving managed pointers that didn't work previously
author Chris Cannam
date Thu, 23 Apr 2020 11:39:34 +0100
parents b9422f3e63a4
children 15a89a89aa9b
files src/vamp-hostsdk/PluginWrapper.cpp vamp-hostsdk/PluginBufferingAdapter.h vamp-hostsdk/PluginChannelAdapter.h vamp-hostsdk/PluginInputDomainAdapter.h vamp-hostsdk/PluginSummarisingAdapter.h vamp-hostsdk/PluginWrapper.h
diffstat 6 files changed, 42 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/vamp-hostsdk/PluginWrapper.cpp	Wed Apr 22 14:23:51 2020 +0100
+++ b/src/vamp-hostsdk/PluginWrapper.cpp	Thu Apr 23 11:39:34 2020 +0100
@@ -44,13 +44,22 @@
 
 PluginWrapper::PluginWrapper(Plugin *plugin) :
     Plugin(plugin->getInputSampleRate()),
-    m_plugin(plugin)
+    m_plugin(plugin),
+    m_pluginIsOwned(true)
 {
 }
 
 PluginWrapper::~PluginWrapper()
 {
-    delete m_plugin;
+    if (m_pluginIsOwned) {
+        delete m_plugin;
+    }
+}
+
+void
+PluginWrapper::disownPlugin()
+{
+    m_pluginIsOwned = false;
 }
 
 bool
--- a/vamp-hostsdk/PluginBufferingAdapter.h	Wed Apr 22 14:23:51 2020 +0100
+++ b/vamp-hostsdk/PluginBufferingAdapter.h	Thu Apr 23 11:39:34 2020 +0100
@@ -69,7 +69,8 @@
  * 
  * In other respects, the PluginBufferingAdapter behaves identically
  * to the plugin that it wraps. The wrapped plugin will be deleted
- * when the wrapper is deleted.
+ * when the wrapper is deleted. If you wish to prevent this, call
+ * disownPlugin().
  */
 		
 class PluginBufferingAdapter : public PluginWrapper
@@ -78,7 +79,8 @@
     /**
      * Construct a PluginBufferingAdapter wrapping the given plugin.
      * The adapter takes ownership of the plugin, which will be
-     * deleted when the adapter is deleted.
+     * deleted when the adapter is deleted. If you wish to prevent
+     * this, call disownPlugin().
      */
     PluginBufferingAdapter(Plugin *plugin);
     virtual ~PluginBufferingAdapter();
--- a/vamp-hostsdk/PluginChannelAdapter.h	Wed Apr 22 14:23:51 2020 +0100
+++ b/vamp-hostsdk/PluginChannelAdapter.h	Thu Apr 23 11:39:34 2020 +0100
@@ -104,7 +104,7 @@
  * In every respect other than its management of channels, the
  * PluginChannelAdapter behaves identically to the plugin that it
  * wraps.  The wrapped plugin will be deleted when the wrapper is
- * deleted.
+ * deleted.  If you wish to prevent this, call disownPlugin().
  *
  * \note This class was introduced in version 1.1 of the Vamp plugin SDK.
  */
@@ -115,7 +115,8 @@
     /**
      * Construct a PluginChannelAdapter wrapping the given plugin.
      * The adapter takes ownership of the plugin, which will be
-     * deleted when the adapter is deleted.
+     * deleted when the adapter is deleted. If you wish to prevent
+     * this, call disownPlugin().
      */
     PluginChannelAdapter(Plugin *plugin);
     virtual ~PluginChannelAdapter();
--- a/vamp-hostsdk/PluginInputDomainAdapter.h	Wed Apr 22 14:23:51 2020 +0100
+++ b/vamp-hostsdk/PluginInputDomainAdapter.h	Thu Apr 23 11:39:34 2020 +0100
@@ -79,7 +79,7 @@
  * In every respect other than its input domain handling, the
  * PluginInputDomainAdapter behaves identically to the plugin that it
  * wraps.  The wrapped plugin will be deleted when the wrapper is
- * deleted.
+ * deleted. If you wish to prevent this, call disownPlugin().
  *
  * \note This class was introduced in version 1.1 of the Vamp plugin SDK.
  */
@@ -90,7 +90,8 @@
     /**
      * Construct a PluginInputDomainAdapter wrapping the given plugin.
      * The adapter takes ownership of the plugin, which will be
-     * deleted when the adapter is deleted.
+     * deleted when the adapter is deleted. If you wish to prevent
+     * this, call disownPlugin().
      */
     PluginInputDomainAdapter(Plugin *plugin);
     virtual ~PluginInputDomainAdapter();
--- a/vamp-hostsdk/PluginSummarisingAdapter.h	Wed Apr 22 14:23:51 2020 +0100
+++ b/vamp-hostsdk/PluginSummarisingAdapter.h	Thu Apr 23 11:39:34 2020 +0100
@@ -89,7 +89,8 @@
     /**
      * Construct a PluginSummarisingAdapter wrapping the given plugin.
      * The adapter takes ownership of the plugin, which will be
-     * deleted when the adapter is deleted.
+     * deleted when the adapter is deleted. If you wish to prevent
+     * this, call disownPlugin().
      */
     PluginSummarisingAdapter(Plugin *plugin); 
     virtual ~PluginSummarisingAdapter();
--- a/vamp-hostsdk/PluginWrapper.h	Wed Apr 22 14:23:51 2020 +0100
+++ b/vamp-hostsdk/PluginWrapper.h	Thu Apr 23 11:39:34 2020 +0100
@@ -56,6 +56,9 @@
  * override only the methods that are meaningful for the particular
  * adapter.
  *
+ * A PluginWrapper takes ownership of the plugin it wraps, and deletes
+ * it when it is itself deleted. To prevent this, call disownPlugin().
+ *
  * \note This class was introduced in version 1.1 of the Vamp plugin SDK.
  */
 
@@ -121,9 +124,25 @@
         return 0;
     }
 
+    /**
+     * Disown the wrapped plugin, so that we no longer delete it on
+     * our own destruction. The identity of the wrapped plugin is
+     * unchanged, but this switches the expectation about ownership so
+     * that the caller is expected to handle the lifecycle of the
+     * wrapped plugin and to ensure that it outlasts this wrapper.
+     *
+     * This is not the normal model, but could be useful in situations
+     * where a plugin is already managed using a smart pointer model
+     * before being adapted into this wrapper. Note that this can't be
+     * reversed - if you call this, you must subsequently take charge
+     * of the wrapped plugin yourself.
+     */
+    void disownPlugin();
+    
 protected:
     PluginWrapper(Plugin *plugin); // I take ownership of plugin
     Plugin *m_plugin;
+    bool m_pluginIsOwned;
 };
 
 }