comparison utilities/json-cli.cpp @ 40:55d69b26d4db

Pull out CountingPluginHandleMapper; consts
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 22 Aug 2016 17:16:44 +0100
parents 533ca5ca3404
children 38780f15ac8d
comparison
equal deleted inserted replaced
39:183be3bc9d7b 40:55d69b26d4db
1 1
2 #include "VampJson.h" 2 #include "VampJson.h"
3 #include "bits/CountingPluginHandleMapper.h"
3 4
4 #include <iostream> 5 #include <iostream>
5 #include <sstream> 6 #include <sstream>
6 #include <stdexcept> 7 #include <stdexcept>
7 8
12 using namespace Vamp; 13 using namespace Vamp;
13 using namespace Vamp::HostExt; 14 using namespace Vamp::HostExt;
14 using namespace json11; 15 using namespace json11;
15 using namespace vampipe; 16 using namespace vampipe;
16 17
17 class Mapper : public PluginHandleMapper 18 static CountingPluginHandleMapper mapper;
18 {
19 public:
20 Mapper() : m_nextHandle(1) { }
21
22 void addPlugin(Plugin *p) {
23 if (m_rplugins.find(p) == m_rplugins.end()) {
24 int32_t h = m_nextHandle++;
25 m_plugins[h] = p;
26 m_rplugins[p] = h;
27 }
28 }
29
30 int32_t pluginToHandle(Plugin *p) {
31 if (m_rplugins.find(p) == m_rplugins.end()) {
32 throw NotFound();
33 }
34 return m_rplugins[p];
35 }
36
37 Plugin *handleToPlugin(int32_t h) {
38 if (m_plugins.find(h) == m_plugins.end()) {
39 throw NotFound();
40 }
41 return m_plugins[h];
42 }
43
44 bool isInitialised(int32_t h) {
45 return m_initialisedPlugins.find(h) != m_initialisedPlugins.end();
46 }
47
48 void markInitialised(int32_t h) {
49 m_initialisedPlugins.insert(h);
50 }
51
52 private:
53 //!!! + mutex
54 int32_t m_nextHandle; // plugin handle type must fit in JSON number
55 map<uint32_t, Plugin *> m_plugins;
56 map<Plugin *, uint32_t> m_rplugins;
57 set<uint32_t> m_initialisedPlugins;
58 };
59
60 static Mapper mapper;
61 19
62 Vamp::HostExt::LoadResponse 20 Vamp::HostExt::LoadResponse
63 loadPlugin(json11::Json j) { 21 loadPlugin(json11::Json j) {
64 22
65 auto req = VampJson::toLoadRequest(j); 23 auto req = VampJson::toLoadRequest(j);
131 throw VampJson::Failure("malformed configuration request: " + err); 89 throw VampJson::Failure("malformed configuration request: " + err);
132 } 90 }
133 91
134 int32_t handle = j["pluginHandle"].int_value(); 92 int32_t handle = j["pluginHandle"].int_value();
135 93
136 if (mapper.isInitialised(handle)) { 94 if (mapper.isConfigured(handle)) {
137 throw VampJson::Failure("plugin has already been initialised"); 95 throw VampJson::Failure("plugin has already been configured");
138 } 96 }
139 97
140 Plugin *plugin = mapper.handleToPlugin(handle); 98 Plugin *plugin = mapper.handleToPlugin(handle);
141 99
142 Json config = j["configuration"]; 100 Json config = j["configuration"];
143 101
144 auto response = configurePlugin(plugin, config); 102 auto response = configurePlugin(plugin, config);
145 103
146 mapper.markInitialised(handle); 104 mapper.markConfigured(handle, 0, 0); //!!!
147 105
148 cerr << "Configured and initialised plugin " << handle << endl; 106 cerr << "Configured and initialised plugin " << handle << endl;
149 107
150 return VampJson::fromConfigurationResponse(response); 108 return VampJson::fromConfigurationResponse(response);
151 } 109 }