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