c@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@49: c@49: /* c@49: Vampipe c@49: c@49: Centre for Digital Music, Queen Mary, University of London. c@49: Copyright 2006-2016 Chris Cannam and QMUL. c@49: c@49: Permission is hereby granted, free of charge, to any person c@49: obtaining a copy of this software and associated documentation c@49: files (the "Software"), to deal in the Software without c@49: restriction, including without limitation the rights to use, copy, c@49: modify, merge, publish, distribute, sublicense, and/or sell copies c@49: of the Software, and to permit persons to whom the Software is c@49: furnished to do so, subject to the following conditions: c@49: c@49: The above copyright notice and this permission notice shall be c@49: included in all copies or substantial portions of the Software. c@49: c@49: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@49: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@49: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@49: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR c@49: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@49: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@49: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@49: c@49: Except as contained in this notice, the names of the Centre for c@49: Digital Music; Queen Mary, University of London; and Chris Cannam c@49: shall not be used in advertising or otherwise to promote the sale, c@49: use or other dealings in this Software without prior written c@49: authorization. c@49: */ c@49: c@49: #ifndef VAMPIPE_PLUGIN_ID_MAPPER_H c@49: #define VAMPIPE_PLUGIN_ID_MAPPER_H c@49: c@49: #include c@49: c@49: #include c@49: #include c@49: c@49: namespace vampipe { c@49: c@49: class PluginOutputIdMapper c@49: { c@49: // NB not threadsafe. Does this matter? c@49: c@49: //!!! simplify. A single vector is almost certainly faster. c@49: c@49: public: c@49: PluginOutputIdMapper(Vamp::Plugin *p) : m_plugin(p) { } c@49: c@49: class NotFound : virtual public std::runtime_error { c@49: public: c@49: NotFound() : runtime_error("output id or index not found in mapper") { } c@49: }; c@49: c@49: int idToIndex(std::string outputId) const { c@49: if (m_idIndexMap.empty()) populate(); c@49: auto i = m_idIndexMap.find(outputId); c@49: if (i == m_idIndexMap.end()) throw NotFound(); c@49: return i->second; c@49: } c@49: c@49: std::string indexToId(int index) const { c@49: if (m_ids.empty()) populate(); c@49: if (index < 0 || size_t(index) >= m_ids.size()) throw NotFound(); c@49: return m_ids[index]; c@49: } c@49: c@49: private: c@49: Vamp::Plugin *m_plugin; c@49: mutable std::vector m_ids; c@49: mutable std::map m_idIndexMap; c@49: c@49: void populate() const { c@49: Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors(); c@49: for (const auto &d: outputs) { c@49: m_idIndexMap[d.identifier] = m_ids.size(); c@49: m_ids.push_back(d.identifier); c@49: } c@49: } c@49: }; c@49: c@49: } c@49: c@49: #endif