aboutsummaryrefslogtreecommitdiff
path: root/lib/python/connections.h
blob: 8a06f7faaa3d44ebd2c84b3ffdc05b5b1b0c9491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
#ifndef __lib_python_connections_h
#define __lib_python_connections_h

#include <libsig_comp.h>

		/* avoid warnigs :) */
#include <features.h>
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#include <Python.h>
#include <lib/python/python.h>

class PSignal
{
public:
	PyObject *m_list;
public:
	PSignal()
	{
		m_list = PyList_New(0);
		Py_INCREF(m_list);
	}
	~PSignal()
	{
		Py_DECREF(m_list);
	}
	
	void callPython(PyObject *tuple)
	{
		int size = PyList_Size(m_list);
		int i;
		for (i=0; i<size; ++i)
		{
			PyObject *b = PyList_GET_ITEM(m_list, i);
			ePython::call(b, tuple);
		}
	}
	
	
	PyObject *get() { Py_INCREF(m_list); return m_list; }
};

inline PyObject *PyFrom(int v)
{
	return PyInt_FromLong(v);
}

inline PyObject *PyFrom(const char *c)
{
	return PyString_FromString(c);
}

template <class R>
class PSignal0: public PSignal, public Signal0<R>
{
public:
	R operator()()
	{
		PyObject *pArgs = PyTuple_New(0);
		callPython(pArgs);
		Py_DECREF(pArgs);
		return Signal0<R>::operator()();
	}
};

template <class R, class V0>
class PSignal1: public PSignal, public Signal1<R,V0>
{
public:
	R operator()(V0 a0)
	{
		PyObject *pArgs = PyTuple_New(1);
		PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
		callPython(pArgs);
		Py_DECREF(pArgs);
		return Signal1<R,V0>::operator()(a0);
	}
};

template <class R, class V0, class V1>
class PSignal2: public PSignal, public Signal2<R,V0,V1>
{
public:
	R operator()(V0 a0, V1 a1)
	{
		PyObject *pArgs = PyTuple_New(2);
		PyTuple_SET_ITEM(pArgs, 0, PyFrom(a0));
		PyTuple_SET_ITEM(pArgs, 1, PyFrom(a1));
		callPython(pArgs);
		Py_DECREF(pArgs);
		return Signal2<R,V0,V1>::operator()(a0, a1);
	}
};

#endif