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

Ephraim Yawitz <[email protected]> Sun, 22 Dec 2024 11:22:46 +0200
Newsgroups gmane.comp.programming.swig
Message-ID <CAP-4f0J-V=6qDz-=PydQx+Y4BpPQq3sdhr+qrfCsxbZBEY=sEg@mail.gmail.com>
--===============4044418284111100758==
Content-Type: multipart/alternative; boundary="000000000000effce40629d86c54"

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

---------- 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();".  So=
 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 use=
d
> "new" instead of "CUSTOM_NEW_T", so I guess the issue is more complicated
> 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@excalibu=
r.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 who
>> really know what they're doing.
>>
>> On Mon, Dec 16, 2024 at 4:53=E2=80=AFAM Jim <[email protected]> wrote=
:
>>
>>> 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=8Cl=
ike
>>> 'result=3DCUSTOM_NEW_T(Bar)'.   I have read the doc for SWIG, but it se=
ems
>>> 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
>>>
>>

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

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote gmail_quote_container"><=
div dir=3D"ltr" class=3D"gmail_attr">---------- Forwarded message ---------=
<br>From: <b class=3D"gmail_sendername" dir=3D"auto">Ephraim Yawitz</b> <sp=
an dir=3D"auto">&lt;<a href=3D"mailto:[email protected]">ephraim@exca=
libur.co.il</a>&gt;</span><br>Date: Sun, Dec 22, 2024 at 11:22=E2=80=AFAM<b=
r>Subject: Re: Re: [Swig-user] how to map &#39;new&#39; to &#39;CUSTOM_NEW_=
T&#39;<br>To: Jim &lt;<a href=3D"mailto:[email protected]">guyiming2011@=
126.com</a>&gt;<br></div><br><br><div dir=3D"ltr">I&#39;m very surprised th=
at this can&#39;t be done with typemaps. Did anyone else on the list confir=
m this?<br></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"g=
mail_attr">On Fri, Dec 20, 2024 at 12:05=E2=80=AFPM Jim &lt;<a href=3D"mail=
to:[email protected]" target=3D"_blank">[email protected]</a>&gt; wro=
te:<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=
 id=3D"m_-6177578573782233742m_-7802994937922086891spnEditorContent"><p sty=
le=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 clon=
e the git repository and 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_attribute =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</spa=
n></p><p style=3D"margin:0px"><span style=3D"color:rgb(141,148,64)">if (!na=
tive_function_flag) {</span></p><p style=3D"margin:0px"><br></p><p style=3D=
"margin:0px"><span style=3D"white-space:normal;color:rgb(141,148,64)"><span=
 style=3D"color:rgb(141,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;color:rgb(141,148,64)"><span style=3D"color:rgb(141,148,=
64);white-space:pre-wrap">	</span>String* actioncode =3D emit_action(n);</s=
pan></p><p style=3D"margin:0px"><br></p><p style=3D"margin:0px"><span style=
=3D"white-space:normal;color:rgb(141,148,64)"><span style=3D"color:rgb(141,=
148,64);white-space:pre-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)"><span style=3D"color:rgb(141,148,64);white-space:pre-wrap"=
>	</span>if ((tm =3D Swig_typemap_lookup_out(&quot;out&quot;, n, Swig_cresu=
lt_name(), f, actioncode))) {</span></p><p style=3D"margin:0px"><span style=
=3D"white-space:normal;color:rgb(141,148,64)"><span style=3D"color:rgb(141,=
148,64);white-space:pre-wrap">		</span>canThrow(n, &quot;out&quot;, n);</sp=
an></p><p style=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(=
141,148,64)"><span style=3D"color:rgb(141,148,64);white-space:pre-wrap">		<=
/span>Replaceall(tm, &quot;$result&quot;, &quot;jresult&quot;);</span></p><=
p style=3D"margin:0px"><br></p><p style=3D"margin:0px"><span style=3D"white=
-space:normal;color:rgb(141,148,64)"><span style=3D"color:rgb(141,148,64);w=
hite-space:pre-wrap">		</span>if (GetFlag(n, &quot;feature:new&quot;))</spa=
n></p><p style=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(1=
41,148,64)"><span style=3D"color:rgb(141,148,64);white-space:pre-wrap">			<=
/span>Replaceall(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)"><s=
pan style=3D"color:rgb(141,148,64);white-space:pre-wrap">		</span>else</spa=
n></p><p style=3D"margin:0px"><span style=3D"white-space:normal;color:rgb(1=
41,148,64)"><span style=3D"color:rgb(141,148,64);white-space:pre-wrap">			<=
/span>Replaceall(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;re=
sult =3D (Bar *)new Bar();&quot;.=C2=A0 So I digged the emit_action but hav=
e no idea about how to modify it.</p><p style=3D"margin:0px"><br></p><p sty=
le=3D"margin:0px"><br></p><p style=3D"margin:0px"><br></p></div><div></div>=
<div id=3D"m_-6177578573782233742m_-7802994937922086891divNeteaseMailCard">=
</div><p style=3D"margin:0px"><br></p><p>At 2024-12-20 00:05:38, &quot;Ephr=
aim Yawitz&quot; &lt;<a href=3D"mailto:[email protected]" target=3D"_=
blank">[email protected]</a>&gt; wrote:</p><blockquote id=3D"m_-61775=
78573782233742m_-7802994937922086891isReplyContent" style=3D"padding-left:1=
ex;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)"><div di=
r=3D"ltr">I see no one has followed this up, but in the meantime I have tri=
ed a little example of what I suggested and it didn&#39;t work, i.e., it st=
ill used &quot;new&quot; instead of &quot;CUSTOM_NEW_T&quot;, so I guess th=
e issue is more complicated than I thought, although it might just need a s=
mall 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 Ephr=
aim Yawitz &lt;<a href=3D"mailto:[email protected]" target=3D"_blank"=
>[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 dir=3D"ltr"><div>I&#39;m no SWIG expert, but f=
rom the little bit I understand, the code for this C++ constructor is creat=
ed based on the following lines in Lib/csharp/csharp.swg in the SWIG instal=
lation directory:</div><div><br></div><div>%typemap(out) SWIGTYPE <br>#ifde=
f __cplusplus<br>%{ $result =3D new $1_ltype($1); %}<br>#else<br>{<br>=C2=
=A0 $&amp;1_ltype $1ptr =3D ($&amp;1_ltype) malloc(sizeof($1_ltype));<br>=
=C2=A0 memmove($1ptr, &amp;$1, sizeof($1_type));<br>=C2=A0 $result =3D $1pt=
r;<br>}<br>#endif</div><div><br></div><div>I&#39;m guessing that perhaps cr=
eating 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"marg=
in:0px">=C2=A0I want change the &#39;new&#39; operator to my custom &#39;CU=
STOM_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"ma=
rgin:0px">=C2=A0Thank you so much, have a nice day!</div></div><img style=
=3D"width:1px;height:1px" src=3D"https://count.mail.163.com/beacon/webmail.=
gif?type=3Dwebmail_mailtrace&amp;guid=3Dpre_1c51cbb92865f663cb929be752099ce=
a">_______________________________________________<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" src=3D"https://count=
.mail.163.com/beacon/webmail.gif?type=3Dwebmail_mailtrace&amp;guid=3Dpre_26=
39352238bc87af42444a6b0c112d90"></blockquote></div>
</div></div>

--000000000000effce40629d86c54--


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


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

--===============4044418284111100758==--