Merge remote branch 'remotes/origin/pootle-import'
[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 #include <lib/python/python.h>
7
8 class PSignal
9 {
10 protected:
11         ePyObject m_list;
12 public:
13         PSignal();
14         ~PSignal();
15         void callPython(SWIG_PYOBJECT(ePyObject) tuple);
16 #ifndef SWIG
17         PyObject *getSteal(bool clear=false);
18 #endif
19         PyObject *get();
20 };
21
22 inline PyObject *PyFrom(int v)
23 {
24         return PyInt_FromLong(v);
25 }
26
27 inline PyObject *PyFrom(const char *c)
28 {
29         return PyString_FromString(c);
30 }
31
32 template <class R>
33 class PSignal0: public PSignal, public Signal0<R>
34 {
35 public:
36         R operator()()
37         {
38                 if (m_list)
39                 {
40                         PyObject *pArgs = PyTuple_New(0);
41                         callPython(pArgs);
42                         Org_Py_DECREF(pArgs);
43                 }
44                 return Signal0<R>::operator()();
45         }
46 };
47
48 template <class R, class V0>
49 class PSignal1: public PSignal, public Signal1<R,V0>
50 {
51 public:
52         R operator()(V0 a0)
53         {
54                 if (m_list)
55                 {
56                         PyObject *pArgs = PyTuple_New(1);
57                         PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
58                         callPython(pArgs);
59                         Org_Py_DECREF(pArgs);
60                 }
61                 return Signal1<R,V0>::operator()(a0);
62         }
63 };
64
65 template <class R, class V0, class V1>
66 class PSignal2: public PSignal, public Signal2<R,V0,V1>
67 {
68 public:
69         R operator()(V0 a0, V1 a1)
70         {
71                 if (m_list)
72                 {
73                         PyObject *pArgs = PyTuple_New(2);
74                         PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
75                         PyTuple_SET_ITEM(pArgs, 1, PyFrom(a1));
76                         callPython(pArgs);
77                         Org_Py_DECREF(pArgs);
78                 }
79                 return Signal2<R,V0,V1>::operator()(a0, a1);
80         }
81 };
82
83 #endif