signal-wrapper.hh 3.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Copyright (c) 2018, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
//
// This file is part of dynamic-graph-python.
// dynamic-graph-python is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// dynamic-graph-python is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Lesser Public License for more details.  You should have
// received a copy of the GNU Lesser General Public License along with
// dynamic-graph-python. If not, see <http://www.gnu.org/licenses/>.

#include <Python.h>

#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal.h>
21
#include <dynamic-graph/entity.h>
22
23
24
25
26
27
28
29
30
31
32
33
34
35

namespace dynamicgraph {
  namespace python {
    namespace signalWrapper {
      void convert (PyObject* o, int   & v);
      void convert (PyObject* o, bool  & v);
      void convert (PyObject* o, float & v);
      void convert (PyObject* o, double& v);
      // void convert (PyObject* o, std::string& v);
      void convert (PyObject* o, Vector     & v);
      // void convert (PyObject* o, Eigen::MatrixXd& v);
      // void convert (PyObject* o, Eigen::Matrix4d& v);
    }

36
37
38
39
40
41
42
43
44
45
46
47
48
    class PythonSignalContainer : public Entity
    {
      DYNAMIC_GRAPH_ENTITY_DECL();

      public:
        PythonSignalContainer (const std::string& name) : Entity (name) {};

        void signalRegistration (const SignalArray<int>& signals)
        {
          Entity::signalRegistration (signals);
        }
    };

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    template <class T, class Time>
    class SignalWrapper : public Signal<T, Time>
    {
      public:
        typedef Signal<T,Time> parent_t;

        static bool checkCallable (PyObject* c, std::string& error);

        SignalWrapper (std::string name, PyObject* _callable) : 
          parent_t (name)
          , callable (_callable)
          // , argsTuple (NULL)
          // , argTime (NULL)
          // , argValue (NULL)
        {
          typedef boost::function2<T&,T&,Time> function_t;
          Py_INCREF(callable);
          function_t f = boost::bind (&SignalWrapper::call, this, _1, _2);
          this->setFunction (f);

          // argsTuple = PyTuple_New(1);
          // argTime = Py
        }

        virtual ~SignalWrapper ()
        {
          Py_DECREF(callable);
          // Py_DECREF(args);
        };

      private:
        T& call (T& value, Time t)
        {
          char format[] = "i";
          PyObject* obj = PyObject_CallFunction(callable, format, t);
          if (obj == NULL)
            std::cerr << "Could not call callable" << std::endl;
          else {
            signalWrapper::convert (obj, value);
            Py_DECREF(obj);
          }
          return value;
        }
        PyObject* callable;
        // PyObject* argsTuple;
        // PyObject* argTime;
        // PyObject* argValue;
    };

  } // namespace dynamicgraph
} // namespace python