From: Toivo H. <toi...@mo...> - 2013-10-29 13:39:37
|
I was able to make my example work by not using typemaps directly but creating SWIG_From_frag and SWIG_AsVal_frag fragments for my type (IntBox), and then using the macros %traits_value and %typemap_traits on it before doing anything about std::vector<IntBox>. I had to copy the definition of %traits_value from swig2.0/typemaps/traits.swg, a file which does not seem to be used but has definitions that conflict with typemaps/std_common.i. (I am using SWIG v2.0.4) This makes me wonder if I am doing things right. Is there a better way to achieve this? I could not find any documentation for the fragments and macros used below, so I had to guess from their usage in the SWIG library source code. Below is my working intbox.i: (that makes vector<IntBox> make use of the same typemaps as IntBox itself does) %module intbox %include <std/std_common.i> ... definition of %traits_value(Type...) copied straight from swig2.0/typemaps/traits.swg ... %{ #include "intbox.hpp" %} %fragment(SWIG_From_frag(IntBox),"header", fragment=SWIG_From_frag(int)) { SWIGINTERNINLINE SWIG_Object SWIG_From(IntBox)(const IntBox& box) { return SWIG_From(int)(box.x); } } %fragment(SWIG_AsVal_frag(IntBox),"header", fragment=SWIG_AsVal_frag(int)) { SWIGINTERN int SWIG_AsVal(IntBox)(SWIG_Object o, IntBox* val) { int x; int result = SWIG_AsVal(int)(o, &x); if (SWIG_IsOK(result) && val) { val->x = x; } return result; } } %traits_value(IntBox); %typemap_traits(SWIG_TYPECHECK_INT32, IntBox); %include <std_vector.i> %template(IntBoxVector) std::vector<IntBox>; %include "intbox.hpp" Thanks, Toivo > -----Original Message----- > From: Toivo Henningsson > Sent: den 23 oktober 2013 10:28 > To: 'swi...@li...' > Subject: std_vector.i with custom typemaps for the elements > > Hi, > > Perhaps my last mail was too long. I will try to make a condensed version below: > > I'm having some trouble with using std_vector.i to wrap std::vector<T>, where I > have defined my own typemaps for T. A simple example: > > I create my own class IntBox, and define in and out typemaps for it. > My intbox.i looks like > > %module intbox > > %{ > #include "intbox.hpp" > %} > > ... in and out typemaps for IntBox ... > > %include "std_vector.i" > %template(IntBoxVector) std::vector<IntBox>; > %include "intbox.hpp" > > Now, when I invoke functions from python that return or take IntBox directly, > the typemaps take effect as expected. But when I invoke functions from python > that return or take std::vector<IntBox>, it is as if I had never defined any > typemaps for IntBox at all. > > How can I make std_vector.i aware of my own typemaps on the element type? > > Thanks, > Toivo This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Modelon does not accept or assume any liability or responsibility for any use of or reliance on this email. |