Skip to main content
gamedev.net
🔒 Locked

Hardcore Python/C++ integration

Started by Uberman Sep 25, 2008 at 12:33 PM 1 replies 1.2k views
Original Post
Uberman
Uberman
I've tried to get an answer to this in two other location (comp.lang.python and the SWIG Users List), but I've been pretty much ignored. I hope there are people here who can handle it. I've exported a C++ class into the Python environment using SWIG. Instances of this C++ class are used by the application and, via SWIG, now there are also instances within Python. I need to (preferably elegantly) exchange the data contained in the C++ and Python instances bidirectionally. In other words, I can exchange integral types between C++ and Python using the Python conversion functions (e.g., PyLong_AsLong() and PyLong_FromLong()). How then do I do something similar for my user-defined C++ class instances? There is the brute-force approach of writing long lines of C++ conversion code for the class, reading individual attributes from each and cross-pollinating to create opposing instances. This is the worst-case approach, because it requires constant maintenance of the code to keep up with any changes to the class. Not an option. An approach that I'm considering is adding helper functions within the exported SWIG code that will convert the PyObject of my class instance into a pointer to a C++ instance, and return that for class copy operations (i.e., *python_instance = cpp_instance, or vice-versa). I've not yet tried this approach to see if it would actually work, and I am aware of the need to keep memory allocations localized to the correct heaps. Is there some SWIG- or Python-specific approach I'm missing for elegantly exchanging user-defined data types between C++ and Python? I'm sure I could modify the Python interpreter to add my own conversion functions, but that means then maintaining versions of Python along with my application, which just adds more overhead. Thanks for any help.
Richy2k
Richy2k
I personally prefer using SIP for my bindings, got most of my engine exposed through it including base game states, menu states, etc etc.

With SWIG you should just be able to put the typedefs and structs into the .i file much like I do with SIP, then the code to transfer between C++/Python is generated for you.

To call Python from C++ using user defined C++ datatypes, I can simply use some of SIPs conversion routines, build a tuple, and pass that as the parameters - I haven't tried this with SWIG mind. I've actually got a base object which automatically converts C++ types to their Python wrapper types in the constructor, and also accessors to extract the C++ types from the Python wrapper instances on the same object.
Uberman
Uberman
After writing the original post, I stumbled upon a fairly elegant solution (at least, for my situation) using SWIG, that will work for me until something better comes along.

Thanks for taking the time to respond, Richy.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.