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
|
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
%module otk_timer
%{
#include "config.h"
#include "timer.hh"
%}
%{
struct PythonCallbackData {
PyObject *pyfunc;
void *data;
};
/*
Calls a python callback for the TimeoutHandler function type
*/
static void PythonCallback(PythonCallbackData *calldata) {
PyObject *arglist, *result;
arglist = Py_BuildValue("(O)", calldata->data);
// call the callback
result = PyEval_CallObject((PyObject*)calldata->pyfunc, arglist);
if (!result || PyErr_Occurred()) {
// an exception occured in the script, display it
PyErr_Print();
}
Py_XDECREF(result);
Py_DECREF(arglist);
}
%}
// Grab a Python function object as a Python object.
%typemap(python,in) PyObject *func {
if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
return NULL;
}
$1 = $input;
}
namespace otk {
%ignore Timer::Timer(long, TimeoutHandler, void*);
%ignore Timer::operator delete(void*);
%ignore Timer::initialize();
%ignore Timer::destroy();
%ignore Timer::dispatchTimers(bool);
%ignore Timer::nearestTimeout(struct timeval&);
%extend Timer {
Timer(long, PyObject*, PyObject*);
// if you don't call stop() before the object disappears, the timer will
// keep firing forever
void stop() {
delete self;
}
}
}
%{
static otk::Timer *new_otk_Timer(long delay,
PyObject *func, PyObject *data) {
PythonCallbackData *d = new PythonCallbackData;
d->pyfunc = func;
d->data = data;
return new otk::Timer(delay,
(otk::Timer::TimeoutHandler)&PythonCallback, d);
// the PythonCallbackData is leaked.. XXX
}
%}
%include "timer.hh"
|