c@75: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@75: c@75: /* c@75: Piper C++ c@75: c@75: Centre for Digital Music, Queen Mary, University of London. c@75: Copyright 2006-2016 Chris Cannam and QMUL. c@75: c@75: Permission is hereby granted, free of charge, to any person c@75: obtaining a copy of this software and associated documentation c@75: files (the "Software"), to deal in the Software without c@75: restriction, including without limitation the rights to use, copy, c@75: modify, merge, publish, distribute, sublicense, and/or sell copies c@75: of the Software, and to permit persons to whom the Software is c@75: furnished to do so, subject to the following conditions: c@75: c@75: The above copyright notice and this permission notice shall be c@75: included in all copies or substantial portions of the Software. c@75: c@75: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@75: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@75: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@75: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR c@75: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@75: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@75: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@75: c@75: Except as contained in this notice, the names of the Centre for c@75: Digital Music; Queen Mary, University of London; and Chris Cannam c@75: shall not be used in advertising or otherwise to promote the sale, c@75: use or other dealings in this Software without prior written c@75: authorization. c@75: */ c@75: c@75: #ifndef PIPER_PRESERVING_PLUGIN_HANDLE_MAPPER_H c@75: #define PIPER_PRESERVING_PLUGIN_HANDLE_MAPPER_H c@75: c@75: #include "PluginHandleMapper.h" c@75: #include "PreservingPluginOutputIdMapper.h" c@75: c@75: #include c@75: c@97: namespace piper_vamp { c@75: cannam@265: /** cannam@265: * A PluginHandleMapper that accepts a handle in the handleToPlugin cannam@265: * method, storing it for later, and returns the same handle from cannam@265: * pluginToHandle when given the same plugin pointer as it had earlier cannam@265: * returned from handleToPlugin. It can only remember one handle, and cannam@265: * knows nothing about actual plugins - the plugin pointer it returns cannam@265: * is nominal and must never be dereferenced. cannam@265: */ c@75: class PreservingPluginHandleMapper : public PluginHandleMapper c@75: { cannam@265: class NotAPlugin : public Vamp::Plugin cannam@265: { cannam@265: #define STR(x) std::string get##x() const { return "not-a-plugin"; } cannam@265: public: cannam@265: STR(Identifier) STR(Name) STR(Description) STR(Maker) STR(Copyright) cannam@265: int getPluginVersion() const { return 1; } cannam@265: bool initialise(size_t, size_t, size_t) { return false; } cannam@265: void reset() { } cannam@265: InputDomain getInputDomain() const { return TimeDomain; } cannam@265: OutputList getOutputDescriptors() const { return {}; } cannam@265: FeatureSet process(const float *const *, Vamp::RealTime) { return {}; } cannam@265: FeatureSet getRemainingFeatures() { return {}; } cannam@265: NotAPlugin() : Plugin(1) { } cannam@265: }; cannam@265: c@75: public: c@75: PreservingPluginHandleMapper() : cannam@265: m_handle(INVALID_HANDLE), cannam@265: m_plugin(nullptr), c@75: m_omapper(std::make_shared()) { } c@75: cannam@265: virtual ~PreservingPluginHandleMapper() { cannam@265: delete m_plugin; cannam@265: } cannam@265: c@75: virtual Handle pluginToHandle(Vamp::Plugin *p) const noexcept { c@75: if (!p) return INVALID_HANDLE; c@75: if (p == m_plugin) return m_handle; c@75: else { c@75: std::cerr << "PreservingPluginHandleMapper: p = " << p c@75: << " differs from saved m_plugin " << m_plugin c@75: << " (not returning saved handle " << m_handle << ")" c@75: << std::endl; c@75: return INVALID_HANDLE; c@75: } c@75: } c@75: c@75: virtual Vamp::Plugin *handleToPlugin(Handle h) const noexcept { c@75: if (h == INVALID_HANDLE) return nullptr; cannam@265: if (h == m_handle) return m_plugin; cannam@265: if (m_handle != INVALID_HANDLE) { cannam@265: std::cerr << "PreservingPluginHandleMapper: m_handle " << m_handle cannam@277: << " is non-null when a new handle (" << h cannam@277: << ") is provided, but " cannam@265: << "this stupid stub class can only handle one handle" cannam@265: << std::endl; cannam@265: return nullptr; cannam@265: } c@75: m_handle = h; cannam@265: // We allocate something here, just so that we can cannam@265: // sanity-check in the pluginToHandle call that the thing cannam@265: // passed in is likely to be the pointer we returned from cannam@265: // handleToPlugin earlier. Allocating an actual plugin allows cannam@265: // us to return it without running afoul of strict-aliasing cannam@265: // rules or the C++ object memory model. cannam@265: m_plugin = new NotAPlugin(); cannam@265: return m_plugin; c@75: } c@75: c@75: virtual const std::shared_ptr pluginToOutputIdMapper c@75: (Vamp::Plugin *p) const noexcept { c@75: if (!p) return {}; c@75: return m_omapper; c@75: } c@75: c@75: virtual const std::shared_ptr handleToOutputIdMapper c@75: (Handle h) const noexcept { c@75: if (h == INVALID_HANDLE) return {}; c@75: return m_omapper; c@75: } c@75: c@75: private: c@75: mutable Handle m_handle; cannam@265: mutable NotAPlugin *m_plugin; c@75: std::shared_ptr m_omapper; c@75: }; c@75: c@75: } c@75: c@75: #endif