The dcast member didn't seem to be populated (i.e. Int owndata /* flag if the structure owns the clientdata */ Void *clientdata /* language specific type data */ Struct swig_cast_info *cast /* linked list of types that can cast into this type */ Swig_dycast_func dcast /* dynamic cast function down a hierarchy */ If we look inside the generated test_wrap.cxx we can see the following definition of swig_type_info, which is emitted once per type that SWIG knows of: /* Structure to store information on one type */Ĭonst char *name /* mangled name of this type */Ĭonst char *str /* human readable name of this type */ G++ -Wall -Wextra -o _cast.so -shared -fPIC cast_wrap.cxx -I/usr/include/python2.7īut then didn't touch it other than by using import test inside my code. I compiled this with: swig3.0 -Wall -c++ -python test.i So to validate and develop this I put together the following, test.i file which is a minimal example of the problem you've got: %module test I should caveat this by saying that it feels like what I had to do to solve it ought to be more complex than necessary, but I can't see any other way of exposing the type hierarchy information from swig_type_info's internal to the module without doing it like this. i file in anyway, provided you can match the SWIG version that was used to build it and compiler close enough that we can rely on the struct layouts in memory being the same. SWIG does store enough information to make this possible and I was able to fairly simply achieve the result you desired without needing to patch/recompile/reverse-engineer/rely upon the original module's. The tricky bit about getting this cast to work properly is not just changing the type of the proxy, which you've done in your answer, but changing the type of the this member inside the proxy also as you recognised is needed in the desired output of your question. Ideally I'd like to do this in pure python, but if it must be wrapper around some C/C++ bear in mind I don't have access to the SWIG template or original C code. Note in the sample output, both output lines reference the same memory address. I'd like to be able to write: obj = MyBase() # eg might come from networkĬhild_obj = M圜om_raw_ptr(obj.get_raw_ptr()) # Pseudo-code On the Python side, the proxy objects for MyBase and M圜hild exist, but all objects enter python as a MyBase type. If (obj.is_child()) // runtime info lets us know what we are dealing with For example, typical C++ usage would be MyBase* obj = new MyBase() // eg might come via network The reason for doing this is to emulate C++ downcasting, but purely from python. Is there a way to downcast the swig proxy of a swig object on the fly?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |