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 |
|
|
|
14 |
|
|
#include "dynamic-graph/python/python-compat.hh" |
15 |
|
|
|
16 |
|
|
namespace dynamicgraph { |
17 |
|
|
namespace python { |
18 |
|
|
|
19 |
|
|
class 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 |
|
|
|
30 |
|
|
template <class T, class Time> |
31 |
|
|
class 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 |