comparison PyPluginObject.cpp @ 49:d4a3cd9dcf2c

Check that parameter id exists
author Chris Cannam
date Tue, 13 Jan 2015 12:10:18 +0000
parents 0e0e18629917
children
comparison
equal deleted inserted replaced
48:0e0e18629917 49:d4a3cd9dcf2c
50 #include "PyRealTime.h" 50 #include "PyRealTime.h"
51 51
52 #include <string> 52 #include <string>
53 #include <vector> 53 #include <vector>
54 #include <cstddef> 54 #include <cstddef>
55 #include <set>
55 56
56 using namespace std; 57 using namespace std;
57 using namespace Vamp; 58 using namespace Vamp;
58 59
59 static 60 static
270 271
271 pd->plugin->reset(); 272 pd->plugin->reset();
272 return Py_True; 273 return Py_True;
273 } 274 }
274 275
276 static bool
277 hasParameter(PyPluginObject *pd, string id)
278 {
279 PluginBase::ParameterList pl = pd->plugin->getParameterDescriptors();
280 for (int i = 0; i < (int)pl.size(); ++i) {
281 if (pl[i].identifier == id) {
282 return true;
283 }
284 }
285 return false;
286 }
287
275 static PyObject * 288 static PyObject *
276 getParameterValue(PyObject *self, PyObject *args) 289 getParameterValue(PyObject *self, PyObject *args)
277 { 290 {
278 PyObject *pyParam; 291 PyObject *pyParam;
279 292
283 return 0; } 296 return 0; }
284 297
285 PyPluginObject *pd = getPluginObject(self); 298 PyPluginObject *pd = getPluginObject(self);
286 if (!pd) return 0; 299 if (!pd) return 0;
287 300
288 float value = pd->plugin->getParameter(PyString_AS_STRING(pyParam)); 301 string param = PyString_AS_STRING(pyParam);
302
303 if (!hasParameter(pd, param)) {
304 PyErr_SetString(PyExc_StandardError,
305 (string("Unknown parameter id \"") + param + "\"").c_str());
306 return 0;
307 }
308
309 float value = pd->plugin->getParameter(param);
289 return PyFloat_FromDouble(double(value)); 310 return PyFloat_FromDouble(double(value));
290 } 311 }
291 312
292 static PyObject * 313 static PyObject *
293 setParameterValue(PyObject *self, PyObject *args) 314 setParameterValue(PyObject *self, PyObject *args)
301 return 0; } 322 return 0; }
302 323
303 PyPluginObject *pd = getPluginObject(self); 324 PyPluginObject *pd = getPluginObject(self);
304 if (!pd) return 0; 325 if (!pd) return 0;
305 326
306 pd->plugin->setParameter(PyString_AS_STRING(pyParam), value); 327 string param = PyString_AS_STRING(pyParam);
328
329 if (!hasParameter(pd, param)) {
330 PyErr_SetString(PyExc_StandardError,
331 (string("Unknown parameter id \"") + param + "\"").c_str());
332 return 0;
333 }
334
335 pd->plugin->setParameter(param, value);
307 return Py_True; 336 return Py_True;
308 } 337 }
309 338
310 static PyObject * 339 static PyObject *
311 setParameterValues(PyObject *self, PyObject *args) 340 setParameterValues(PyObject *self, PyObject *args)
322 "setParameterValues() takes dict argument"); 351 "setParameterValues() takes dict argument");
323 return 0; } 352 return 0; }
324 353
325 PyPluginObject *pd = getPluginObject(self); 354 PyPluginObject *pd = getPluginObject(self);
326 if (!pd) return 0; 355 if (!pd) return 0;
356
357 PluginBase::ParameterList pl = pd->plugin->getParameterDescriptors();
358 set<string> paramIds;
359 for (int i = 0; i < (int)pl.size(); ++i) {
360 paramIds.insert(pl[i].identifier);
361 }
327 362
328 Py_ssize_t pos = 0; 363 Py_ssize_t pos = 0;
329 PyObject *key, *value; 364 PyObject *key, *value;
330 while (PyDict_Next(dict, &pos, &key, &value)) { 365 while (PyDict_Next(dict, &pos, &key, &value)) {
331 if (!key || !PyString_CheckExact(key)) { 366 if (!key || !PyString_CheckExact(key)) {
336 if (!value || !FloatConversion::check(value)) { 371 if (!value || !FloatConversion::check(value)) {
337 PyErr_SetString(PyExc_TypeError, 372 PyErr_SetString(PyExc_TypeError,
338 "Parameter dict values must be convertible to float"); 373 "Parameter dict values must be convertible to float");
339 return 0; 374 return 0;
340 } 375 }
341 pd->plugin->setParameter(PyString_AS_STRING(key), 376 string param = PyString_AS_STRING(key);
342 FloatConversion::convert(value)); 377 if (paramIds.find(param) == paramIds.end()) {
378 PyErr_SetString(PyExc_StandardError,
379 (string("Unknown parameter id \"") + param + "\"").c_str());
380 return 0;
381 }
382 pd->plugin->setParameter(param, FloatConversion::convert(value));
343 } 383 }
344 384
345 return Py_True; 385 return Py_True;
346 } 386 }
347 387