Re: Question: how do I prevent a %ytpemap(out) for T* to match constructors of T?
William S Fulton <[email protected]> Mon, 29 Apr 2024 07:50:52 +0100
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CANGqftAdKXt06C=FnaN4OaR7O4nm-zJE6rd78teFRc+CBGGazg@mail.gmail.com> |
--===============1634118063577740980== Content-Type: multipart/alternative; boundary="000000000000cf8e14061736ae07" --000000000000cf8e14061736ae07 Content-Type: text/plain; charset="UTF-8" If you use the -debug-tmsearch option without any typemaps you will find the following in the output for the out typemap for the constructor: example.i:20: Searching for a suitable 'out' typemap for: SomeType *SomeType::SomeType Looking for: SomeType *SomeType::SomeType Looking for: SomeType *SomeType Looking for: SomeType * Looking for: SWIGTYPE *SomeType::SomeType Looking for: SWIGTYPE *SomeType Looking for: SWIGTYPE * Using: %typemap(out) SWIGTYPE * It shows the list of typemaps searched. I suggest you overwrite the typemap for the constructor with the default SWIGTYPE * with the following: %typemap(out) SomeType *SomeType::SomeType = SWIGTYPE *; Then you'll see it gets used as it is the first in the typemap search list, even with your own custom typemap present as the more specifc typemap is searched for first: example.i:18: Searching for a suitable 'out' typemap for: SomeType *SomeType::SomeType Looking for: SomeType *SomeType::SomeType Using: %typemap(out) SomeType *SomeType::SomeType = SWIGTYPE * See the docs for more info: https://swig.org/Doc4.2/Typemaps.html#Typemaps_pattern_matching. William On Wed, 24 Apr 2024 at 18:39, Alessio M <[email protected]> wrote: > Hi > Hopefully quick question: > I have the following typemap(out) that checks if a result object has_value > and returns Py_None if it does not. > Unfortunately it also matches the constructor of SomeType > > Is there a way to prevent that match or override the typemap for all > SomeType constructors > > I searched the docs, far and wide, and attempted a few things, but could > not find anything that would give me that level selectivity. > > Thank you for any help you can provide! > > %typemap(out) SomeType * %{ > if ( $1 && $1->has_value() ) > { > $result = SWIG_NewPointerObj((void*)$1, $descriptor(SometType *), > SWIG_BUILTIN_INIT | SWIG_POINTER_OWN); > } > else > { > delete $1; > $result = Py_None; > Py_INCREF(Py_None); > } > %} > _______________________________________________ > Swig-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/swig-user > --000000000000cf8e14061736ae07 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div>If you use the -debug-tmsearch option without any typ= emaps you will find the following in the output for the out typemap for the= constructor:</div><div><br></div><div style=3D"margin-left:40px">example.i= :20: Searching for a suitable 'out' typemap for: SomeType *SomeType= ::SomeType<br>=C2=A0 Looking for: SomeType *SomeType::SomeType<br>=C2=A0 Lo= oking for: SomeType *SomeType<br>=C2=A0 Looking for: SomeType *<br>=C2=A0 L= ooking for: SWIGTYPE *SomeType::SomeType<br>=C2=A0 Looking for: SWIGTYPE *S= omeType<br>=C2=A0 Looking for: SWIGTYPE *<br>=C2=A0 Using: %typemap(out) SW= IGTYPE *</div><div><br></div><div>It shows the list of typemaps searched. I= suggest you overwrite the typemap for the constructor with the default SWI= GTYPE * with the following:</div><div><br></div><div style=3D"margin-left:4= 0px">%typemap(out) SomeType *SomeType::SomeType =3D SWIGTYPE *;</div><div><= br></div><div>Then you'll see it gets used as it is the first in the ty= pemap search list, even with your own custom typemap present as the more sp= ecifc typemap is searched for first:<br></div><div><br></div><div style=3D"= margin-left:40px">example.i:18: Searching for a suitable 'out' type= map for: SomeType *SomeType::SomeType<br>=C2=A0 Looking for: SomeType *Some= Type::SomeType<br>=C2=A0 Using: %typemap(out) SomeType *SomeType::SomeType = =3D SWIGTYPE *</div><div><br></div><div>See the docs for more info: <a href= =3D"https://swig.org/Doc4.2/Typemaps.html#Typemaps_pattern_matching">https:= //swig.org/Doc4.2/Typemaps.html#Typemaps_pattern_matching</a>. <br></div><d= iv><br></div><div>William</div><div><br></div><div><br></div></div><br><div= class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, 24 Apr= 2024 at 18:39, Alessio M <<a href=3D"mailto:masariello%2Bswig-user@gmai= l.com">[email protected]</a>> wrote:<br></div><blockquote c= lass=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px soli= d rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Hi<div>Hopefully quic= k question:</div><div>I have the following typemap(out) that checks if a re= sult object has_value and returns Py_None if it does not.</div><div>Unfortu= nately it also matches the constructor of SomeType</div><div><br></div><div= >Is there a way to prevent that match or override the typemap for all SomeT= ype constructors</div><div><br></div><div>I searched the=C2=A0docs, far and= wide, and attempted a few things, but could not find anything that would g= ive me that level selectivity.</div><div><br></div><div>Thank you for any h= elp you can provide!</div><div><br></div><div>%typemap(out) SomeType * %{</= div><div>=C2=A0 if ( $1 && $1->has_value() )</div><div>=C2=A0 {<= /div><div>=C2=A0 =C2=A0 $result =3D SWIG_NewPointerObj((void*)$1, $descript= or(SometType *), SWIG_BUILTIN_INIT | SWIG_POINTER_OWN);</div><div>=C2=A0 }<= /div><div>=C2=A0 else</div><div>=C2=A0 {</div><div>=C2=A0 =C2=A0 delete $1;= </div><div>=C2=A0 =C2=A0 $result =3D Py_None;</div><div>=C2=A0 =C2=A0 Py_IN= CREF(Py_None);</div><div>=C2=A0 }</div><div>%}</div></div> _______________________________________________<br> Swig-user mailing list<br> <a href=3D"mailto:[email protected]" target=3D"_blank">Swig-u= [email protected]</a><br> <a href=3D"https://lists.sourceforge.net/lists/listinfo/swig-user" rel=3D"n= oreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/s= wig-user</a><br> </blockquote></div> --000000000000cf8e14061736ae07-- --===============1634118063577740980== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============1634118063577740980== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Swig-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/swig-user --===============1634118063577740980==--