signal-wrapper.cc 1.96 KB
Newer Older
1
2
3
// Copyright (c) 2018, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)

4
5
#include <Python.h>
#include <dynamic-graph/factory.h>
6
#include <dynamic-graph/command-bind.h>
7

8
9
#include "dynamic-graph/python/signal-wrapper.hh"

10
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
11
12
13
namespace python {
namespace signalWrapper {
void convert(PyObject* o, bool& v) { v = (o == Py_True); }
14
void convert(PyObject* o, int& v) { v = (int)PyLong_AsLong(o); }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
15
16
17
18
19
20
21
void convert(PyObject* o, float& v) { v = (float)PyFloat_AS_DOUBLE(o); }
void convert(PyObject* o, double& v) { v = PyFloat_AS_DOUBLE(o); }
void convert(PyObject* o, Vector& v) {
  v.resize(PyTuple_Size(o));
  for (int i = 0; i < v.size(); ++i) convert(PyTuple_GetItem(o, i), v[i]);
}
}  // namespace signalWrapper
22

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
23
24
PythonSignalContainer::PythonSignalContainer(const std::string& name) : Entity(name) {
  std::string docstring;
25

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
26
27
28
29
30
31
32
33
34
  docstring =
      "    \n"
      "    Remove a signal\n"
      "    \n"
      "      Input:\n"
      "        - name of the signal\n"
      "    \n";
  addCommand("rmSignal", command::makeCommandVoid1(*this, &PythonSignalContainer::rmSignal, docstring));
}
35

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
36
37
38
void PythonSignalContainer::signalRegistration(const SignalArray<int>& signals) {
  Entity::signalRegistration(signals);
}
39

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
40
void PythonSignalContainer::rmSignal(const std::string& name) { Entity::signalDeregistration(name); }
41

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
42
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(PythonSignalContainer, "PythonSignalContainer");
43

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
44
45
46
47
template <class T, class Time>
bool SignalWrapper<T, Time>::checkCallable(PyObject* c, std::string& error) {
  if (PyCallable_Check(c) == 0) {
    PyObject* str = PyObject_Str(c);
48
    error = PyUnicode_AS_DATA(str);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
49
50
51
52
53
54
    error += " is not callable";
    Py_DECREF(str);
    return false;
  }
  return true;
}
55

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
56
57
58
59
60
61
62
template class SignalWrapper<bool, int>;
template class SignalWrapper<int, int>;
template class SignalWrapper<float, int>;
template class SignalWrapper<double, int>;
template class SignalWrapper<Vector, int>;
}  // namespace python
}  // namespace dynamicgraph