Is this a bug?
David L. Davis
SpottedTiger at yahoo.com
Fri Apr 14 11:06:10 PDT 2006
In article <e1hvg6$vqj$1 at digitaldaemon.com>, Li Jie says...
>
>In article <e1hqph$q8g$1 at digitaldaemon.com>, David L. Davis says...
>>Li Jie, I don't think it's a bug. Below is some code I pull together that I
>>think is sort of what you were trying to do (just remove the '#' characters from
>>the code, they're only there for holding the formating). But I could be
>>wrong...it has happen before. ;)
>
>Thanks David.
>
>Your code is function call, my code is value-parameter template.
>
>I want to write a "__uuidof" template, like VC++. A simple implemention is:
>
># template __uuidof(T:IUnknown)
># {
># IID __uuidof = {0x00000000, 0x0000, 0x0000, [0xC0, 0x00, 0x00, 0x00, 0x00,
>0x00, 0x00, 0x00, 0x46]};
># }
>
>It's too long.
>
>
>IIDFromStr!("00000000-0000-0000-C000-000000000046");
>and
>{0x00000000, 0x0000, 0x0000, [0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
>0x46]};
>
>I like the first.
>
Li Jie, I'm still not sure I see the problem...but it looks like your code
should compile and work fine in D. (I made a few changes, but nothing major)
# // Li Jie's uui.d - tested WinXP SP1 D v0.154
# // To Compiled: dmd uui.d ole32.lib uuid.lib
# private import std.stdio;
# private import std.c.windows.com;
#
# template HexStrToUbyte(wchar[] ws)
# {
# const ubyte HexStrToUbyte = cast(ubyte)HexStrToUlong!(ws);
# }
#
# template HexStrToUshort(wchar[] ws)
# {
# const ushort HexStrToUshort = cast(ushort)HexStrToUlong!(ws);
# }
#
# template HexStrToUint(wchar[] ws)
# {
# const uint HexStrToUint = cast(uint)HexStrToUlong!(ws);
# }
#
# template HexStrToUlong(wchar[] ws)
# {
# static if (ws.length == 1)
# const ulong HexStrToUlong = HexToUbyte_bug!((ws[0]));
# else
# const ulong HexStrToUlong
# = HexToUbyte_bug!((ws[length-1]))
# + 16 * HexStrToUlong!(ws[0..length-1]);
# }
#
# template HexToUbyte(wchar[] wc)
# {
# static if (wc[0] >= '0' && wc[0] <= '9')
# const ubyte HexToUbyte = wc[0] - '0';
# else static if (wc[0] == 'A' || wc[0] == 'a')
# const ubyte HexToUbyte = 0xa;
# else static if (wc[0] == 'B' || wc[0] == 'b')
# const ubyte HexToUbyte = 0xb;
# else static if (wc[0] == 'C' || wc[0] == 'c')
# const ubyte HexToUbyte = 0xc;
# else static if (wc[0] == 'D' || wc[0] == 'd')
# const ubyte HexToUbyte = 0xd;
# else static if (wc[0] == 'E' || wc[0] == 'e')
# const ubyte HexToUbyte = 0xe;
# else static if (wc[0] == 'F' || wc[0] == 'f')
# const ubyte HexToUbyte = 0xf;
# }
#
# template HexToUbyte_bug(wchar wc)
# {
# static if (wc >= '0' && wc <= '9')
# const ubyte HexToUbyte_bug = wc - '0';
# else static if (wc == 'A' || wc == 'a')
# const ubyte HexToUbyte_bug = 0xa;
# else static if (wc == 'B' || wc == 'b')
# const ubyte HexToUbyte_bug = 0xb;
# else static if (wc == 'C' || wc == 'c')
# const ubyte HexToUbyte_bug = 0xc;
# else static if (wc == 'D' || wc == 'd')
# const ubyte HexToUbyte_bug = 0xd;
# else static if (wc == 'E' || wc == 'e')
# const ubyte HexToUbyte_bug = 0xe;
# else static if (wc == 'F' || wc == 'f')
# const ubyte HexToUbyte_bug = 0xf;
# }
#
# template IIDFromStr(wchar[] ws)
# {
# const IID IIDFromStr = {
# HexStrToUint!(ws[0..8]),
# HexStrToUshort!(ws[9..13]),
# HexStrToUshort!(ws[14..18]),
# [
# HexStrToUbyte!(ws[19..21]),
# HexStrToUbyte!(ws[21..23]),
# HexStrToUbyte!(ws[24..26]),
# HexStrToUbyte!(ws[26..28]),
# HexStrToUbyte!(ws[28..30]),
# HexStrToUbyte!(ws[30..32]),
# HexStrToUbyte!(ws[32..34]),
# HexStrToUbyte!(ws[34..36])
# ]
# };
# }
#
# template __uuidof(T:IUnknown)
# {
# IID __uuidof = IIDFromStr!("00000000-0000-0000-C000-000000000046");
# }
#
# template __uuidof(T:IClassFactory)
# {
# IID __uuidof = IIDFromStr!("00000001-0000-0000-C000-000000000046");
# }
#
# char[] toString(in IID udtGUID)
# {
# wchar[ 39 ] wsGUID = '\0';
# char[] sGUID2 = "";
#
# StringFromGUID2( &udtGUID, wsGUID, 39 );
#
# for ( int ix = 0; ix < wsGUID.length - 1; ix++ )
# {
# //writefln( "wsGUID[ %d ] = %s", ix, wsGUID[ ix ] );
# sGUID2 ~= wsGUID[ ix ];
# }
#
# return sGUID2;
# }
#
# void main()
# {
# IID iu = __uuidof!(IUnknown);
# IID icp = __uuidof!(IClassFactory);
#
# writef("{%08s, ", iu.Data1);
# writef("%04s, ", iu.Data2);
# writef("%04s, ", iu.Data3);
# writefln("%s}", iu.Data4);
#
# writefln("toString(iu)=", toString(iu));
# writefln("IIDFromStr!(\"00000000-0000-0000-C000-000000000046\")=\"%s\"",
# toString(IIDFromStr!("00000000-0000-0000-C000-000000000046")));
# }
Output:
C:\dmd>dmd uui.d ole32.lib uuid.lib
C:\dmd\bin\..\..\dm\bin\link.exe uui,,,ole32.lib+uuid.lib+user32+kernel32/noi;
C:\dmd>uui
{00000000, 0000, 0000, [192,0,0,0,0,0,0,70]}
toString(iu)={00000000-0000-0000-C000-000000000046}
IIDFromStr!("00000000-0000-0000-C000-000000000046")="{00000000-0000-0000-C000-000000000046}"
C:\dmd>
David L.
-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------
MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
More information about the Digitalmars-d
mailing list