Re: Fwd: Re: how to map 'new' to 'CUSTOM_NEW_T'

William S Fulton <[email protected]> Mon, 20 Jan 2025 08:57:15 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftCnkrz6jmmpJdqBtXuh7kBqMtYT8CK19gWpGfaC1MDWuQ@mail.gmail.com>
--===============0837363561340716342==
Content-Type: multipart/alternative; boundary="00000000000092c60f062c1f74a8"

--00000000000092c60f062c1f74a8
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

The goal here is to customise this line:

  result =3D (Bar *)new Bar();

to something else:

  result=3DCUSTOM_NEW_T(Bar);

This is the "action" part of the code in SWIG and can be achieved via the
low level "action" feature as follows:

  %feature("action") Bar::Bar "result=3DCUSTOM_NEW_T(Bar);"

Note that as this is a feature, the method (contructors in this case) being
wrapped are specified for matching, that is Bar::Bar. Instead of matching
all constructors, you can of course match, say just the default
constructor, with:

  %feature("action") Bar::Bar() "result=3DCUSTOM_NEW_T(Bar);"

That covers the memory allocation for constructor wrappers and you can of
course match the destructor if you want to control memory deallocation for
destructor wrappers. SWIG will use 'new' and 'delete' in a number of places
and these can be modified via appropriate typemaps, such as the 'out'
typemap that Ephraim showed which is used when wrapping a function
returning by value. There are many others that may need customising
depending on how the type is being used/wrapped.

William

On Sun, 22 Dec 2024 at 09:45, Ephraim Yawitz <[email protected]>
wrote:

>
>
> ---------- Forwarded message ---------
> From: Ephraim Yawitz <[email protected]>
> Date: Sun, Dec 22, 2024 at 11:22=E2=80=AFAM
> Subject: Re: Re: [Swig-user] how to map 'new' to 'CUSTOM_NEW_T'
> To: Jim <[email protected]>
>
>
> I'm very surprised that this can't be done with typemaps. Did anyone else
> on the list confirm this?
>
> On Fri, Dec 20, 2024 at 12:05=E2=80=AFPM Jim <[email protected]> wrote=
:
>
>> Yeah, I have read all the code of the 'csharp.swg' and other files, and
>> looks like can't implent with typemap. So I clone the git repository and
>> found the below code may affect the 'new' operator:
>>
>> String* null_attribute =3D 0;
>>
>> // Now write code to make the function call
>>
>> if (!native_function_flag) {
>>
>>
>> Swig_director_emit_dynamic_cast(n, f);
>>
>> String* actioncode =3D emit_action(n);
>>
>>
>> /* Return value if necessary  */
>>
>> if ((tm =3D Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f,
>> actioncode))) {
>>
>> canThrow(n, "out", n);
>>
>> Replaceall(tm, "$result", "jresult");
>>
>>
>> if (GetFlag(n, "feature:new"))
>>
>> Replaceall(tm, "$owner", "1");
>>
>> else
>>
>> Replaceall(tm, "$owner", "0");
>>
>> the 'emit_action' returned  string is "result =3D (Bar *)new Bar();".  S=
o I
>> digged the emit_action but have no idea about how to modify it.
>>
>>
>>
>>
>>
>> At 2024-12-20 00:05:38, "Ephraim Yawitz" <[email protected]> wrote=
:
>>
>> I see no one has followed this up, but in the meantime I have tried a
>> little example of what I suggested and it didn't work, i.e., it still us=
ed
>> "new" instead of "CUSTOM_NEW_T", so I guess the issue is more complicate=
d
>> than I thought, although it might just need a small tweak in the typemap=
.
>>
>> On Tue, Dec 17, 2024 at 11:58=E2=80=AFAM Ephraim Yawitz <ephraim@excalib=
ur.co.il>
>> wrote:
>>
>>> I'm no SWIG expert, but from the little bit I understand, the code for
>>> this C++ constructor is created based on the following lines in
>>> Lib/csharp/csharp.swg in the SWIG installation directory:
>>>
>>> %typemap(out) SWIGTYPE
>>> #ifdef __cplusplus
>>> %{ $result =3D new $1_ltype($1); %}
>>> #else
>>> {
>>>   $&1_ltype $1ptr =3D ($&1_ltype) malloc(sizeof($1_ltype));
>>>   memmove($1ptr, &$1, sizeof($1_type));
>>>   $result =3D $1ptr;
>>> }
>>> #endif
>>>
>>> I'm guessing that perhaps creating a %typemap of your own with
>>> "CUSTOM_NEW_T" instead of "new" might do the trick.
>>> Again, I'm just posting this to try to get the attention of the guys wh=
o
>>> really know what they're doing.
>>>
>>> On Mon, Dec 16, 2024 at 4:53=E2=80=AFAM Jim <[email protected]> wrot=
e:
>>>
>>>> Hi all,
>>>>     I am using swig to generate csharp code, here is the default
>>>> implementation of the constructor:
>>>>  I want change the 'new' operator to my custom 'CUSTOM_NEW_T'=EF=BC=8C=
like
>>>> 'result=3DCUSTOM_NEW_T(Bar)'.   I have read the doc for SWIG, but it s=
eems
>>>> that does not contain any relevant content.
>>>>
>>>>  Thank you so much, have a nice day!
>>>> _______________________________________________
>>>> Swig-user mailing list
>>>> [email protected]
>>>> https://lists.sourceforge.net/lists/listinfo/swig-user
>>>>
>>> _______________________________________________
> Swig-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

--00000000000092c60f062c1f74a8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>The goal here is to customise this line:</div><div><b=
r></div><div>=C2=A0 result =3D (Bar *)new Bar();</div><div><br></div><div>t=
o something else:</div><div><br></div><div>=C2=A0 result=3DCUSTOM_NEW_T(Bar=
);</div><div><br></div><div>This is the &quot;action&quot; part of the code=
 in SWIG and can be achieved via the low level &quot;action&quot; feature a=
s follows:</div><div><br></div><div>=C2=A0 %feature(&quot;action&quot;) Bar=
::Bar &quot;result=3DCUSTOM_NEW_T(Bar);&quot; <br></div><div><br></div><div=
>Note that as this is a feature, the method (contructors in this case) bein=
g wrapped are specified for matching, that is Bar::Bar. Instead of matching=
 all constructors, you can of course match, say just the default constructo=
r, with:</div><div><br></div><div><div>=C2=A0 %feature(&quot;action&quot;) =
Bar::Bar() &quot;result=3DCUSTOM_NEW_T(Bar);&quot; <br></div><div><br></div=
><div>That covers the memory allocation for constructor wrappers and you ca=
n of course match the destructor if you want to control memory deallocation=
 for destructor wrappers. SWIG will use &#39;new&#39; and &#39;delete&#39; =
in a number of places and these can be modified via appropriate typemaps, s=
uch as the &#39;out&#39; typemap that Ephraim showed which is used when wra=
pping a function returning by value. There are many others that may need cu=
stomising depending on how the type is being used/wrapped.<br></div><div><b=
r></div>William<br></div><div><br></div><div class=3D"gmail_quote gmail_quo=
te_container"><div dir=3D"ltr" class=3D"gmail_attr">On Sun, 22 Dec 2024 at =
09:45, Ephraim Yawitz &lt;<a href=3D"mailto:[email protected]">ephrai=
[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote"=
 style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);p=
adding-left:1ex"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr" class=3D"gmail_attr">---------- Forwarded message ---------<br>F=
rom: <b class=3D"gmail_sendername" dir=3D"auto">Ephraim Yawitz</b> <span di=
r=3D"auto">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a>&gt;</span><br>Date: Sun, Dec 22, 2024 at 11:22=
=E2=80=AFAM<br>Subject: Re: Re: [Swig-user] how to map &#39;new&#39; to &#3=
9;CUSTOM_NEW_T&#39;<br>To: Jim &lt;<a href=3D"mailto:[email protected]" =
target=3D"_blank">[email protected]</a>&gt;<br></div><br><br><div dir=3D=
"ltr">I&#39;m very surprised that this can&#39;t be done with typemaps. Did=
 anyone else on the list confirm this?<br></div><br><div class=3D"gmail_quo=
te"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, Dec 20, 2024 at 12:05=E2=
=80=AFPM Jim &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">=
[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quo=
te" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204=
);padding-left:1ex"><div style=3D"line-height:1.7;color:rgb(0,0,0);font-siz=
e:14px;font-family:Arial"><div id=3D"m_-7450050050691935762m_-6177578573782=
233742m_-7802994937922086891spnEditorContent"><p style=3D"margin:0px">Yeah,=
 I have read all the code of the &#39;csharp.swg&#39; and other files, and =
looks like can&#39;t implent with typemap. So I clone the git repository an=
d found the below code may affect the &#39;new&#39; operator:</p><p style=
=3D"margin:0px"><span style=3D"color:rgb(141,148,64)">String* null_attribut=
e =3D 0;</span></p><p style=3D"margin:0px"><span style=3D"color:rgb(141,148=
,64)">// Now write code to make the function call</span></p><p style=3D"mar=
gin:0px"><span style=3D"color:rgb(141,148,64)">if (!native_function_flag) {=
</span></p><p style=3D"margin:0px"><br></p><p style=3D"margin:0px"><span st=
yle=3D"white-space:normal;color:rgb(141,148,64)"><span style=3D"color:rgb(1=
41,148,64);white-space:pre-wrap">	</span>Swig_director_emit_dynamic_cast(n,=
 f);</span></p><p style=3D"margin:0px"><span style=3D"white-space:normal;co=
lor:rgb(141,148,64)"><span style=3D"color:rgb(141,148,64);white-space:pre-w=
rap">	</span>String* actioncode =3D emit_action(n);</span></p><p style=3D"m=
argin:0px"><br></p><p style=3D"margin:0px"><span style=3D"white-space:norma=
l;color:rgb(141,148,64)"><span style=3D"color:rgb(141,148,64);white-space:p=
re-wrap">	</span>/* Return value if necessary=C2=A0 */</span></p><p style=
=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(141,148,64)"><s=
pan style=3D"color:rgb(141,148,64);white-space:pre-wrap">	</span>if ((tm =
=3D Swig_typemap_lookup_out(&quot;out&quot;, n, Swig_cresult_name(), f, act=
ioncode))) {</span></p><p style=3D"margin:0px"><span style=3D"white-space:n=
ormal;color:rgb(141,148,64)"><span style=3D"color:rgb(141,148,64);white-spa=
ce:pre-wrap">		</span>canThrow(n, &quot;out&quot;, n);</span></p><p style=
=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(141,148,64)"><s=
pan style=3D"color:rgb(141,148,64);white-space:pre-wrap">		</span>Replaceal=
l(tm, &quot;$result&quot;, &quot;jresult&quot;);</span></p><p style=3D"marg=
in:0px"><br></p><p style=3D"margin:0px"><span style=3D"white-space:normal;c=
olor:rgb(141,148,64)"><span style=3D"color:rgb(141,148,64);white-space:pre-=
wrap">		</span>if (GetFlag(n, &quot;feature:new&quot;))</span></p><p style=
=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(141,148,64)"><s=
pan style=3D"color:rgb(141,148,64);white-space:pre-wrap">			</span>Replacea=
ll(tm, &quot;$owner&quot;, &quot;1&quot;);</span></p><p style=3D"margin:0px=
"><span style=3D"white-space:normal;color:rgb(141,148,64)"><span style=3D"c=
olor:rgb(141,148,64);white-space:pre-wrap">		</span>else</span></p><p style=
=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(141,148,64)"><s=
pan style=3D"color:rgb(141,148,64);white-space:pre-wrap">			</span>Replacea=
ll(tm, &quot;$owner&quot;, &quot;0&quot;);</span></p><p style=3D"margin:0px=
">the &#39;emit_action&#39; returned=C2=A0 string is &quot;result =3D (Bar =
*)new Bar();&quot;.=C2=A0 So I digged the emit_action but have no idea abou=
t how to modify it.</p><p style=3D"margin:0px"><br></p><p style=3D"margin:0=
px"><br></p><p style=3D"margin:0px"><br></p></div><div></div><div id=3D"m_-=
7450050050691935762m_-6177578573782233742m_-7802994937922086891divNeteaseMa=
ilCard"></div><p style=3D"margin:0px"><br></p><p>At 2024-12-20 00:05:38, &q=
uot;Ephraim Yawitz&quot; &lt;<a href=3D"mailto:[email protected]" tar=
get=3D"_blank">[email protected]</a>&gt; wrote:</p><blockquote id=3D"=
m_-7450050050691935762m_-6177578573782233742m_-7802994937922086891isReplyCo=
ntent" style=3D"padding-left:1ex;margin:0px 0px 0px 0.8ex;border-left:1px s=
olid rgb(204,204,204)"><div dir=3D"ltr">I see no one has followed this up, =
but in the meantime I have tried a little example of what I suggested and i=
t didn&#39;t work, i.e., it still used &quot;new&quot; instead of &quot;CUS=
TOM_NEW_T&quot;, so I guess the issue is more complicated than I thought, a=
lthough it might just need a small tweak in the typemap.<br></div><br><div =
class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, Dec 17,=
 2024 at 11:58=E2=80=AFAM Ephraim Yawitz &lt;<a href=3D"mailto:ephraim@exca=
libur.co.il" target=3D"_blank">[email protected]</a>&gt; wrote:<br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div=
>I&#39;m no SWIG expert, but from the little bit I understand, the code for=
 this C++ constructor is created based on the following lines in Lib/csharp=
/csharp.swg in the SWIG installation directory:</div><div><br></div><div>%t=
ypemap(out) SWIGTYPE <br>#ifdef __cplusplus<br>%{ $result =3D new $1_ltype(=
$1); %}<br>#else<br>{<br>=C2=A0 $&amp;1_ltype $1ptr =3D ($&amp;1_ltype) mal=
loc(sizeof($1_ltype));<br>=C2=A0 memmove($1ptr, &amp;$1, sizeof($1_type));<=
br>=C2=A0 $result =3D $1ptr;<br>}<br>#endif</div><div><br></div><div>I&#39;=
m guessing that perhaps creating a %typemap of your own with=20
&quot;CUSTOM_NEW_T&quot; instead of &quot;new&quot; might do the trick.</di=
v><div>Again, I&#39;m just posting this to try to get the attention of the =
guys who really know what they&#39;re doing.<br></div><br><div class=3D"gma=
il_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Dec 16, 2024 at 4:5=
3=E2=80=AFAM Jim &lt;<a href=3D"mailto:[email protected]" target=3D"_bla=
nk">[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail=
_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204=
,204);padding-left:1ex"><div style=3D"line-height:1.7;color:rgb(0,0,0);font=
-size:14px;font-family:Arial"><div style=3D"margin:0px">Hi all,</div><div s=
tyle=3D"margin:0px">=C2=A0 =C2=A0 I am using swig to generate csharp code, =
here is the default implementation of the constructor:</div><div style=3D"m=
argin:0px"><img style=3D"width: 636px; height: 182px;"></div><div style=3D"=
margin:0px">=C2=A0I want change the &#39;new&#39; operator to my custom &#3=
9;CUSTOM_NEW_T&#39;=EF=BC=8Clike &#39;result=3DCUSTOM_NEW_T(Bar)&#39;.=C2=
=A0 =C2=A0I have read the doc for SWIG, but it seems that does not contain =
any relevant content.</div><div style=3D"margin:0px"><br></div><div style=
=3D"margin:0px">=C2=A0Thank you so much, have a nice day!</div></div><img s=
tyle=3D"width: 1px; height: 1px;">_________________________________________=
______<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></div>
</blockquote></div>
</blockquote></div><img style=3D"width: 1px; height: 1px;"></blockquote></d=
iv>
</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></div>

--00000000000092c60f062c1f74a8--


--===============0837363561340716342==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============0837363561340716342==
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

--===============0837363561340716342==--