PLearn 0.1
Classes | Typedefs | Functions | Variables
test_trampoline.cc File Reference
#include <plearn/python/PythonCodeSnippet.h>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
Include dependency graph for test_trampoline.cc:

Go to the source code of this file.

Classes

struct  X

Typedefs

typedef boost::function< void()> XFunction

Functions

PyObject * python_trampoline (PyObject *self, PyObject *args)
int main ()

Variables

string python_code = " print >>sys.stderr, 'The C function returned the charming value',y\n"

Typedef Documentation

typedef boost::function<void ()> XFunction

Definition at line 32 of file test_trampoline.cc.


Function Documentation

int main ( )

Definition at line 45 of file test_trampoline.cc.

References PLearn::X::f(), python_code, python_trampoline(), and x.

{
    PP<PythonCodeSnippet> python = new PythonCodeSnippet(python_code);
    python->build();

    // Build my interesting instance of X
    X x(42);

    // Bind it to a function object
    XFunction xfunc = boost::bind(&X::f, x);

    // Create a Python Function Object
    PyMethodDef py_method;
    py_method.ml_name  = NULL;
    py_method.ml_meth  = python_trampoline;
    py_method.ml_flags = METH_VARARGS;
    py_method.ml_doc   = NULL;
    PyObject* py_funcobj = PyCFunction_NewEx(&py_method,
                                             reinterpret_cast<PyObject*>(&xfunc),
                                             NULL /* module */);

    // Inject into the running python snippet
    python->setGlobalObject("injected_c_function", py_funcobj);

    // And now call our darling
    python->invoke("trampoline_call", 64);

    Py_XDECREF(py_funcobj);
    return 0;
}

Here is the call graph for this function:

PyObject* python_trampoline ( PyObject *  self,
PyObject *  args 
)

Definition at line 34 of file test_trampoline.cc.

Referenced by main().

{
    XFunction *xfunc = reinterpret_cast<XFunction*>(self);

    // Should parse args here

    (*xfunc)();

    return PyInt_FromLong(64);
}

Here is the caller graph for this function:


Variable Documentation

string python_code = " print >>sys.stderr, 'The C function returned the charming value',y\n"
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines