c++ - Swig: SWIG_NewPointerObj and Typemaps -
i'm working on qt-application , use swig generate python bindings public api.
i trying implement signal/slots in swig , embedding python final app. in end boils down simple function, swig seems miss or haven't found yet:
pyobject* createpyobject(qstring typename, bool passownership, void* obj)
requirements
the function should create pyobject passed obj
. whereas typename
type of instance obj
points , passownership
specifies whether or not object should owned python interpreter afterwards. function should:
- apply out-typemap passed type, if exists
- otherwise wrap object "swig proxy object of type ..." if possible
- and if that's not possible should return "swig object of type ..."
example
let's have 3 classes:
class a1
have out-typemap convert instances of class python-integers.class a2
have out-typemap convert instances of class python-tuples.class b
have wrapped using swig interface fileclass c
unknown swig.
then createpyobject
should
- return
pyintobject*
if it's invokedcreatepyobj("a1",true,new a1())
- return
pytupleobject*
if it's invokedcreatepyobj("a2",true,new a2())
- return
proxy of <swig object of type 'b*'>
if it's invokedcreatepyobj("b",true,new b())
- return
swig object of type 'c*'
if it's invokedcreatepyobj("c",true,new c())
so summarize: want returned pyobject constructed if return value of function wrapped using swig interface file.
my current implementation
pyobject* createpyobject(qstring typename, bool passownership, void* obj) { typename += " *"; //pointer symbol swig_type_info* info = swig_typequery(typename.tostdstring().c_str()); if (info == nullptr) return nullptr; return swig_newpointerobj(obj, info, passownership ? swig_pointer_own : 0); }
this works fine types require no typemap , known swig (point 2 in requirements). doesn't work types require typemap.
could provide me correct implementation of createpyobject
? correct mean, 1 meets requirements listed above.
Comments
Post a Comment