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 (bp::ssize_t i = 1; i < bp::len(args); ++i)
87  values.push_back(convert::toValue(
88  args[i],
89  command
90  .valueTypes()[static_cast<std::vector<Value>::size_type>(i - 1)]));
91  command.setParameterValues(values);
92  return convert::fromValue(command.execute());
93 }
94 
95 } // namespace entity
96 } // namespace python
97 } // namespace dynamicgraph
boost::python
dynamicgraph
dynamicgraph::python::convert::fromValue
boost::python::object fromValue(const command::Value &value)
Definition: convert-dg-to-py.cc:68
dynamicgraph::Entity
dynamicgraph::Matrix
Eigen::MatrixXd Matrix
dynamicgraph::Entity::getSignalMap
SignalMap getSignalMap() const
dynamicgraph::Entity::getNewStyleCommandMap
CommandMap_t getNewStyleCommandMap()
dynamicgraph::python::entity::executeCmd
bp::object executeCmd(bp::tuple args, bp::dict)
Definition: entity-py.cc:79
dynamicgraph::command::Command::setParameterValues
void setParameterValues(const std::vector< Value > &values)
dynamic-graph-py.hh
dynamicgraph::python::entity::addSignals
void addSignals(bp::object obj)
Definition: entity-py.cc:45
python
dynamicgraph::command::Command::valueTypes
const std::vector< Value::Type > & valueTypes() const
dynamicgraph::command::Command
dynamicgraph::Entity::getClassName
virtual const std::string & getClassName() const
value.h
dynamicgraph::python::entity::addCommands
void addCommands(bp::object obj)
Definition: entity-py.cc:38
dynamicgraph::Vector
Eigen::VectorXd Vector
convert-dg-to-py.hh
dynamicgraph::python::convert::toValue
command::Value toValue(boost::python::object o, const command::Value::Type &type)
linear-algebra.h
create
Eigen::Matrix< CustomType< Scalar >, Eigen::Dynamic, Eigen::Dynamic > create(int rows, int cols)
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
dynamicgraph::command::Command::execute
Value execute()
dynamicgraph::SignalBase
command.h
dynamicgraph::command::Value
dynamicgraph::FactoryStorage::newEntity
Entity * newEntity(const std::string &classname, const std::string &objname) const
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()


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