From: Michael Z. <mz...@im...> - 2007-12-04 22:01:50
|
Suppose we have a simple header file: #include <boost/shared_ptr.hpp> #include <iostream> struct Foo { int a; }; typedef boost::shared_ptr<Foo> RcPtr; struct Result { RcPtr ptr; }; Result getResult(); RcPtr getRcPtr(); The swig interface file is %module test %{ #include "test.h" %} %typemap(out) boost::shared_ptr<Foo> { std::cout << "I am here"; $result = NULL; } %include "test.h" If you look at the generated cpp file, in the function _wrap_Result_ptr_get, typemap defined in the swig file is successfully inserted into the function as the following. result = ((arg1)->ptr); { std::cout << "I am here"; resultobj = NULL; } return resultobj; This is great. However, in the Swig interface file above, we didn't include shared_ptr.i, which means you can not use shared pointer. In order to make shared pointer, we change the swig interface to : %include "shared_ptr.i" %module test %{ #include "test.h" %} %typemap(out) boost::shared_ptr<Foo> { std::cout << "I am here"; $result = NULL; } %include "test.h" Ok, shared pointer is working. But, if you look at the generated cpp code, in the function _wrap_Result_ptr_get, the typemap seems not working any more. The output is like: result = (RcPtr *)& ((arg1)->ptr); resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_boost__shared_ptrTFoo_t, 0 | 0 ); return resultobj; I find the similar behavior for other kind of membership variable in a structure. struct Result { std::vector<std::string> list; }; struct Result2 { std::map<std::string, std::string> amap; }; Typemaps is defined in the swig file: %typemap(out) std::vector<std::string> { // here $result = NULL; } %typemap(out) std::map<std::string, std::string> { // here $result = NULL; } If you do not put %include "std_string.i" in the swig file, typemaps works great. However, if you put %include "std_string.i" into the swig file, typemaps seems not working any more. Any input is welcomed. Thanks! |