comparison bits/CountingPluginHandleMapper.h @ 40:55d69b26d4db

Pull out CountingPluginHandleMapper; consts
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 22 Aug 2016 17:16:44 +0100
parents
children f4244a2d55ac
comparison
equal deleted inserted replaced
39:183be3bc9d7b 40:55d69b26d4db
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vampipe
5
6 Centre for Digital Music, Queen Mary, University of London.
7 Copyright 2006-2016 Chris Cannam and QMUL.
8
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use, copy,
13 modify, merge, publish, distribute, sublicense, and/or sell copies
14 of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
24 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the names of the Centre for
29 Digital Music; Queen Mary, University of London; and Chris Cannam
30 shall not be used in advertising or otherwise to promote the sale,
31 use or other dealings in this Software without prior written
32 authorization.
33 */
34
35 #ifndef VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H
36 #define VAMPIPE_COUNTING_PLUGIN_HANDLE_MAPPER_H
37
38 #include "PluginHandleMapper.h"
39
40 #include <set>
41 #include <map>
42
43 namespace vampipe {
44
45 //!!! NB not thread-safe at present, should it be?
46 class CountingPluginHandleMapper : public PluginHandleMapper
47 {
48 public:
49 CountingPluginHandleMapper() : m_nextHandle(1) { }
50
51 void addPlugin(Vamp::Plugin *p) {
52 if (m_rplugins.find(p) == m_rplugins.end()) {
53 int32_t h = m_nextHandle++;
54 m_plugins[h] = p;
55 m_rplugins[p] = h;
56 }
57 }
58
59 void removePlugin(int32_t h) {
60 if (m_plugins.find(h) == m_plugins.end()) {
61 throw NotFound();
62 }
63 Vamp::Plugin *p = m_plugins[h];
64 m_plugins.erase(h);
65 if (isConfigured(h)) {
66 m_configuredPlugins.erase(h);
67 m_channelCounts.erase(h);
68 }
69 m_rplugins.erase(p);
70 }
71
72 int32_t pluginToHandle(Vamp::Plugin *p) const {
73 if (m_rplugins.find(p) == m_rplugins.end()) {
74 throw NotFound();
75 }
76 return m_rplugins.at(p);
77 }
78
79 Vamp::Plugin *handleToPlugin(int32_t h) const {
80 if (m_plugins.find(h) == m_plugins.end()) {
81 throw NotFound();
82 }
83 return m_plugins.at(h);
84 }
85
86 bool isConfigured(int32_t h) const {
87 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
88 }
89
90 void markConfigured(int32_t h, int channelCount, int blockSize) {
91 m_configuredPlugins.insert(h);
92 m_channelCounts[h] = channelCount;
93 m_blockSizes[h] = blockSize;
94 }
95
96 int getChannelCount(int32_t h) const {
97 if (m_channelCounts.find(h) == m_channelCounts.end()) {
98 throw NotFound();
99 }
100 return m_channelCounts.at(h);
101 }
102
103 int getBlockSize(int32_t h) const {
104 if (m_blockSizes.find(h) == m_blockSizes.end()) {
105 throw NotFound();
106 }
107 return m_blockSizes.at(h);
108 }
109
110 private:
111 int32_t m_nextHandle; // NB plugin handle type must fit in JSON number
112 std::map<uint32_t, Vamp::Plugin *> m_plugins;
113 std::map<Vamp::Plugin *, uint32_t> m_rplugins;
114 std::set<uint32_t> m_configuredPlugins;
115 std::map<uint32_t, int> m_channelCounts;
116 std::map<uint32_t, int> m_blockSizes;
117 };
118
119 }
120
121 #endif