From: lorubenet <lor...@go...> - 2013-04-22 13:51:51
|
Hi swig-users! I am using swig for quite some time now to export C++ to tcl and python. It is very good thanks! I have never been able to solve the following problem though: We want to export a method which returns an std::vector<double>. Swig works this out fine but returns a native List in tcl and a tuple in python. We want it to return a wrapped std::vector instead. Is there a way, like a flag or something, where I change that? I just found out today (which gave me hope) that in python this is possible by adding: %{ #define SWIG_PYTHON_EXTRA_NATIVE_CONTAINERS %} in the i file. I tested that successfully. I cannot find something similar for tcl. Checking the cxx code generated by swig for python seems clear why this "#define" does the job. I also checked the cxx code generated for tcl but all seems hardcoded there. The code gets first a "std::vector<double> result" and then it copies it into a TclList: for (unsigned int i=0; i<(&result)->size(); i++) { Tcl_ListObjAppendElement(interp, (Tcl_GetObjResult(interp)), \ Tcl_NewDoubleObj(((std::vector< double > &)result)[i])); } I have been further playing with this (since the mailing list rejected my email at first). I did this change in the std_vector.i file in the tcl directory of the swig installation: %typemap(out) vector< T > { %#ifdef SWIG_TCL_EXTRA_NATIVE_CONTAINERS std::vector< T > * pRes = (std::vector< T > *)new std::vector< T >($1); Tcl_SetObjResult(interp, SWIG_NewInstanceObj( %as_voidptr(pRes), $&1_descriptor,0)); %#else for (unsigned int i=0; i<$1.size(); i++) { Tcl_ListObjAppendElement(interp, $result, \ CONVERT_TO((($1_type &)$1)[i])); } %#endif } Adding then the ifdefs and the code within the SWIG_TCL_EXTRA_NATIVE_CONTAINERS. This seems to work, but I have no clue whether it makes sense to do it this way, nor whether it will fail in some other place... Thanks in advance, Ruben |