signal-wrapper.hh 1.54 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

Joseph Mirabel's avatar
Joseph Mirabel committed
7
#include <boost/python.hpp>
Guilhem Saurel's avatar
Guilhem Saurel committed
8
#include <boost/bind.hpp>
Joseph Mirabel's avatar
Joseph Mirabel committed
9

10
11
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal.h>
12
#include <dynamic-graph/entity.h>
13
#include "dynamic-graph/python/python-compat.hh"
14
15

namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
16
namespace python {
17

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
18
19
class PythonSignalContainer : public Entity {
  DYNAMIC_GRAPH_ENTITY_DECL();
20

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
21
 public:
Joseph Mirabel's avatar
Joseph Mirabel committed
22
  using Entity::Entity;
23

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
26
27
  void rmSignal(const std::string& name);
};
28

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
29
30
31
32
template <class T, class Time>
class SignalWrapper : public Signal<T, Time> {
 public:
  typedef Signal<T, Time> parent_t;
Joseph Mirabel's avatar
Joseph Mirabel committed
33
  typedef boost::python::object pyobject;
34

Joseph Mirabel's avatar
Joseph Mirabel committed
35
  static bool checkCallable(pyobject c, std::string& error);
36

Joseph Mirabel's avatar
Joseph Mirabel committed
37
  SignalWrapper(std::string name, pyobject callable) : parent_t(name), callable(callable) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
38
39
40
41
    typedef boost::function2<T&, T&, Time> function_t;
    function_t f = boost::bind(&SignalWrapper::call, this, _1, _2);
    this->setFunction(f);
  }
42

Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
43
  virtual ~SignalWrapper(){};
44

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
45
46
47
48
49
50
51
 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;
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
52
53
    pyobject obj = callable(t);
    value = boost::python::extract<T>(obj);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
54
55
56
    PyGILState_Release(gstate);
    return value;
  }
Joseph Mirabel's avatar
Joseph Mirabel committed
57
  pyobject callable;
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
58
};
59

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
60
61
}  // namespace python
}  // namespace dynamicgraph
Guilhem Saurel's avatar
Guilhem Saurel committed
62
#endif