annotate vamp-support/PreservingPluginHandleMapper.h @ 296:50a0b4fea7f1 tip master

Merge pull request #8 from michel-slm/gcc15 Include headers needed to compile with GCC 15's -std=gnu23 default
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 27 Jan 2025 08:53:58 +0000
parents 4b581a498981
children
rev   line source
c@75 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@75 2
c@75 3 /*
c@75 4 Piper C++
c@75 5
c@75 6 Centre for Digital Music, Queen Mary, University of London.
c@75 7 Copyright 2006-2016 Chris Cannam and QMUL.
c@75 8
c@75 9 Permission is hereby granted, free of charge, to any person
c@75 10 obtaining a copy of this software and associated documentation
c@75 11 files (the "Software"), to deal in the Software without
c@75 12 restriction, including without limitation the rights to use, copy,
c@75 13 modify, merge, publish, distribute, sublicense, and/or sell copies
c@75 14 of the Software, and to permit persons to whom the Software is
c@75 15 furnished to do so, subject to the following conditions:
c@75 16
c@75 17 The above copyright notice and this permission notice shall be
c@75 18 included in all copies or substantial portions of the Software.
c@75 19
c@75 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@75 21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@75 22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@75 23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
c@75 24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@75 25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@75 26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@75 27
c@75 28 Except as contained in this notice, the names of the Centre for
c@75 29 Digital Music; Queen Mary, University of London; and Chris Cannam
c@75 30 shall not be used in advertising or otherwise to promote the sale,
c@75 31 use or other dealings in this Software without prior written
c@75 32 authorization.
c@75 33 */
c@75 34
c@75 35 #ifndef PIPER_PRESERVING_PLUGIN_HANDLE_MAPPER_H
c@75 36 #define PIPER_PRESERVING_PLUGIN_HANDLE_MAPPER_H
c@75 37
c@75 38 #include "PluginHandleMapper.h"
c@75 39 #include "PreservingPluginOutputIdMapper.h"
c@75 40
c@75 41 #include <iostream>
c@75 42
c@97 43 namespace piper_vamp {
c@75 44
cannam@265 45 /**
cannam@265 46 * A PluginHandleMapper that accepts a handle in the handleToPlugin
cannam@265 47 * method, storing it for later, and returns the same handle from
cannam@265 48 * pluginToHandle when given the same plugin pointer as it had earlier
cannam@265 49 * returned from handleToPlugin. It can only remember one handle, and
cannam@265 50 * knows nothing about actual plugins - the plugin pointer it returns
cannam@265 51 * is nominal and must never be dereferenced.
cannam@265 52 */
c@75 53 class PreservingPluginHandleMapper : public PluginHandleMapper
c@75 54 {
cannam@265 55 class NotAPlugin : public Vamp::Plugin
cannam@265 56 {
cannam@280 57 #define STR(x) std::string get##x() const override { return "not-a-plugin"; }
cannam@265 58 public:
cannam@265 59 STR(Identifier) STR(Name) STR(Description) STR(Maker) STR(Copyright)
cannam@280 60 int getPluginVersion() const override { return 1; }
cannam@280 61 bool initialise(size_t, size_t, size_t) override { return false; }
cannam@280 62 void reset() override { }
cannam@280 63 InputDomain getInputDomain() const override { return TimeDomain; }
cannam@280 64 OutputList getOutputDescriptors() const override { return {}; }
cannam@280 65 FeatureSet process(const float *const *, Vamp::RealTime) override { return {}; }
cannam@280 66 FeatureSet getRemainingFeatures() override { return {}; }
cannam@265 67 NotAPlugin() : Plugin(1) { }
cannam@265 68 };
cannam@265 69
c@75 70 public:
c@75 71 PreservingPluginHandleMapper() :
cannam@265 72 m_handle(INVALID_HANDLE),
cannam@265 73 m_plugin(nullptr),
c@75 74 m_omapper(std::make_shared<PreservingPluginOutputIdMapper>()) { }
c@75 75
cannam@265 76 virtual ~PreservingPluginHandleMapper() {
cannam@265 77 delete m_plugin;
cannam@265 78 }
cannam@265 79
cannam@280 80 Handle pluginToHandle(Vamp::Plugin *p) const noexcept override {
c@75 81 if (!p) return INVALID_HANDLE;
c@75 82 if (p == m_plugin) return m_handle;
c@75 83 else {
c@75 84 std::cerr << "PreservingPluginHandleMapper: p = " << p
c@75 85 << " differs from saved m_plugin " << m_plugin
c@75 86 << " (not returning saved handle " << m_handle << ")"
c@75 87 << std::endl;
c@75 88 return INVALID_HANDLE;
c@75 89 }
c@75 90 }
c@75 91
cannam@280 92 Vamp::Plugin *handleToPlugin(Handle h) const noexcept override {
c@75 93 if (h == INVALID_HANDLE) return nullptr;
cannam@265 94 if (h == m_handle) return m_plugin;
cannam@265 95 if (m_handle != INVALID_HANDLE) {
cannam@265 96 std::cerr << "PreservingPluginHandleMapper: m_handle " << m_handle
cannam@277 97 << " is non-null when a new handle (" << h
cannam@277 98 << ") is provided, but "
cannam@265 99 << "this stupid stub class can only handle one handle"
cannam@265 100 << std::endl;
cannam@265 101 return nullptr;
cannam@265 102 }
c@75 103 m_handle = h;
cannam@265 104 // We allocate something here, just so that we can
cannam@265 105 // sanity-check in the pluginToHandle call that the thing
cannam@265 106 // passed in is likely to be the pointer we returned from
cannam@265 107 // handleToPlugin earlier. Allocating an actual plugin allows
cannam@265 108 // us to return it without running afoul of strict-aliasing
cannam@265 109 // rules or the C++ object memory model.
cannam@265 110 m_plugin = new NotAPlugin();
cannam@265 111 return m_plugin;
c@75 112 }
c@75 113
cannam@280 114 const std::shared_ptr<PluginOutputIdMapper> pluginToOutputIdMapper
cannam@280 115 (Vamp::Plugin *p) const noexcept override {
c@75 116 if (!p) return {};
c@75 117 return m_omapper;
c@75 118 }
c@75 119
cannam@280 120 const std::shared_ptr<PluginOutputIdMapper> handleToOutputIdMapper
cannam@280 121 (Handle h) const noexcept override {
c@75 122 if (h == INVALID_HANDLE) return {};
c@75 123 return m_omapper;
c@75 124 }
c@75 125
c@75 126 private:
c@75 127 mutable Handle m_handle;
cannam@265 128 mutable NotAPlugin *m_plugin;
c@75 129 std::shared_ptr<PreservingPluginOutputIdMapper> m_omapper;
c@75 130 };
c@75 131
c@75 132 }
c@75 133
c@75 134 #endif