comparison vamp-support/PreservingPluginHandleMapper.h @ 265:7ada63fe1084

Some neatening
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 12 Oct 2018 22:18:37 +0100
parents 9a044706ab73
children 5ac494d998b2
comparison
equal deleted inserted replaced
264:37760b5376b3 265:7ada63fe1084
40 40
41 #include <iostream> 41 #include <iostream>
42 42
43 namespace piper_vamp { 43 namespace piper_vamp {
44 44
45 //!!! document -- this is a passthrough thing for a single plugin 45 /**
46 //!!! handle only, it does not use actually valid Plugin pointers at 46 * A PluginHandleMapper that accepts a handle in the handleToPlugin
47 //!!! all. Or, better, reimplement in a way that doesn't involve 47 * method, storing it for later, and returns the same handle from
48 //!!! such alarmingly invalid reinterpret_casts 48 * pluginToHandle when given the same plugin pointer as it had earlier
49 49 * returned from handleToPlugin. It can only remember one handle, and
50 * knows nothing about actual plugins - the plugin pointer it returns
51 * is nominal and must never be dereferenced.
52 */
50 class PreservingPluginHandleMapper : public PluginHandleMapper 53 class PreservingPluginHandleMapper : public PluginHandleMapper
51 { 54 {
55 class NotAPlugin : public Vamp::Plugin
56 {
57 #define STR(x) std::string get##x() const { return "not-a-plugin"; }
58 public:
59 STR(Identifier) STR(Name) STR(Description) STR(Maker) STR(Copyright)
60 int getPluginVersion() const { return 1; }
61 bool initialise(size_t, size_t, size_t) { return false; }
62 void reset() { }
63 InputDomain getInputDomain() const { return TimeDomain; }
64 OutputList getOutputDescriptors() const { return {}; }
65 FeatureSet process(const float *const *, Vamp::RealTime) { return {}; }
66 FeatureSet getRemainingFeatures() { return {}; }
67 NotAPlugin() : Plugin(1) { }
68 };
69
52 public: 70 public:
53 PreservingPluginHandleMapper() : 71 PreservingPluginHandleMapper() :
54 m_handle(0), 72 m_handle(INVALID_HANDLE),
55 m_plugin(0), 73 m_plugin(nullptr),
56 m_omapper(std::make_shared<PreservingPluginOutputIdMapper>()) { } 74 m_omapper(std::make_shared<PreservingPluginOutputIdMapper>()) { }
75
76 virtual ~PreservingPluginHandleMapper() {
77 delete m_plugin;
78 }
57 79
58 virtual Handle pluginToHandle(Vamp::Plugin *p) const noexcept { 80 virtual Handle pluginToHandle(Vamp::Plugin *p) const noexcept {
59 if (!p) return INVALID_HANDLE; 81 if (!p) return INVALID_HANDLE;
60 if (p == m_plugin) return m_handle; 82 if (p == m_plugin) return m_handle;
61 else { 83 else {
67 } 89 }
68 } 90 }
69 91
70 virtual Vamp::Plugin *handleToPlugin(Handle h) const noexcept { 92 virtual Vamp::Plugin *handleToPlugin(Handle h) const noexcept {
71 if (h == INVALID_HANDLE) return nullptr; 93 if (h == INVALID_HANDLE) return nullptr;
94 if (h == m_handle) return m_plugin;
95 if (m_handle != INVALID_HANDLE) {
96 std::cerr << "PreservingPluginHandleMapper: m_handle " << m_handle
97 << " is non-null when a new handle is provided, but "
98 << "this stupid stub class can only handle one handle"
99 << std::endl;
100 return nullptr;
101 }
72 m_handle = h; 102 m_handle = h;
73 //!!! see comment at top 103 // We allocate something here, just so that we can
74 m_plugin = reinterpret_cast<Vamp::Plugin *>(h); 104 // sanity-check in the pluginToHandle call that the thing
75 return m_plugin; 105 // passed in is likely to be the pointer we returned from
106 // handleToPlugin earlier. Allocating an actual plugin allows
107 // us to return it without running afoul of strict-aliasing
108 // rules or the C++ object memory model.
109 m_plugin = new NotAPlugin();
110 return m_plugin;
76 } 111 }
77 112
78 virtual const std::shared_ptr<PluginOutputIdMapper> pluginToOutputIdMapper 113 virtual const std::shared_ptr<PluginOutputIdMapper> pluginToOutputIdMapper
79 (Vamp::Plugin *p) const noexcept { 114 (Vamp::Plugin *p) const noexcept {
80 if (!p) return {}; 115 if (!p) return {};
87 return m_omapper; 122 return m_omapper;
88 } 123 }
89 124
90 private: 125 private:
91 mutable Handle m_handle; 126 mutable Handle m_handle;
92 mutable Vamp::Plugin *m_plugin; 127 mutable NotAPlugin *m_plugin;
93 std::shared_ptr<PreservingPluginOutputIdMapper> m_omapper; 128 std::shared_ptr<PreservingPluginOutputIdMapper> m_omapper;
94 }; 129 };
95 130
96 } 131 }
97 132