Skip to content
Snippets Groups Projects
convert-dg-to-py.cc 2.91 KiB
Newer Older
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.

#include <iostream>
#include <sstream>

Joseph Mirabel's avatar
Joseph Mirabel committed
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>

#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"
Guilhem Saurel's avatar
Guilhem Saurel committed
using ::dynamicgraph::SignalBase;

namespace python {
namespace convert {
Joseph Mirabel's avatar
Joseph Mirabel committed
namespace bp = boost::python;
Joseph Mirabel's avatar
Joseph Mirabel committed
command::Value toValue(bp::object o, const command::Value::Type& valueType) {
Guilhem Saurel's avatar
Guilhem Saurel committed
  using command::Value;
  switch (valueType) {
    case (Value::BOOL):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<bool>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::UNSIGNED):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<unsigned>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::INT):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<int>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::FLOAT):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<float>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::DOUBLE):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<double>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::STRING):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<std::string>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::VECTOR):
Joseph Mirabel's avatar
Joseph Mirabel committed
      // TODO for backward compatibility, support tuple or list ?
      // I don't think so
      return Value(bp::extract<Vector>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::MATRIX):
Joseph Mirabel's avatar
Joseph Mirabel committed
      // TODO for backward compatibility, support tuple or list ?
      // I don't think so
      return Value(bp::extract<Matrix>(o));
Guilhem Saurel's avatar
Guilhem Saurel committed
    case (Value::MATRIX4D):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return Value(bp::extract<Eigen::Matrix4d>(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.
Joseph Mirabel's avatar
Joseph Mirabel committed
      throw std::invalid_argument("not implemented: cannot create a vector of values");
Guilhem Saurel's avatar
Guilhem Saurel committed
    default:
      std::cerr << "Only int, double and string are supported." << std::endl;
  }
  return Value();
}
Joseph Mirabel's avatar
Joseph Mirabel committed
bp::object fromValue(const command::Value& value) {
Guilhem Saurel's avatar
Guilhem Saurel committed
  using command::Value;
  switch (value.type()) {
    case (Value::BOOL):
Joseph Mirabel's avatar
Joseph Mirabel committed
      return bp::object(value.boolValue());
Joseph Mirabel's avatar
Joseph Mirabel committed
    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();
Guilhem Saurel's avatar
Guilhem Saurel committed
  }
}
Guilhem Saurel's avatar
Guilhem Saurel committed
}  // namespace convert
}  // namespace python
}  // namespace dynamicgraph