Re: managed C++ StringBuilder & wchar_t* by Lloyd
Lloyd
Mon Jun 27 20:43:04 CDT 2005
This is a multi-part message in MIME format.
------=_NextPart_000_0130_01C57BD6.8BCCA6B0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
just replying myself.. one of the macro could be optimized but hell! =
it's nicer like that...
and it does involves some copy....
#define STRING_TO_POINTER_UNICODE(str, ptr) \
pin_ptr<wchar_t> ptr =3D nullptr; \
if(str) {\
array<wchar_t>^ __private__##str__##ptr__ =3D gcnew =
array<wchar_t>(str->Length); \
str->CopyTo(0, __private__##str__##ptr__, 0, str->Length); \
ptr =3D &__private__##str__##ptr__[0]; \
}
#define STRING_TO_POINTER_ANSI(str, ptr) \
pin_ptr<unsigned char> ptr =3D nullptr; \
if(str) {\
array<unsigned char>^ __private__##str__##ptr__ =3D =
Encoding::ASCII->GetBytes(str);\
ptr =3D &__private__##str__##ptr__[0];\
}
#define STRING_BUILDER_TO_POINTER_UNICODE(sb, ptr)\
pin_ptr<wchar_t> ptr =3D nullptr;\
if(sb) {\
array<wchar_t>^ __private__##str__##ptr__ =3D gcnew =
array<wchar_t>(sb->Capacity);\
sb->CopyTo(0, __private__##str__##ptr__, 0, sb->Length);\
ptr =3D &__private__##str__##ptr__[0];\
}
#define STRING_BUILDER_SET_TEXT_UNICODE(sb, ptr)\
if(sb) {\
sb->Length =3D 0;\
sb->Append(gcnew String((wchar_t*)ptr));\
}
#define STRING_BUILDER_TO_POINTER_ANSI(sb, ptr)\
pin_ptr<unsigned char> ptr =3D nullptr;\
if(sb) {\
array<unsigned char>^ __private__##str__##ptr__ =3D gcnew =
array<unsigned char>(sb->Capacity);\
=
Encoding::ASCII->GetBytes(sb->ToString())->CopyTo(__private__##str__##ptr=
__, 0);\
ptr =3D &__private__##str__##ptr__[0];\
}
#define STRING_BUILDER_SET_TEXT_ANSI(sb, ptr)\
if(sb) {\
sb->Length =3D 0;\
sb->Append(gcnew String((char*) ptr));\
}
"Lloyd Dupont" <net.galador@ld> wrote in message =
news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl...
> from some ManagedC++ code I'm writing I would like to call a function =
like=20
> that
> void myfunction(wchar_t*, unsigned);
>=20
> I would like to avoid attribute, particularly as I import the header, =
there=20
> are plenty of custom types and function and it's much more easy to =
just call=20
> the function (otherwise I rather code in C#, in fact...)
>=20
>=20
> I though I could just
>=20
> wchar_t* buf =3D alloc(n * sizeof(wchar_t))
> for(...)
> {
> // copy char 1 by 1
> }
>=20
> bur certainly there is a faster version?
> involving some memcpy, maybe internally to the StringBuilder?=20
>=20
>
------=_NextPart_000_0130_01C57BD6.8BCCA6B0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2668" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>just replying myself.. one of the macro =
could be=20
optimized but hell! it's nicer like that...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and it does involves some =
copy....</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#0000ff size=3D2>
<P>#define</FONT><FONT size=3D2> STRING_TO_POINTER_UNICODE(str, ptr)=20
\<BR></FONT><FONT color=3D#0000ff size=3D2> =
pin_ptr</FONT><FONT=20
size=3D2><</FONT><FONT color=3D#0000ff size=3D2>wchar_t</FONT><FONT =
size=3D2>> ptr=20
=3D </FONT><FONT color=3D#0000ff size=3D2>nullptr</FONT><FONT size=3D2>; =
\<BR> </FONT><FONT color=3D#0000ff =
size=3D2>if</FONT><FONT=20
size=3D2>(str) {\<BR> </FONT><FONT=20
color=3D#0000ff size=3D2>array</FONT><FONT size=3D2><</FONT><FONT =
color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT size=3D2>>^ __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__ =3D </FONT><FONT color=3D#0000ff=20
size=3D2>gcnew</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2><</FONT><FONT color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT size=3D2>>(str->Length); =
\<BR> =20
str->CopyTo(0, __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__, 0, str->Length); =
\<BR> =20
ptr =3D &__private__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__[0]; \<BR> =
}<BR></FONT><FONT=20
color=3D#0000ff size=3D2>#define</FONT><FONT size=3D2> =
STRING_TO_POINTER_ANSI(str,=20
ptr) \<BR></FONT><FONT color=3D#0000ff size=3D2> =20
pin_ptr</FONT><FONT size=3D2><</FONT><FONT color=3D#0000ff=20
size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>char</FONT><FONT size=3D2>> ptr =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>nullptr</FONT><FONT size=3D2>; \<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2> if</FONT><FONT size=3D2>(str) =
{\<BR></FONT><FONT=20
color=3D#0000ff size=3D2> =
array</FONT><FONT=20
size=3D2><</FONT><FONT color=3D#0000ff size=3D2>unsigned</FONT><FONT =
size=3D2>=20
</FONT><FONT color=3D#0000ff size=3D2>char</FONT><FONT size=3D2>>^=20
__private__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT=20
size=3D2>#str__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT =
size=3D2>#ptr__ =3D=20
Encoding::ASCII->GetBytes(str);\<BR> =
ptr=20
=3D &__private__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT=20
size=3D2>#str__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT=20
size=3D2>#ptr__[0];\<BR>}<BR></FONT><FONT color=3D#0000ff =
size=3D2>#define</FONT><FONT=20
size=3D2> STRING_BUILDER_TO_POINTER_UNICODE(sb, ptr)\<BR></FONT><FONT=20
color=3D#0000ff size=3D2> pin_ptr</FONT><FONT=20
size=3D2><</FONT><FONT color=3D#0000ff size=3D2>wchar_t</FONT><FONT =
size=3D2>> ptr=20
=3D </FONT><FONT color=3D#0000ff size=3D2>nullptr</FONT><FONT=20
size=3D2>;\<BR></FONT><FONT color=3D#0000ff size=3D2> =
if</FONT><FONT=20
size=3D2>(sb) {\<BR></FONT><FONT color=3D#0000ff =
size=3D2> =20
array</FONT><FONT size=3D2><</FONT><FONT =
color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT size=3D2>>^ __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__ =3D </FONT><FONT color=3D#0000ff=20
size=3D2>gcnew</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2><</FONT><FONT color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT =
size=3D2>>(sb->Capacity);\<BR> =20
sb->CopyTo(0, __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__, 0, =
sb->Length);\<BR> =20
ptr =3D &__private__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__[0];\<BR>}<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>#define</FONT><FONT size=3D2> =
STRING_BUILDER_SET_TEXT_UNICODE(sb,=20
ptr)\<BR></FONT><FONT color=3D#0000ff size=3D2> =
if</FONT><FONT=20
size=3D2>(sb) {\<BR> sb->Length =3D =
0;\<BR> =20
sb->Append(</FONT><FONT color=3D#0000ff size=3D2>gcnew</FONT><FONT =
size=3D2>=20
String((</FONT><FONT color=3D#0000ff size=3D2>wchar_t</FONT><FONT=20
size=3D2>*)ptr));\<BR>}<BR></FONT><FONT color=3D#0000ff =
size=3D2>#define</FONT><FONT=20
size=3D2> STRING_BUILDER_TO_POINTER_ANSI(sb, ptr)\<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2> pin_ptr</FONT><FONT =
size=3D2><</FONT><FONT=20
color=3D#0000ff size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>char</FONT><FONT size=3D2>> ptr =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>nullptr</FONT><FONT size=3D2>;\<BR></FONT><FONT color=3D#0000ff =
size=3D2> if</FONT><FONT size=3D2>(sb) =
{\<BR></FONT><FONT=20
color=3D#0000ff size=3D2><FONT size=3D2><FONT face=3DArial=20
color=3D#000000> <FONT face=3D"Times New Roman"=20
color=3D#0000ff> </FONT></FONT></FONT><FONT =
color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2><</FONT><FONT color=3D#0000ff=20
size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>char</FONT><FONT size=3D2>>^ __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__ =3D </FONT><FONT color=3D#0000ff=20
size=3D2>gcnew</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2><</FONT><FONT color=3D#0000ff=20
size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>char</FONT><FONT=20
size=3D2>>(sb->Capacity);\<BR> &=
nbsp; =20
Encoding::ASCII->GetBytes(sb->ToString())->CopyTo(__private__</F=
ONT><FONT=20
color=3D#0000ff size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__, 0);\<BR> =20
</FONT></FONT><FONT size=3D2>ptr =3D =
&__private__</FONT><FONT=20
color=3D#0000ff size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__[0];\<BR>}<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>#define</FONT><FONT size=3D2> STRING_BUILDER_SET_TEXT_ANSI(sb,=20
ptr)\<BR></FONT><FONT color=3D#0000ff size=3D2> =
if</FONT><FONT=20
size=3D2>(sb) {\<BR> sb->Length =
=3D=20
0;\<BR> sb->Append(</FONT><FONT=20
color=3D#0000ff size=3D2>gcnew</FONT><FONT size=3D2> =
String((</FONT><FONT=20
color=3D#0000ff size=3D2>char</FONT><FONT size=3D2>*) =
ptr));\<BR> =20
}<BR></P></FONT><FONT face=3DArial size=3D2></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>"Lloyd Dupont" <</FONT><A=20
href=3D"mailto:net.galador@ld"><FONT face=3DArial=20
size=3D2>net.galador@ld</FONT></A><FONT face=3DArial size=3D2>> wrote =
in message=20
</FONT><A href=3D"news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl"><FONT =
face=3DArial=20
size=3D2>news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl</FONT></A><FONT =
face=3DArial=20
size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>> from some =
ManagedC++ code=20
I'm writing I would like to call a function like <BR>> that<BR>> =
void=20
myfunction(wchar_t*, unsigned);<BR>> <BR>> I would like to avoid=20
attribute, particularly as I import the header, there <BR>> are =
plenty of=20
custom types and function and it's much more easy to just call <BR>> =
the=20
function (otherwise I rather code in C#, in fact...)<BR>> <BR>> =
<BR>> I=20
though I could just<BR>> <BR>> wchar_t* buf =3D alloc(n *=20
sizeof(wchar_t))<BR>> for(...)<BR>> {<BR>> // =
copy=20
char 1 by 1<BR>> }<BR>> <BR>> bur certainly there is a faster=20
version?<BR>> involving some memcpy, maybe internally to the =
StringBuilder?=20
<BR>> <BR>></FONT></BODY></HTML>
------=_NextPart_000_0130_01C57BD6.8BCCA6B0--