简体   繁体   中英

C++ callback with python through SWIG when the function takes non-trivial arguments?

I just read the following section of the "SWIG and Python" tutorial: http://www.swig.org/Doc1.1/HTML/Python.html#n11

I understand what they do in the example, basically you write a C function that calls python. However, the example provided takes only a double in the argument list, making it rather trivial to build an argument list for python.

I have a C++ callback that has the following signature:

Vector (*CALLBACK)(Simulation*, Vector, Vector, double, void*)

where Vector and Simulation are my own classes, and void* is for clientData holding a PyObject (as used in the example)

I know that SWIG wraps both the Vector and Simulation classes, because I can create them in a python shell. The only problem is, I have no idea how to transform a Vector argument in the callback function to an appropriate PyObject for the python argument list. SWIG obviously has some way to do this when it wraps the class itself, but I don't know how to accomplish it manually in the function.

Any help much appreciated! -Chase

I do not think your question is actually about SWIG; it is purely about Python. At this level, your problem is simply calling in to Python from C, so you need to study "Parsing arguments and building values" in the Python documentation. (Also "Extending and Embedding the Python interpreter" .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM