convert-dg-to-py.cc
Go to the documentation of this file.
1 // Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
2 
4 
7 #include <dynamic-graph/signal.h>
8 
9 #include <boost/python.hpp>
10 #include <boost/python/stl_iterator.hpp>
11 #include <cstdint>
12 #include <iostream>
13 #include <sstream>
14 
16 
17 namespace dynamicgraph {
18 
19 using ::dynamicgraph::SignalBase;
20 
21 namespace python {
22 namespace convert {
23 
24 namespace bp = boost::python;
25 
26 command::Value toValue(bp::object o, const command::Value::Type& valueType) {
27  using command::Value;
28  switch (valueType) {
29  case (Value::BOOL):
30  return Value(bp::extract<bool>(o));
31  case (Value::UNSIGNED):
32  return Value(bp::extract<unsigned>(o));
33  case (Value::UNSIGNEDLONGINT):
34  return Value(bp::extract<std::uint64_t>(o));
35  case (Value::INT):
36  return Value(bp::extract<int>(o));
37  case (Value::LONGINT):
38  return Value(bp::extract<std::int64_t>(o));
39  case (Value::FLOAT):
40  return Value(bp::extract<float>(o));
41  case (Value::DOUBLE):
42  return Value(bp::extract<double>(o));
43  case (Value::STRING):
44  return Value(bp::extract<std::string>(o));
45  case (Value::VECTOR):
46  // TODO for backward compatibility, support tuple or list ?
47  // I don't think so
48  return Value(bp::extract<Vector>(o));
49  case (Value::MATRIX):
50  // TODO for backward compatibility, support tuple or list ?
51  // I don't think so
52  return Value(bp::extract<Matrix>(o));
53  case (Value::MATRIX4D):
54  return Value(bp::extract<Eigen::Matrix4d>(o));
55  case (Value::VALUES):
56  // TODO the vector of values cannot be built since
57  // - the value type inside the vector are not know
58  // - inferring the value type from the Python type is not implemented.
59  throw std::invalid_argument(
60  "not implemented: cannot create a vector of values");
61  break;
62  default:
63  std::cerr << "Only int, double and string are supported." << std::endl;
64  }
65  return Value();
66 }
67 
68 bp::object fromValue(const command::Value& value) {
69  using command::Value;
70  switch (value.type()) {
71  case (Value::BOOL):
72  return bp::object(value.boolValue());
73  case (Value::UNSIGNED):
74  return bp::object(value.unsignedValue());
75  case (Value::UNSIGNEDLONGINT):
76  return bp::object(value.unsignedlongintValue());
77  case (Value::INT):
78  return bp::object(value.intValue());
79  case (Value::LONGINT):
80  return bp::object(value.longintValue());
81  case (Value::FLOAT):
82  return bp::object(value.floatValue());
83  case (Value::DOUBLE):
84  return bp::object(value.doubleValue());
85  case (Value::STRING):
86  return bp::object(value.stringValue());
87  case (Value::VECTOR):
88  return bp::object(value.vectorValue());
89  case (Value::MATRIX):
90  return bp::object(value.matrixXdValue());
91  case (Value::MATRIX4D):
92  return bp::object(value.matrix4dValue());
93  case (Value::VALUES): {
94  bp::list list;
95  for (const Value& v : value.constValuesValue()) list.append(fromValue(v));
96  return list;
97  }
98  case (Value::NONE):
99  default:
100  return bp::object();
101  }
102 }
103 
104 } // namespace convert
105 } // namespace python
106 } // namespace dynamicgraph
boost::python
signal-base.h
dynamicgraph
dynamicgraph::python::convert::fromValue
boost::python::object fromValue(const command::Value &value)
Definition: convert-dg-to-py.cc:68
python
python-compat.hh
value
float value
signal-caster.h
convert-dg-to-py.hh
dynamicgraph::python::convert::toValue
command::Value toValue(boost::python::object o, const command::Value::Type &type)
v
v
dynamicgraph::command::Value::Type
Type
dynamicgraph::command::Value


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Oct 27 2023 02:16:36