comparison vamp-stubber/stubber.cpp @ 238:0664784566d8

Generate plausible metadata names; emit empty type URIs for outputs that don't have any (to make the structure clearer)
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 13 Jun 2017 12:09:35 +0100
parents 5ba2749f2024
children d00199f3b010
comparison
equal deleted inserted replaced
237:5ba2749f2024 238:0664784566d8
72 72
73 static void usage(bool successful = false) 73 static void usage(bool successful = false)
74 { 74 {
75 cerr << "\n" << myname << 75 cerr << "\n" << myname <<
76 ": Emit stub version of main code\nfor a Piper Adapter implementation of a Vamp plugin library\n\n" 76 ": Emit stub version of main code\nfor a Piper Adapter implementation of a Vamp plugin library\n\n"
77 " Usage: " << myname << " [-d] <soname>\n" 77 " Usage: " << myname << " [-d] <libname>\n"
78 " " << myname << " -v\n" 78 " " << myname << " -v\n"
79 " " << myname << " -h\n\n" 79 " " << myname << " -h\n\n"
80 " where\n" 80 " where\n"
81 " <soname>: the Vamp plugin library name to emit stub conversion code for\n" 81 " <libname>: the Vamp plugin library name to emit stub conversion code for\n"
82 " -d: also print debug information to stderr\n"
83 " -v: print version number to stdout and exit\n" 82 " -v: print version number to stdout and exit\n"
84 " -h: print this text to stderr and exit\n\n"; 83 " -h: print this text to stderr and exit\n\n";
85 if (successful) exit(0); 84 if (successful) exit(0);
86 else exit(2); 85 else exit(2);
87 } 86 }
137 dup2(normalFd, 1); 136 dup2(normalFd, 1);
138 #endif 137 #endif
139 } 138 }
140 139
141 ListResponse 140 ListResponse
142 makeRequest(string soname, bool debug) 141 makeRequest(string libname)
143 { 142 {
144 ListRequest req; 143 ListRequest req;
145 req.from.push_back(soname); 144 req.from.push_back(libname);
146 return LoaderRequests().listPluginData(req); 145 return LoaderRequests().listPluginData(req);
147 } 146 }
148 147
149 struct PlausibleMetadata 148 struct PlausibleMetadata
150 { 149 {
153 }; 152 };
154 153
155 PlausibleMetadata 154 PlausibleMetadata
156 inventPlausibleMetadata(string key) 155 inventPlausibleMetadata(string key)
157 { 156 {
157 string className, adapterName;
158 bool initial = true, interCap = false;
159
160 int idIdx = 0;
161 for (int i = 0; i < int(key.size()); ++i) {
162 char c = key[i];
163 if (c == ':') {
164 idIdx = i + 1;
165 break;
166 }
167 }
168
169 for (int i = idIdx; i < int(key.size()); ++i) {
170 char c = key[i];
171 if (isalpha(c)) {
172 if (initial || interCap) {
173 className += toupper(c);
174 } else {
175 className += c;
176 }
177 if (interCap) {
178 adapterName += toupper(c);
179 } else {
180 adapterName += c;
181 }
182 interCap = false;
183 } else {
184 interCap = true;
185 }
186 initial = false;
187 }
188 adapterName += "Adapter";
189
158 PlausibleMetadata pm; 190 PlausibleMetadata pm;
159 pm.className = "MyPlugin";//!!! 191 pm.className = className;
160 pm.adapterName = "myPluginAdapter";//!!! 192 pm.adapterName = adapterName;
161 return pm; 193 return pm;
162 } 194 }
163 195
164 void 196 void
165 emitFor(string soname, const ListResponse &resp, bool debug) 197 emitFor(string libname, const ListResponse &resp)
166 { 198 {
167 cout << 199 cout <<
168 "\n#include \"PiperExport.h\"\n" 200 "\n#include \"PiperExport.h\"\n"
169 "\n" 201 "\n"
170 "// #include your own plugin headers here\n" 202 "// #include your own plugin headers here\n"
171 "\n" 203 "\n"
172 "using piper_vamp_js::PiperAdapter;\n" 204 "using piper_vamp_js::PiperAdapter;\n"
173 "using piper_vamp_js::PiperPluginLibrary;\n" 205 "using piper_vamp_js::PiperPluginLibrary;\n"
174 "\n" 206 "\n"
175 "static std::string soname(\"" << soname << "\");\n" 207 "static std::string libname(\"" << libname << "\");\n"
176 "\n"; 208 "\n";
177 209
178 // The same plugin key may appear more than once in the available 210 // The same plugin key may appear more than once in the available
179 // list, if the same plugin is installed in more than one location 211 // list, if the same plugin is installed in more than one location
180 // on the Vamp path. To avoid emitting its wrapper definition 212 // on the Vamp path. To avoid emitting its wrapper definition
192 PlausibleMetadata pm = inventPlausibleMetadata(key); 224 PlausibleMetadata pm = inventPlausibleMetadata(key);
193 cout << "static PiperAdapter<" 225 cout << "static PiperAdapter<"
194 << pm.className 226 << pm.className
195 << "> // replace with the actual Vamp plugin class name for \"" 227 << "> // replace with the actual Vamp plugin class name for \""
196 << plugin.basic.identifier << "\" plugin\n" << pm.adapterName 228 << plugin.basic.identifier << "\" plugin\n" << pm.adapterName
197 << "(\n soname,\n "; 229 << "(\n libname,\n ";
198 230
199 string catString = "{ "; 231 string catString = "{ ";
200 bool first = true; 232 bool first = true;
201 for (auto c: plugin.category) { 233 for (auto c: plugin.category) {
202 if (!first) catString += ", "; 234 if (!first) catString += ", ";
206 catString += " }"; 238 catString += " }";
207 cout << catString << ",\n "; 239 cout << catString << ",\n ";
208 240
209 cout << "{\n "; 241 cout << "{\n ";
210 first = true; 242 first = true;
211 for (auto o: plugin.staticOutputInfo) { 243 for (auto o: plugin.basicOutputInfo) {
212 if (!first) { 244 if (!first) {
213 cout << ",\n "; 245 cout << ",\n ";
214 } 246 }
215 cout << " "; 247 cout << " ";
216 string outputId = o.first; 248 string outputId = o.identifier;
217 const StaticOutputDescriptor &desc = o.second; 249 cout << "{ \"" << outputId << "\",\n { \"";
218 cout << "{ \"" << outputId << "\",\n { \"" 250 if (plugin.staticOutputInfo.find(outputId) !=
219 << desc.typeURI << "\" }\n }"; 251 plugin.staticOutputInfo.end()) {
252 cout << plugin.staticOutputInfo.at(outputId).typeURI;
253 }
254 cout << "\" }\n }";
220 first = false; 255 first = false;
221 } 256 }
222 cout << "\n }\n"; 257 cout << "\n }\n";
223 cout << " );\n\n"; 258 cout << " );\n\n";
224 emitted.insert(key); 259 emitted.insert(key);
248 { 283 {
249 if (argc != 2 && argc != 3) { 284 if (argc != 2 && argc != 3) {
250 usage(); 285 usage();
251 } 286 }
252 287
253 bool debug = false;
254
255 string arg = argv[1]; 288 string arg = argv[1];
256 if (arg == "-h") { 289 if (arg == "-h") {
257 if (argc == 2) { 290 if (argc == 2) {
258 usage(true); 291 usage(true);
259 } else { 292 } else {
263 if (argc == 2) { 296 if (argc == 2) {
264 version(); 297 version();
265 } else { 298 } else {
266 usage(); 299 usage();
267 } 300 }
268 } else if (arg == "-d") { 301 }
269 if (argc == 2) { 302
270 usage(); 303 string libname = arg;
271 } else {
272 debug = true;
273 arg = argv[2];
274 }
275 }
276
277 string soname = arg;
278 304
279 try { 305 try {
280 initFds(false); 306 initFds(false);
281 } catch (exception &e) { 307 } catch (exception &e) {
282 cerr << "ERROR: " << e.what() << endl; 308 cerr << "ERROR: " << e.what() << endl;
283 exit(1); 309 exit(1);
284 } 310 }
285 311
286 suspendOutput(); 312 suspendOutput();
287 313
288 ListResponse resp = makeRequest(soname, debug); 314 ListResponse resp = makeRequest(libname);
289 315
290 resumeOutput(); 316 resumeOutput();
291 317
292 emitFor(soname, resp, debug); 318 emitFor(libname, resp);
293 319
294 exit(0); 320 exit(0);
295 } 321 }