c@40: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@40: c@40: /* c@40: Vampipe c@40: c@40: Centre for Digital Music, Queen Mary, University of London. c@40: Copyright 2006-2016 Chris Cannam and QMUL. c@40: c@40: Permission is hereby granted, free of charge, to any person c@40: obtaining a copy of this software and associated documentation c@40: files (the "Software"), to deal in the Software without c@40: restriction, including without limitation the rights to use, copy, c@40: modify, merge, publish, distribute, sublicense, and/or sell copies c@40: of the Software, and to permit persons to whom the Software is c@40: furnished to do so, subject to the following conditions: c@40: c@40: The above copyright notice and this permission notice shall be c@40: included in all copies or substantial portions of the Software. c@40: c@40: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@40: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@40: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@40: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR c@40: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@40: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@40: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@40: c@40: Except as contained in this notice, the names of the Centre for c@40: Digital Music; Queen Mary, University of London; and Chris Cannam c@40: shall not be used in advertising or otherwise to promote the sale, c@40: use or other dealings in this Software without prior written c@40: authorization. c@40: */ c@40: c@40: #ifndef VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H c@40: #define VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H c@40: c@40: #include "PluginHandleMapper.h" c@40: c@40: #include c@40: #include c@40: c@40: namespace vampipe { c@40: c@40: //!!! NB not thread-safe at present, should it be? c@40: class CountingPluginHandleMapper : public PluginHandleMapper c@40: { c@40: public: c@40: CountingPluginHandleMapper() : m_nextHandle(1) { } c@40: c@40: void addPlugin(Vamp::Plugin *p) { c@40: if (m_rplugins.find(p) == m_rplugins.end()) { c@40: int32_t h = m_nextHandle++; c@40: m_plugins[h] = p; c@40: m_rplugins[p] = h; c@40: } c@40: } c@40: c@40: void removePlugin(int32_t h) { c@40: if (m_plugins.find(h) == m_plugins.end()) { c@40: throw NotFound(); c@40: } c@40: Vamp::Plugin *p = m_plugins[h]; c@40: m_plugins.erase(h); c@40: if (isConfigured(h)) { c@40: m_configuredPlugins.erase(h); c@40: m_channelCounts.erase(h); c@40: } c@40: m_rplugins.erase(p); c@40: } c@40: c@40: int32_t pluginToHandle(Vamp::Plugin *p) const { c@40: if (m_rplugins.find(p) == m_rplugins.end()) { c@40: throw NotFound(); c@40: } c@40: return m_rplugins.at(p); c@40: } c@40: c@40: Vamp::Plugin *handleToPlugin(int32_t h) const { c@40: if (m_plugins.find(h) == m_plugins.end()) { c@40: throw NotFound(); c@40: } c@40: return m_plugins.at(h); c@40: } c@40: c@40: bool isConfigured(int32_t h) const { c@40: return m_configuredPlugins.find(h) != m_configuredPlugins.end(); c@40: } c@40: c@40: void markConfigured(int32_t h, int channelCount, int blockSize) { c@40: m_configuredPlugins.insert(h); c@40: m_channelCounts[h] = channelCount; c@40: m_blockSizes[h] = blockSize; c@40: } c@40: c@40: int getChannelCount(int32_t h) const { c@40: if (m_channelCounts.find(h) == m_channelCounts.end()) { c@40: throw NotFound(); c@40: } c@40: return m_channelCounts.at(h); c@40: } c@40: c@40: int getBlockSize(int32_t h) const { c@40: if (m_blockSizes.find(h) == m_blockSizes.end()) { c@40: throw NotFound(); c@40: } c@40: return m_blockSizes.at(h); c@40: } c@40: c@40: private: c@40: int32_t m_nextHandle; // NB plugin handle type must fit in JSON number c@40: std::map m_plugins; c@40: std::map m_rplugins; c@40: std::set m_configuredPlugins; c@40: std::map m_channelCounts; c@40: std::map m_blockSizes; c@40: }; c@40: c@40: } c@40: c@40: #endif