Newer
Older

Nicolas Mansard
committed
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <iostream>
#include <sstream>
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>

Nicolas Mansard
committed
#include <dynamic-graph/signal-base.h>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-caster.h>
#include "dynamic-graph/python/convert-dg-to-py.hh"
#include "dynamic-graph/python/python-compat.hh"

Nicolas Mansard
committed
namespace dynamicgraph {
using ::dynamicgraph::SignalBase;
namespace python {
namespace convert {

Nicolas Mansard
committed

Nicolas Mansard
committed
command::Value toValue(bp::object o, const command::Value::Type& valueType) {
using command::Value;
switch (valueType) {
case (Value::BOOL):
// TODO for backward compatibility, support tuple or list ?
// I don't think so
return Value(bp::extract<Vector>(o));
// TODO for backward compatibility, support tuple or list ?
// I don't think so
return Value(bp::extract<Matrix>(o));
case (Value::VALUES):
// TODO the vector of values cannot be built since
// - the value type inside the vector are not know
// - inferring the value type from the Python type is not implemented.
throw std::invalid_argument("not implemented: cannot create a vector of values");
default:
std::cerr << "Only int, double and string are supported." << std::endl;
}
return Value();
}

Nicolas Mansard
committed
bp::object fromValue(const command::Value& value) {
using command::Value;
switch (value.type()) {
case (Value::BOOL):
case (Value::UNSIGNED):
return bp::object(value.unsignedValue());
case (Value::INT):
return bp::object(value.intValue());
case (Value::FLOAT):
return bp::object(value.floatValue());
case (Value::DOUBLE):
return bp::object(value.doubleValue());
case (Value::STRING):
return bp::object(value.stringValue());
case (Value::VECTOR):
return bp::object(value.vectorValue());
case (Value::MATRIX):
return bp::object(value.matrixXdValue());
case (Value::MATRIX4D):
return bp::object(value.matrix4dValue());
case (Value::VALUES):
{
bp::list list;
for(const Value& v : value.constValuesValue())
list.append(fromValue(v));
return list;
}
case (Value::NONE):
default:
return bp::object();

Nicolas Mansard
committed
} // namespace convert
} // namespace python
} // namespace dynamicgraph