|
From: Marcelo M. <mm...@ac...> - 2006-02-23 03:03:17
|
I think you are trying to re-implement by hand the "director" feature.
Read the Python docs about director, the main idea is like this:
You enable directors using the 'directors' option and the %director feature:
-------- myodule.i -------
%module(directors=1) mymodule
%director SolverReporter;
class SolverReporter{
SolverReporter();
virtual ~SolverReporter();
virtual int report(const SolverStatus &status) = 0;
}
-------- mymodule.i -------
Then, in python you use:
class PythonSolverReporter(mymodule.SoverReporter):
def __init__(self, something):
self.somtheing = something
mymodule.SoverReporter.__init__(self)
def report(self, status): //overload SoverReporter::report
if self.something:
print status.something
...
reporter = PythonSolverReporter(show_something)
simulation = Simulation(...)
simulation.solve(reporter)
and that is, 'reporter' is a C++ SoverReporter object for Simulation,
but execute the python code you define in the python side, ie:
def report(self, status): //overload SoverReporter::report
if self.something:
print status.something
...
Marcelo
John Pye wrote:
> Hi all,
>
> I'm having trouble using Py_BuildValue to pass a SWIG-wrapped *C++
> object* back to my python callback function. It says in the python
> docs that I can pass a PyObject using the "o" string, but I'm not sure
> how I can cast my C++ object to a PyObject from inside C++. What's the
> correct way of doing that?
>
> Here are some details: I'm setting up a callback mechanism for a
> Python method to display selected status data from a C++ solver
> engine. In C++ I have written a class 'SolverReporter':
>
> class SolverReporter{
> SolverReporter();
> virtual ~SolverReporter();
> virtual int report(const SolverStatus &status) = 0;
> }
>
> Then I have written a subclass PythonSolverReporter with the constructor
>
> PythonSolverReporter::PythonSolverReporter(PyObject *pyfunc){
> this->pyfunc = pyfunc;
> Py_INCREF(pyfunc);
> }
>
> and now, there's the 'report' method, and *this* is what I'm
> struggling with:
>
> int PythonSolverReporter::report(const SolverStatus &status){
> PyObject *pyarglist, *pyresult;
> pyarglist = Py_BuildValue("(o)",status); // <------------------
> what to do here?
> pyresult = PyEval_CallObject(this->pyfunc,pyarglist);
> Py_DECREF(pyarglist);
> }
>
> Usage of all this in Python would be like this, for example:
>
> def show_something(status):
> print "Solver status is",status.getIterationNum()
>
> reporter = PythonSolverReporter(show_something)
> simulation = Simulation(...)
> simulation.solve(reporter)
>
> Any suggestions -- even if it's a completely different way of doing
> things -- would be really appreciated.
>
> Cheers
> JP
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the live
> webcast
> and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Swig-user mailing list
> Swi...@li...
> https://lists.sourceforge.net/lists/listinfo/swig-user
|