Mercurial > hg > piper-cpp
changeset 40:55d69b26d4db
Pull out CountingPluginHandleMapper; consts
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 22 Aug 2016 17:16:44 +0100 |
parents | 183be3bc9d7b |
children | 10ac36b7198a |
files | bits/CountingPluginHandleMapper.h bits/PluginHandleMapper.h bits/PreservingPluginHandleMapper.h utilities/json-cli.cpp utilities/json-to-capnp.cpp utilities/vampipe-server.cpp |
diffstat | 6 files changed, 136 insertions(+), 148 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bits/CountingPluginHandleMapper.h Mon Aug 22 17:16:44 2016 +0100 @@ -0,0 +1,121 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Vampipe + + Centre for Digital Music, Queen Mary, University of London. + Copyright 2006-2016 Chris Cannam and QMUL. + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, copy, + modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR + ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + Except as contained in this notice, the names of the Centre for + Digital Music; Queen Mary, University of London; and Chris Cannam + shall not be used in advertising or otherwise to promote the sale, + use or other dealings in this Software without prior written + authorization. +*/ + +#ifndef VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H +#define VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H + +#include "PluginHandleMapper.h" + +#include <set> +#include <map> + +namespace vampipe { + +//!!! NB not thread-safe at present, should it be? +class CountingPluginHandleMapper : public PluginHandleMapper +{ +public: + CountingPluginHandleMapper() : m_nextHandle(1) { } + + void addPlugin(Vamp::Plugin *p) { + if (m_rplugins.find(p) == m_rplugins.end()) { + int32_t h = m_nextHandle++; + m_plugins[h] = p; + m_rplugins[p] = h; + } + } + + void removePlugin(int32_t h) { + if (m_plugins.find(h) == m_plugins.end()) { + throw NotFound(); + } + Vamp::Plugin *p = m_plugins[h]; + m_plugins.erase(h); + if (isConfigured(h)) { + m_configuredPlugins.erase(h); + m_channelCounts.erase(h); + } + m_rplugins.erase(p); + } + + int32_t pluginToHandle(Vamp::Plugin *p) const { + if (m_rplugins.find(p) == m_rplugins.end()) { + throw NotFound(); + } + return m_rplugins.at(p); + } + + Vamp::Plugin *handleToPlugin(int32_t h) const { + if (m_plugins.find(h) == m_plugins.end()) { + throw NotFound(); + } + return m_plugins.at(h); + } + + bool isConfigured(int32_t h) const { + return m_configuredPlugins.find(h) != m_configuredPlugins.end(); + } + + void markConfigured(int32_t h, int channelCount, int blockSize) { + m_configuredPlugins.insert(h); + m_channelCounts[h] = channelCount; + m_blockSizes[h] = blockSize; + } + + int getChannelCount(int32_t h) const { + if (m_channelCounts.find(h) == m_channelCounts.end()) { + throw NotFound(); + } + return m_channelCounts.at(h); + } + + int getBlockSize(int32_t h) const { + if (m_blockSizes.find(h) == m_blockSizes.end()) { + throw NotFound(); + } + return m_blockSizes.at(h); + } + +private: + int32_t m_nextHandle; // NB plugin handle type must fit in JSON number + std::map<uint32_t, Vamp::Plugin *> m_plugins; + std::map<Vamp::Plugin *, uint32_t> m_rplugins; + std::set<uint32_t> m_configuredPlugins; + std::map<uint32_t, int> m_channelCounts; + std::map<uint32_t, int> m_blockSizes; +}; + +} + +#endif
--- a/bits/PluginHandleMapper.h Mon Aug 22 17:16:23 2016 +0100 +++ b/bits/PluginHandleMapper.h Mon Aug 22 17:16:44 2016 +0100 @@ -49,8 +49,8 @@ NotFound() : runtime_error("plugin or handle not found in mapper") { } }; - virtual int32_t pluginToHandle(Vamp::Plugin *) = 0; // may throw NotFound - virtual Vamp::Plugin *handleToPlugin(int32_t) = 0; // may throw NotFound + virtual int32_t pluginToHandle(Vamp::Plugin *) const = 0; // may throw NotFound + virtual Vamp::Plugin *handleToPlugin(int32_t) const = 0; // may throw NotFound }; }
--- a/bits/PreservingPluginHandleMapper.h Mon Aug 22 17:16:23 2016 +0100 +++ b/bits/PreservingPluginHandleMapper.h Mon Aug 22 17:16:44 2016 +0100 @@ -46,7 +46,7 @@ public: PreservingPluginHandleMapper() : m_handle(0), m_plugin(0) { } - virtual int32_t pluginToHandle(Vamp::Plugin *p) { + virtual int32_t pluginToHandle(Vamp::Plugin *p) const { if (p == m_plugin) return m_handle; else { std::cerr << "PreservingPluginHandleMapper: p = " << p @@ -57,15 +57,15 @@ } } - virtual Vamp::Plugin *handleToPlugin(int32_t h) { + virtual Vamp::Plugin *handleToPlugin(int32_t h) const { m_handle = h; m_plugin = reinterpret_cast<Vamp::Plugin *>(h); return m_plugin; } private: - int32_t m_handle; - Vamp::Plugin *m_plugin; + mutable int32_t m_handle; + mutable Vamp::Plugin *m_plugin; }; }
--- a/utilities/json-cli.cpp Mon Aug 22 17:16:23 2016 +0100 +++ b/utilities/json-cli.cpp Mon Aug 22 17:16:44 2016 +0100 @@ -1,5 +1,6 @@ #include "VampJson.h" +#include "bits/CountingPluginHandleMapper.h" #include <iostream> #include <sstream> @@ -14,50 +15,7 @@ using namespace json11; using namespace vampipe; -class Mapper : public PluginHandleMapper -{ -public: - Mapper() : m_nextHandle(1) { } - - void addPlugin(Plugin *p) { - if (m_rplugins.find(p) == m_rplugins.end()) { - int32_t h = m_nextHandle++; - m_plugins[h] = p; - m_rplugins[p] = h; - } - } - - int32_t pluginToHandle(Plugin *p) { - if (m_rplugins.find(p) == m_rplugins.end()) { - throw NotFound(); - } - return m_rplugins[p]; - } - - Plugin *handleToPlugin(int32_t h) { - if (m_plugins.find(h) == m_plugins.end()) { - throw NotFound(); - } - return m_plugins[h]; - } - - bool isInitialised(int32_t h) { - return m_initialisedPlugins.find(h) != m_initialisedPlugins.end(); - } - - void markInitialised(int32_t h) { - m_initialisedPlugins.insert(h); - } - -private: -//!!! + mutex - int32_t m_nextHandle; // plugin handle type must fit in JSON number - map<uint32_t, Plugin *> m_plugins; - map<Plugin *, uint32_t> m_rplugins; - set<uint32_t> m_initialisedPlugins; -}; - -static Mapper mapper; +static CountingPluginHandleMapper mapper; Vamp::HostExt::LoadResponse loadPlugin(json11::Json j) { @@ -133,8 +91,8 @@ int32_t handle = j["pluginHandle"].int_value(); - if (mapper.isInitialised(handle)) { - throw VampJson::Failure("plugin has already been initialised"); + if (mapper.isConfigured(handle)) { + throw VampJson::Failure("plugin has already been configured"); } Plugin *plugin = mapper.handleToPlugin(handle); @@ -143,7 +101,7 @@ auto response = configurePlugin(plugin, config); - mapper.markInitialised(handle); + mapper.markConfigured(handle, 0, 0); //!!! cerr << "Configured and initialised plugin " << handle << endl;
--- a/utilities/json-to-capnp.cpp Mon Aug 22 17:16:23 2016 +0100 +++ b/utilities/json-to-capnp.cpp Mon Aug 22 17:16:44 2016 +0100 @@ -6,6 +6,8 @@ #include <sstream> #include <stdexcept> +#include "bits/PreservingPluginHandleMapper.h" + using namespace std; using namespace json11; using namespace vampipe; @@ -35,27 +37,6 @@ return j; } -class PreservingPluginHandleMapper : public PluginHandleMapper -{ -public: - PreservingPluginHandleMapper() : m_handle(0), m_plugin(0) { } - - virtual int32_t pluginToHandle(Vamp::Plugin *p) { - if (p == m_plugin) return m_handle; - else throw NotFound(); - } - - virtual Vamp::Plugin *handleToPlugin(int32_t h) { - m_handle = h; - m_plugin = reinterpret_cast<Vamp::Plugin *>(h); - return m_plugin; - } - -private: - int32_t m_handle; - Vamp::Plugin *m_plugin; -}; - void handle_input(::capnp::MallocMessageBuilder &message, string input) {
--- a/utilities/vampipe-server.cpp Mon Aug 22 17:16:23 2016 +0100 +++ b/utilities/vampipe-server.cpp Mon Aug 22 17:16:44 2016 +0100 @@ -2,6 +2,7 @@ #include "VampnProto.h" #include "bits/RequestOrResponse.h" +#include "bits/CountingPluginHandleMapper.h" #include <iostream> #include <sstream> @@ -27,80 +28,7 @@ exit(2); } -class Mapper : public PluginHandleMapper -{ -public: - Mapper() : m_nextHandle(1) { } - - void addPlugin(Plugin *p) { - if (m_rplugins.find(p) == m_rplugins.end()) { - int32_t h = m_nextHandle++; - m_plugins[h] = p; - m_rplugins[p] = h; - } - } - - void removePlugin(int32_t h) { - if (m_plugins.find(h) == m_plugins.end()) { - throw NotFound(); - } - Plugin *p = m_plugins[h]; - m_plugins.erase(h); - if (isConfigured(h)) { - m_configuredPlugins.erase(h); - m_channelCounts.erase(h); - } - m_rplugins.erase(p); - } - - int32_t pluginToHandle(Plugin *p) { - if (m_rplugins.find(p) == m_rplugins.end()) { - throw NotFound(); - } - return m_rplugins[p]; - } - - Plugin *handleToPlugin(int32_t h) { - if (m_plugins.find(h) == m_plugins.end()) { - throw NotFound(); - } - return m_plugins[h]; - } - - bool isConfigured(int32_t h) { - return m_configuredPlugins.find(h) != m_configuredPlugins.end(); - } - - void markConfigured(int32_t h, int channelCount, int blockSize) { - m_configuredPlugins.insert(h); - m_channelCounts[h] = channelCount; - m_blockSizes[h] = blockSize; - } - - int getChannelCount(int32_t h) { - if (m_channelCounts.find(h) == m_channelCounts.end()) { - throw NotFound(); - } - return m_channelCounts[h]; - } - - int getBlockSize(int32_t h) { - if (m_blockSizes.find(h) == m_blockSizes.end()) { - throw NotFound(); - } - return m_blockSizes[h]; - } - -private: - int32_t m_nextHandle; // NB plugin handle type must fit in JSON number - map<uint32_t, Plugin *> m_plugins; - map<Plugin *, uint32_t> m_rplugins; - set<uint32_t> m_configuredPlugins; - map<uint32_t, int> m_channelCounts; - map<uint32_t, int> m_blockSizes; -}; - -static Mapper mapper; +static CountingPluginHandleMapper mapper; RequestOrResponse readRequestCapnp()