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