9da6a002660ab1c72eeb356d9e02ce59ed7f5147
[vuplus_dvbapp] / lib / python / connections.h
1 #ifndef __lib_python_connections_h
2 #define __lib_python_connections_h
3
4 #include <libsig_comp.h>
5
6                 /* avoid warnigs :) */
7 #include <features.h>
8 #undef _POSIX_C_SOURCE
9 #define _POSIX_C_SOURCE 200112L
10 #include <lib/python/python.h>
11
12 class PSignal
13 {
14 public:
15         PyObject *m_list;
16 public:
17         PSignal()
18         {
19                 m_list = PyList_New(0);
20                 Py_INCREF(m_list);
21         }
22         ~PSignal()
23         {
24                 Py_DECREF(m_list);
25         }
26         
27         void callPython(PyObject *tuple)
28         {
29                 int size = PyList_Size(m_list);
30                 int i;
31                 for (i=0; i<size; ++i)
32                 {
33                         PyObject *b = PyList_GET_ITEM(m_list, i);
34                         ePython::call(b, tuple);
35                 }
36         }
37         
38         
39         PyObject *get() { Py_INCREF(m_list); return m_list; }
40 };
41
42 inline PyObject *PyFrom(int v)
43 {
44         return PyInt_FromLong(v);
45 }
46
47 inline PyObject *PyFrom(const char *c)
48 {
49         return PyString_FromString(c);
50 }
51
52 template <class R>
53 class PSignal0: public PSignal, public Signal0<R>
54 {
55 public:
56         R operator()()
57         {
58                 PyObject *pArgs = PyTuple_New(0);
59                 callPython(pArgs);
60                 Py_DECREF(pArgs);
61                 return Signal0<R>::operator()();
62         }
63 };
64
65 template <class R, class V0>
66 class PSignal1: public PSignal, public Signal1<R,V0>
67 {
68 public:
69         R operator()(V0 a0)
70         {
71                 PyObject *pArgs = PyTuple_New(1);
72                 PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
73                 callPython(pArgs);
74                 Py_DECREF(pArgs);
75                 return Signal1<R,V0>::operator()(a0);
76         }
77 };
78
79 template <class R, class V0, class V1>
80 class PSignal2: public PSignal, public Signal2<R,V0,V1>
81 {
82 public:
83         R operator()(V0 a0, V1 a1)
84         {
85                 PyObject *pArgs = PyTuple_New(2);
86                 PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
87                 PyTuple_SET_ITEM(pArgs, 1, PyFrom(a1));
88                 callPython(pArgs);
89                 Py_DECREF(pArgs);
90                 return Signal2<R,V0,V1>::operator()(a0, a1);
91         }
92 };
93
94 #endif