GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/dynamic_graph/entity-py.cc Lines: 24 32 75.0 %
Date: 2022-09-30 08:22:45 Branches: 39 108 36.1 %

Line Branch Exec Source
1
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
2
3
#include <dynamic-graph/command.h>
4
#include <dynamic-graph/entity.h>
5
#include <dynamic-graph/factory.h>
6
#include <dynamic-graph/linear-algebra.h>
7
#include <dynamic-graph/pool.h>
8
#include <dynamic-graph/value.h>
9
10
#include <iostream>
11
12
#include "dynamic-graph/python/convert-dg-to-py.hh"
13
#include "dynamic-graph/python/dynamic-graph-py.hh"
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
23
using dynamicgraph::Entity;
24
using dynamicgraph::Matrix;
25
using dynamicgraph::SignalBase;
26
using dynamicgraph::Vector;
27
using dynamicgraph::command::Command;
28
using dynamicgraph::command::Value;
29
30
namespace dynamicgraph {
31
namespace python {
32
33
using namespace convert;
34
35
namespace entity {
36
37
/// \param obj an Entity object
38
5
void addCommands(bp::object obj) {
39
5
  Entity& entity = bp::extract<Entity&>(obj);
40

10
  for (const auto& el : entity.getNewStyleCommandMap())
41


5
    obj.attr(el.first.c_str()) = bp::object(bp::ptr(el.second));
42
5
}
43
44
/// \param obj an Entity object
45
5
void addSignals(bp::object obj) {
46
5
  Entity& entity = bp::extract<Entity&>(obj);
47

15
  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


10
    obj.attr(el.first.c_str()) = bp::object(bp::ptr(el.second));
52
5
}
53
54
/**
55
   \brief Create an instance of Entity
56
*/
57
5
Entity* create(const char* className, const char* instanceName) {
58
5
  Entity* obj = NULL;
59
  /* Try to find if the corresponding object already exists. */
60


5
  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
  {
72

10
    obj = dynamicgraph::FactoryStorage::getInstance()->newEntity(
73

10
        std::string(className), std::string(instanceName));
74
  }
75
76
5
  return obj;
77
}
78
79
2
bp::object executeCmd(bp::tuple args, bp::dict) {
80


2
  Command& command = bp::template extract<Command&>(args[0]);
81

2
  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
3
  std::vector<Value> values;
85

2
  values.reserve(command.valueTypes().size());
86

2
  for (int i = 1; i < bp::len(args); ++i)
87
    values.push_back(convert::toValue(args[i], command.valueTypes()[i - 1]));
88
2
  command.setParameterValues(values);
89

4
  return convert::fromValue(command.execute());
90
}
91
92
}  // namespace entity
93
}  // namespace python
94
}  // namespace dynamicgraph