small optimizations and cleanups by Moritz Venn
[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 public:
18         PSignal();
19         ~PSignal();
20         void callPython(SWIG_PYOBJECT(ePyObject) tuple);
21 #ifndef SWIG
22         PyObject *getSteal(bool clear=false);
23 #endif
24         PyObject *get();
25 };
26
27 inline PyObject *PyFrom(int v)
28 {
29         return PyInt_FromLong(v);
30 }
31
32 inline PyObject *PyFrom(const char *c)
33 {
34         return PyString_FromString(c);
35 }
36
37 template <class R>
38 class PSignal0: public PSignal, public Signal0<R>
39 {
40 public:
41         R operator()()
42         {
43                 if (m_list)
44                 {
45                         PyObject *pArgs = PyTuple_New(0);
46                         callPython(pArgs);
47                         Org_Py_DECREF(pArgs);
48                 }
49                 return Signal0<R>::operator()();
50         }
51 };
52
53 template <class R, class V0>
54 class PSignal1: public PSignal, public Signal1<R,V0>
55 {
56 public:
57         R operator()(V0 a0)
58         {
59                 if (m_list)
60                 {
61                         PyObject *pArgs = PyTuple_New(1);
62                         PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
63                         callPython(pArgs);
64                         Org_Py_DECREF(pArgs);
65                 }
66                 return Signal1<R,V0>::operator()(a0);
67         }
68 };
69
70 template <class R, class V0, class V1>
71 class PSignal2: public PSignal, public Signal2<R,V0,V1>
72 {
73 public:
74         R operator()(V0 a0, V1 a1)
75         {
76                 if (m_list)
77                 {
78                         PyObject *pArgs = PyTuple_New(2);
79                         PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
80                         PyTuple_SET_ITEM(pArgs, 1, PyFrom(a1));
81                         callPython(pArgs);
82                         Org_Py_DECREF(pArgs);
83                 }
84                 return Signal2<R,V0,V1>::operator()(a0, a1);
85         }
86 };
87
88 #endif