Skip to content
Snippets Groups Projects
Commit 62084b29 authored by florent's avatar florent
Browse files

Support more types in command parameters

	* src/entity-py.cc.
parent b38f00b1
No related branches found
No related tags found
No related merge requests found
...@@ -145,32 +145,62 @@ namespace dynamicgraph { ...@@ -145,32 +145,62 @@ namespace dynamicgraph {
Value pythonToValue(PyObject* pyObject, Value pythonToValue(PyObject* pyObject,
const Value::Type& valueType) const Value::Type& valueType)
{ {
bool bvalue;
unsigned uvalue;
int ivalue;
float fvalue;
double dvalue; double dvalue;
std::string svalue; std::string svalue;
int ivalue;
switch (valueType) { switch (valueType) {
case (Value::BOOL) :
if (!PyBool_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"bool");
}
bvalue = (pyObject == Py_True);
return Value(bvalue);
break;
case (Value::UNSIGNED) :
if (!PyLong_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"int");
}
// Check that value is not negative
ivalue = (int)PyLong_AsLong(pyObject);
if (ivalue < 0) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"unsigned int");
}
uvalue = (unsigned) ivalue;
return Value(uvalue);
break;
case (Value::INT) : case (Value::INT) :
if (!PyLong_Check(pyObject)) { if (!PyLong_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC, throw ExceptionFactory(ExceptionFactory::GENERIC,
"float"); "int");
} }
ivalue = (int)PyLong_AsLong(pyObject); ivalue = (int)PyLong_AsLong(pyObject);
std::cout << "int param = " << ivalue << std::endl;
return Value(ivalue); return Value(ivalue);
break; break;
case (Value::FLOAT) :
if (!PyFloat_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC,
"float");
}
fvalue = (float)PyFloat_AsDouble(pyObject);
return Value(fvalue);
break;
case (Value::DOUBLE) : case (Value::DOUBLE) :
if (!PyFloat_Check(pyObject)) { if (!PyFloat_Check(pyObject)) {
throw ExceptionFactory(ExceptionFactory::GENERIC, throw ExceptionFactory(ExceptionFactory::GENERIC,
"float"); "float");
} }
dvalue = PyFloat_AsDouble(pyObject); dvalue = PyFloat_AsDouble(pyObject);
std::cout << "double param = " << dvalue << std::endl;
return Value(dvalue); return Value(dvalue);
break; break;
case (Value::STRING) : case (Value::STRING) :
svalue = PyString_AsString(pyObject); svalue = PyString_AsString(pyObject);
std::cout << "string param = \"" << dvalue << "\"" << std::endl;
return Value(svalue); return Value(svalue);
break; break;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment