comparison bits/CountingPluginHandleMapper.h @ 57:7aec704705c7

Make the output ID mapper stuff safer by using shared_ptr
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 19 Sep 2016 14:43:32 +0100
parents e90fd30990eb
children 8a4bcb3dc3a6
comparison
equal deleted inserted replaced
56:815e94fedc1c 57:7aec704705c7
48 class CountingPluginHandleMapper : public PluginHandleMapper 48 class CountingPluginHandleMapper : public PluginHandleMapper
49 { 49 {
50 public: 50 public:
51 CountingPluginHandleMapper() : m_nextHandle(1) { } 51 CountingPluginHandleMapper() : m_nextHandle(1) { }
52 52
53 ~CountingPluginHandleMapper() {
54 for (auto &o: m_outputMappers) {
55 delete o.second;
56 }
57 }
58
59 void addPlugin(Vamp::Plugin *p) { 53 void addPlugin(Vamp::Plugin *p) {
60 if (m_rplugins.find(p) == m_rplugins.end()) { 54 if (m_rplugins.find(p) == m_rplugins.end()) {
61 Handle h = m_nextHandle++; 55 Handle h = m_nextHandle++;
62 m_plugins[h] = p; 56 m_plugins[h] = p;
63 m_rplugins[p] = h; 57 m_rplugins[p] = h;
64 m_outputMappers[h] = new DefaultPluginOutputIdMapper(p); 58 m_outputMappers[h] =
59 std::make_shared<DefaultPluginOutputIdMapper>(p);
65 } 60 }
66 } 61 }
67 62
68 void removePlugin(Handle h) { 63 void removePlugin(Handle h) {
69 if (m_plugins.find(h) == m_plugins.end()) { 64 if (m_plugins.find(h) == m_plugins.end()) {
70 std::cerr << "remove: no handle " << h << std::endl;
71 throw NotFound(); 65 throw NotFound();
72 } 66 }
73 Vamp::Plugin *p = m_plugins[h]; 67 Vamp::Plugin *p = m_plugins[h];
74 delete m_outputMappers.at(h);
75 m_outputMappers.erase(h); 68 m_outputMappers.erase(h);
76 m_plugins.erase(h); 69 m_plugins.erase(h);
77 if (isConfigured(h)) { 70 if (isConfigured(h)) {
78 m_configuredPlugins.erase(h); 71 m_configuredPlugins.erase(h);
79 m_channelCounts.erase(h); 72 m_channelCounts.erase(h);
81 m_rplugins.erase(p); 74 m_rplugins.erase(p);
82 } 75 }
83 76
84 Handle pluginToHandle(Vamp::Plugin *p) const { 77 Handle pluginToHandle(Vamp::Plugin *p) const {
85 if (m_rplugins.find(p) == m_rplugins.end()) { 78 if (m_rplugins.find(p) == m_rplugins.end()) {
86 std::cerr << "pluginToHandle: no plugin " << p << std::endl;
87 throw NotFound(); 79 throw NotFound();
88 } 80 }
89 return m_rplugins.at(p); 81 return m_rplugins.at(p);
90 } 82 }
91 83
92 Vamp::Plugin *handleToPlugin(Handle h) const { 84 Vamp::Plugin *handleToPlugin(Handle h) const {
93 if (m_plugins.find(h) == m_plugins.end()) { 85 if (m_plugins.find(h) == m_plugins.end()) {
94 std::cerr << "handleToPlugin: no handle " << h << std::endl;
95 throw NotFound(); 86 throw NotFound();
96 } 87 }
97 return m_plugins.at(h); 88 return m_plugins.at(h);
98 } 89 }
99 90
100 //!!! iffy: mapper will move when another plugin is added. return by value? 91 const std::shared_ptr<PluginOutputIdMapper> pluginToOutputIdMapper
101 const PluginOutputIdMapper &pluginToOutputIdMapper(Vamp::Plugin *p) const { 92 (Vamp::Plugin *p) const {
102 // pluginToHandle checks the plugin has been registered with us 93 // pluginToHandle checks the plugin has been registered with us
103 std::cerr << "output id mapper requested for plugin with handle " << pluginToHandle(p) << std::endl; 94 return m_outputMappers.at(pluginToHandle(p));
104 return *m_outputMappers.at(pluginToHandle(p));
105 } 95 }
106 96
107 //!!! iffy: mapper will move when another plugin is added. return by value? 97 const std::shared_ptr<PluginOutputIdMapper> handleToOutputIdMapper
108 const PluginOutputIdMapper &handleToOutputIdMapper(Handle h) const { 98 (Handle h) const {
109 std::cerr << "output id mapper requested for handle " << h << std::endl;
110 if (m_plugins.find(h) == m_plugins.end()) { 99 if (m_plugins.find(h) == m_plugins.end()) {
111 throw NotFound(); 100 throw NotFound();
112 } 101 }
113 return *m_outputMappers.at(h); 102 return m_outputMappers.at(h);
114 } 103 }
115 104
116 bool isConfigured(Handle h) const { 105 bool isConfigured(Handle h) const {
117 return m_configuredPlugins.find(h) != m_configuredPlugins.end(); 106 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
118 } 107 }
142 std::map<Handle, Vamp::Plugin *> m_plugins; 131 std::map<Handle, Vamp::Plugin *> m_plugins;
143 std::map<Vamp::Plugin *, Handle> m_rplugins; 132 std::map<Vamp::Plugin *, Handle> m_rplugins;
144 std::set<Handle> m_configuredPlugins; 133 std::set<Handle> m_configuredPlugins;
145 std::map<Handle, int> m_channelCounts; 134 std::map<Handle, int> m_channelCounts;
146 std::map<Handle, int> m_blockSizes; 135 std::map<Handle, int> m_blockSizes;
147 std::map<Handle, DefaultPluginOutputIdMapper *> m_outputMappers; 136 std::map<Handle, std::shared_ptr<PluginOutputIdMapper>> m_outputMappers;
148 }; 137 };
149 138
150 } 139 }
151 140
152 #endif 141 #endif