signal-wrapper.hh
Go to the documentation of this file.
1 // Copyright (c) 2018, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 
4 #ifndef DGPY_SIGNAL_WRAPPER
5 #define DGPY_SIGNAL_WRAPPER
6 
7 #include <boost/python.hpp>
8 #include <boost/bind.hpp>
9 
10 #include <dynamic-graph/linear-algebra.h>
11 #include <dynamic-graph/signal.h>
12 #include <dynamic-graph/entity.h>
14 
15 namespace dynamicgraph {
16 namespace python {
17 
18 class PythonSignalContainer : public Entity {
19  DYNAMIC_GRAPH_ENTITY_DECL();
20 
21  public:
22  using Entity::Entity;
23 
24  void signalRegistration(const SignalArray<int>& signals);
25 
26  void rmSignal(const std::string& name);
27 };
28 
29 template <class T, class Time>
30 class SignalWrapper : public Signal<T, Time> {
31  public:
32  typedef Signal<T, Time> parent_t;
33  typedef boost::python::object pyobject;
34 
35  static bool checkCallable(pyobject c, std::string& error);
36 
37  SignalWrapper(std::string name, pyobject callable) : parent_t(name), callable(callable) {
38  typedef boost::function2<T&, T&, Time> function_t;
39  function_t f = boost::bind(&SignalWrapper::call, this, _1, _2);
40  this->setFunction(f);
41  }
42 
43  virtual ~SignalWrapper(){};
44 
45  private:
46  T& call(T& value, Time t) {
47  PyGILState_STATE gstate;
48  gstate = PyGILState_Ensure();
49  if (PyGILState_GetThisThreadState() == NULL) {
50  dgDEBUG(10) << "python thread not initialized" << std::endl;
51  }
52  pyobject obj = callable(t);
53  value = boost::python::extract<T>(obj);
54  PyGILState_Release(gstate);
55  return value;
56  }
57  pyobject callable;
58 };
59 
60 } // namespace python
61 } // namespace dynamicgraph
62 #endif
Definition: signal-wrapper.hh:18
Definition: signal-wrapper.hh:30
SignalWrapper(std::string name, pyobject callable)
Definition: signal-wrapper.hh:37
Signal< T, Time > parent_t
Definition: signal-wrapper.hh:32
void rmSignal(const std::string &name)
Definition: signal-wrapper.cc:15
boost::python::object pyobject
Definition: signal-wrapper.hh:33
virtual ~SignalWrapper()
Definition: signal-wrapper.hh:43
void signalRegistration(const SignalArray< int > &signals)
Definition: signal-wrapper.cc:11
Definition: convert-dg-to-py.hh:8