3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
25 * N.B., this is a _very_ non-standard SWIG .i file
27 * It contains a bunch of magic that is required to ensure that when
28 * these classes are used as base classes for python code,
29 * everything works when calling back from C++ into Python.
31 * The gist of the problem is that our C++ code is usually not holding
32 * the Python Global Interpreter Lock (GIL). Thus if we invoke a
33 * "director" method from C++, we'll end up in Python not holding the
34 * GIL. Disaster (SIGSEGV) will result. To avoid this we insert a
35 * "shim" that grabs and releases the GIL.
37 * If you don't understand SWIG "directors" or the Python GIL,
38 * don't bother trying to understand what's going on in here.
40 * [We could eliminate a bunch of this hair by requiring SWIG 1.3.29
41 * or later and some additional magic declarations, but many systems
42 * aren't shipping that version yet. Thus we kludge...]
46 // Enable SWIG directors for these classes
47 %feature("director") gr_py_feval_dd;
48 %feature("director") gr_py_feval_cc;
49 %feature("director") gr_py_feval_ll;
50 %feature("director") gr_py_feval;
52 %feature("nodirector") gr_py_feval_dd::calleval;
53 %feature("nodirector") gr_py_feval_cc::calleval;
54 %feature("nodirector") gr_py_feval_ll::calleval;
55 %feature("nodirector") gr_py_feval::calleval;
58 %rename(feval_dd) gr_py_feval_dd;
59 %rename(feval_cc) gr_py_feval_cc;
60 %rename(feval_ll) gr_py_feval_ll;
61 %rename(feval) gr_py_feval;
65 // catch (Swig::DirectorException &e) { std::cerr << e.getMessage(); SWIG_fail; }
70 // class that ensures we acquire and release the Python GIL
72 class ensure_py_gil_state {
73 PyGILState_STATE d_gstate;
75 ensure_py_gil_state() { d_gstate = PyGILState_Ensure(); }
76 ~ensure_py_gil_state() { PyGILState_Release(d_gstate); }
82 * These are the real C++ base classes, however we don't want these exposed.
88 virtual double eval(double x);
92 virtual ~gr_feval_dd();
94 virtual double calleval(double x);
101 virtual gr_complex eval(gr_complex x);
105 virtual ~gr_feval_cc();
107 virtual gr_complex calleval(gr_complex x);
114 virtual long eval(long x);
118 virtual ~gr_feval_ll();
120 virtual long calleval(long x);
133 virtual void calleval();
137 * These are the ones to derive from in Python. They have the magic shim
138 * that ensures that we're holding the Python GIL when we enter Python land...
143 class gr_py_feval_dd : public gr_feval_dd
146 double calleval(double x)
148 ensure_py_gil_state _lock;
153 class gr_py_feval_cc : public gr_feval_cc
156 gr_complex calleval(gr_complex x)
158 ensure_py_gil_state _lock;
163 class gr_py_feval_ll : public gr_feval_ll
166 long calleval(long x)
168 ensure_py_gil_state _lock;
173 class gr_py_feval : public gr_feval
178 ensure_py_gil_state _lock;
187 // examples / test cases
189 %rename(feval_dd_example) gr_feval_dd_example;
190 double gr_feval_dd_example(gr_feval_dd *f, double x);
192 %rename(feval_cc_example) gr_feval_cc_example;
193 gr_complex gr_feval_cc_example(gr_feval_cc *f, gr_complex x);
195 %rename(feval_ll_example) gr_feval_ll_example;
196 long gr_feval_ll_example(gr_feval_ll *f, long x);
198 %rename(feval_example) gr_feval_example;
199 void gr_feval_example(gr_feval *f);