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

Guilhem Saurel's avatar
Guilhem Saurel committed
4
5
6
#ifndef DGPY_SIGNAL_WRAPPER
#define DGPY_SIGNAL_WRAPPER

7
8
9
10
#include <Python.h>

#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal.h>
11
#include <dynamic-graph/entity.h>
12
13

namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
14
15
16
17
18
19
20
21
22
23
24
namespace python {
namespace signalWrapper {
void convert(PyObject* o, int& v);
void convert(PyObject* o, bool& v);
void convert(PyObject* o, float& v);
void convert(PyObject* o, double& v);
// void convert (PyObject* o, std::string& v);
void convert(PyObject* o, Vector& v);
// void convert (PyObject* o, Eigen::MatrixXd& v);
// void convert (PyObject* o, Eigen::Matrix4d& v);
}  // namespace signalWrapper
25

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
26
27
class PythonSignalContainer : public Entity {
  DYNAMIC_GRAPH_ENTITY_DECL();
28

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
29
30
 public:
  PythonSignalContainer(const std::string& name);
31

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
32
  void signalRegistration(const SignalArray<int>& signals);
33

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
34
35
  void rmSignal(const std::string& name);
};
36

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
37
38
39
40
template <class T, class Time>
class SignalWrapper : public Signal<T, Time> {
 public:
  typedef Signal<T, Time> parent_t;
41

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
42
  static bool checkCallable(PyObject* c, std::string& error);
43

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
44
45
46
47
48
49
  SignalWrapper(std::string name, PyObject* _callable) : parent_t(name), callable(_callable) {
    typedef boost::function2<T&, T&, Time> function_t;
    Py_INCREF(callable);
    function_t f = boost::bind(&SignalWrapper::call, this, _1, _2);
    this->setFunction(f);
  }
50

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
51
  virtual ~SignalWrapper() { Py_DECREF(callable); };
52

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 private:
  T& call(T& value, Time t) {
    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();
    if (PyGILState_GetThisThreadState() == NULL) {
      dgDEBUG(10) << "python thread not initialized" << std::endl;
    }
    char format[] = "i";
    PyObject* obj = PyObject_CallFunction(callable, format, t);
    if (obj == NULL) {
      dgERROR << "Could not call callable" << std::endl;
    } else {
      signalWrapper::convert(obj, value);
      Py_DECREF(obj);
    }
    PyGILState_Release(gstate);
    return value;
  }
  PyObject* callable;
};
73

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
74
75
}  // namespace python
}  // namespace dynamicgraph
Guilhem Saurel's avatar
Guilhem Saurel committed
76
#endif