comparison bits/CountingPluginHandleMapper.h @ 51:f4244a2d55ac

Introduce and use output id mappers
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 16 Sep 2016 15:10:57 +0100
parents 55d69b26d4db
children e90fd30990eb
comparison
equal deleted inserted replaced
50:12e3b396544c 51:f4244a2d55ac
34 34
35 #ifndef VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H 35 #ifndef VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H
36 #define VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H 36 #define VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H
37 37
38 #include "PluginHandleMapper.h" 38 #include "PluginHandleMapper.h"
39 #include "PluginOutputIdMapper.h"
40 #include "DefaultPluginOutputIdMapper.h"
39 41
40 #include <set> 42 #include <set>
41 #include <map> 43 #include <map>
42 44
43 namespace vampipe { 45 namespace vampipe {
46 class CountingPluginHandleMapper : public PluginHandleMapper 48 class CountingPluginHandleMapper : public PluginHandleMapper
47 { 49 {
48 public: 50 public:
49 CountingPluginHandleMapper() : m_nextHandle(1) { } 51 CountingPluginHandleMapper() : m_nextHandle(1) { }
50 52
53 ~CountingPluginHandleMapper() {
54 for (auto &o: m_outputMappers) {
55 delete o.second;
56 }
57 }
58
51 void addPlugin(Vamp::Plugin *p) { 59 void addPlugin(Vamp::Plugin *p) {
52 if (m_rplugins.find(p) == m_rplugins.end()) { 60 if (m_rplugins.find(p) == m_rplugins.end()) {
53 int32_t h = m_nextHandle++; 61 Handle h = m_nextHandle++;
54 m_plugins[h] = p; 62 m_plugins[h] = p;
55 m_rplugins[p] = h; 63 m_rplugins[p] = h;
64 m_outputMappers[h] = new DefaultPluginOutputIdMapper(p);
56 } 65 }
57 } 66 }
58 67
59 void removePlugin(int32_t h) { 68 void removePlugin(Handle h) {
60 if (m_plugins.find(h) == m_plugins.end()) { 69 if (m_plugins.find(h) == m_plugins.end()) {
61 throw NotFound(); 70 throw NotFound();
62 } 71 }
63 Vamp::Plugin *p = m_plugins[h]; 72 Vamp::Plugin *p = m_plugins[h];
73 delete m_outputMappers.at(h);
74 m_outputMappers.erase(h);
64 m_plugins.erase(h); 75 m_plugins.erase(h);
65 if (isConfigured(h)) { 76 if (isConfigured(h)) {
66 m_configuredPlugins.erase(h); 77 m_configuredPlugins.erase(h);
67 m_channelCounts.erase(h); 78 m_channelCounts.erase(h);
68 } 79 }
69 m_rplugins.erase(p); 80 m_rplugins.erase(p);
70 } 81 }
71 82
72 int32_t pluginToHandle(Vamp::Plugin *p) const { 83 Handle pluginToHandle(Vamp::Plugin *p) const {
73 if (m_rplugins.find(p) == m_rplugins.end()) { 84 if (m_rplugins.find(p) == m_rplugins.end()) {
74 throw NotFound(); 85 throw NotFound();
75 } 86 }
76 return m_rplugins.at(p); 87 return m_rplugins.at(p);
77 } 88 }
78 89
79 Vamp::Plugin *handleToPlugin(int32_t h) const { 90 Vamp::Plugin *handleToPlugin(Handle h) const {
80 if (m_plugins.find(h) == m_plugins.end()) { 91 if (m_plugins.find(h) == m_plugins.end()) {
81 throw NotFound(); 92 throw NotFound();
82 } 93 }
83 return m_plugins.at(h); 94 return m_plugins.at(h);
84 } 95 }
85 96
86 bool isConfigured(int32_t h) const { 97 //!!! iffy: mapper will move when another plugin is added. return by value?
98 const PluginOutputIdMapper &pluginToOutputIdMapper(Vamp::Plugin *p) const {
99 // pluginToHandle checks the plugin has been registered with us
100 return *m_outputMappers.at(pluginToHandle(p));
101 }
102
103 //!!! iffy: mapper will move when another plugin is added. return by value?
104 const PluginOutputIdMapper &handleToOutputIdMapper(Handle h) const {
105 if (m_plugins.find(h) == m_plugins.end()) {
106 throw NotFound();
107 }
108 return *m_outputMappers.at(h);
109 }
110
111 bool isConfigured(Handle h) const {
87 return m_configuredPlugins.find(h) != m_configuredPlugins.end(); 112 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
88 } 113 }
89 114
90 void markConfigured(int32_t h, int channelCount, int blockSize) { 115 void markConfigured(Handle h, int channelCount, int blockSize) {
91 m_configuredPlugins.insert(h); 116 m_configuredPlugins.insert(h);
92 m_channelCounts[h] = channelCount; 117 m_channelCounts[h] = channelCount;
93 m_blockSizes[h] = blockSize; 118 m_blockSizes[h] = blockSize;
94 } 119 }
95 120
96 int getChannelCount(int32_t h) const { 121 int getChannelCount(Handle h) const {
97 if (m_channelCounts.find(h) == m_channelCounts.end()) { 122 if (m_channelCounts.find(h) == m_channelCounts.end()) {
98 throw NotFound(); 123 throw NotFound();
99 } 124 }
100 return m_channelCounts.at(h); 125 return m_channelCounts.at(h);
101 } 126 }
102 127
103 int getBlockSize(int32_t h) const { 128 int getBlockSize(Handle h) const {
104 if (m_blockSizes.find(h) == m_blockSizes.end()) { 129 if (m_blockSizes.find(h) == m_blockSizes.end()) {
105 throw NotFound(); 130 throw NotFound();
106 } 131 }
107 return m_blockSizes.at(h); 132 return m_blockSizes.at(h);
108 } 133 }
109 134
110 private: 135 private:
111 int32_t m_nextHandle; // NB plugin handle type must fit in JSON number 136 Handle m_nextHandle; // NB plugin handle type must fit in JSON number
112 std::map<uint32_t, Vamp::Plugin *> m_plugins; 137 std::map<Handle, Vamp::Plugin *> m_plugins;
113 std::map<Vamp::Plugin *, uint32_t> m_rplugins; 138 std::map<Vamp::Plugin *, Handle> m_rplugins;
114 std::set<uint32_t> m_configuredPlugins; 139 std::set<Handle> m_configuredPlugins;
115 std::map<uint32_t, int> m_channelCounts; 140 std::map<Handle, int> m_channelCounts;
116 std::map<uint32_t, int> m_blockSizes; 141 std::map<Handle, int> m_blockSizes;
142 std::map<Handle, DefaultPluginOutputIdMapper *> m_outputMappers;
117 }; 143 };
118 144
119 } 145 }
120 146
121 #endif 147 #endif