correctly parse output of new epopen command
[enigma2.git] / 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
11 #include <lib/python/python.h>
12
13 class PSignal
14 {
15 protected:
16         ePyObject m_list;
17         bool *m_destroyed;
18 public:
19         PSignal();
20         ~PSignal();
21         void callPython(SWIG_PYOBJECT(ePyObject) tuple);
22         PyObject *get(bool steal=false);
23 };
24
25 inline PyObject *PyFrom(int v)
26 {
27         return PyInt_FromLong(v);
28 }
29
30 inline PyObject *PyFrom(const char *c)
31 {
32         return PyString_FromString(c);
33 }
34
35 template <class R>
36 class PSignal0: public PSignal, public Signal0<R>
37 {
38 public:
39         R operator()()
40         {
41                 bool destroyed=false;
42                 m_destroyed = &destroyed;
43                 if (m_list)
44                 {
45                         PyObject *pArgs = PyTuple_New(0);
46                         callPython(pArgs);
47                         Org_Py_DECREF(pArgs);
48                 }
49                 if (!destroyed) {
50                         m_destroyed = 0;
51                         return Signal0<R>::operator()();
52                 }
53         }
54 };
55
56 template <class R, class V0>
57 class PSignal1: public PSignal, public Signal1<R,V0>
58 {
59 public:
60         R operator()(V0 a0)
61         {
62                 bool destroyed=false;
63                 m_destroyed = &destroyed;
64                 if (m_list)
65                 {
66                         PyObject *pArgs = PyTuple_New(1);
67                         PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
68                         callPython(pArgs);
69                         Org_Py_DECREF(pArgs);
70                 }
71                 if (!destroyed) {
72                         m_destroyed = 0;
73                         return Signal1<R,V0>::operator()(a0);
74                 }
75         }
76 };
77
78 template <class R, class V0, class V1>
79 class PSignal2: public PSignal, public Signal2<R,V0,V1>
80 {
81 public:
82         R operator()(V0 a0, V1 a1)
83         {
84                 bool destroyed=false;
85                 m_destroyed = &destroyed;
86                 if (m_list)
87                 {
88                         PyObject *pArgs = PyTuple_New(2);
89                         PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
90                         PyTuple_SET_ITEM(pArgs, 1, PyFrom(a1));
91                         callPython(pArgs);
92                         Org_Py_DECREF(pArgs);
93                 }
94                 if (!destroyed) {
95                         m_destroyed = 0;
96                         return Signal2<R,V0,V1>::operator()(a0, a1);
97                 }
98         }
99 };
100
101 #endif