entity-py.cc
Go to the documentation of this file.
1 // Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
2 
4 #include <dynamic-graph/entity.h>
5 #include <dynamic-graph/factory.h>
7 #include <dynamic-graph/pool.h>
8 #include <dynamic-graph/value.h>
9 
10 #include <iostream>
11 
14 
15 // Ignore "dereferencing type-punned pointer will break strict-aliasing rules"
16 // warnings on gcc caused by Py_RETURN_TRUE and Py_RETURN_FALSE.
17 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
18 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
19 #endif
20 
21 namespace bp = boost::python;
22 
29 
30 namespace dynamicgraph {
31 namespace python {
32 
33 using namespace convert;
34 
35 namespace entity {
36 
38 void addCommands(bp::object obj) {
39  Entity& entity = bp::extract<Entity&>(obj);
40  for (const auto& el : entity.getNewStyleCommandMap())
41  obj.attr(el.first.c_str()) = bp::object(bp::ptr(el.second));
42 }
43 
45 void addSignals(bp::object obj) {
46  Entity& entity = bp::extract<Entity&>(obj);
47  for (const auto& el : entity.getSignalMap())
48  // obj.attr(el.first.c_str()) = bp::make_function(
49  //+[&entity,el]() { return &entity.getSignal(el.first); },
50  // bp::return_value_policy<bp::reference_existing_object>());
51  obj.attr(el.first.c_str()) = bp::object(bp::ptr(el.second));
52 }
53 
57 Entity* create(const char* className, const char* instanceName) {
58  Entity* obj = NULL;
59  /* Try to find if the corresponding object already exists. */
60  if (dynamicgraph::PoolStorage::getInstance()->existEntity(instanceName,
61  obj)) {
62  if (obj->getClassName() != className) {
63  throw std::invalid_argument("Found an object named " +
64  std::string(instanceName) +
65  ",\n"
66  "but this object is of type " +
67  std::string(obj->getClassName()) +
68  " and not " + std::string(className));
69  }
70  } else /* If not, create a new object. */
71  {
73  std::string(className), std::string(instanceName));
74  }
75 
76  return obj;
77 }
78 
79 bp::object executeCmd(bp::tuple args, bp::dict) {
80  Command& command = bp::template extract<Command&>(args[0]);
81  if (bp::len(args) != int(command.valueTypes().size() + 1))
82  // TODO Put expected and given number of args
83  throw std::out_of_range("Wrong number of arguments");
84  std::vector<Value> values;
85  values.reserve(command.valueTypes().size());
86  for (int i = 1; i < bp::len(args); ++i)
87  values.push_back(convert::toValue(args[i], command.valueTypes()[i - 1]));
88  command.setParameterValues(values);
89  return convert::fromValue(command.execute());
90 }
91 
92 } // namespace entity
93 } // namespace python
94 } // namespace dynamicgraph
boost::python::object fromValue(const command::Value &value)
Eigen::VectorXd Vector
Eigen::Matrix< CustomType< Scalar >, Eigen::Dynamic, Eigen::Dynamic > create(int rows, int cols)
SignalMap getSignalMap() const
void addSignals(bp::object obj)
Definition: entity-py.cc:45
command::Value toValue(boost::python::object o, const command::Value::Type &type)
virtual const std::string & getClassName() const
bp::object executeCmd(bp::tuple args, bp::dict)
Definition: entity-py.cc:79
void setParameterValues(const std::vector< Value > &values)
static PoolStorage * getInstance()
CommandMap_t getNewStyleCommandMap()
Eigen::MatrixXd Matrix
Entity * newEntity(const std::string &classname, const std::string &objname) const
static FactoryStorage * getInstance()
void addCommands(bp::object obj)
Definition: entity-py.cc:38
const std::vector< Value::Type > & valueTypes() const


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:55:50