www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do I override a function's name with another one?

reply David L. Davis <SpottedTiger yahoo.com> writes:
I'm almost finished porting over the WinCon.h which stores the NT Console APIs
into wincon.d, but I'm stuck on one thing which I'm hoping someone can point me
into the right direction. In many of these Windows header files there's always
an ASCII version and a Unicode version of an API, that's nomally wrappered in
the format below:

#ifdef UNICODE
#define ReadConsoleInput ReadConsoleInputW
#else
#define ReadConsoleInput ReadConsoleInputA
#endif

Which I used in turn a version( UNICODE ) .. else ... wrapper, but how do I
assign ReadConsoleInput to be ReadConsoleInputW or ReadConsoleInputA? Below I
tried alias, and I've even played around with using typedef, but can't figure it
out...so, thought I'd ask for some help.

Thxs in advance.

# /+
#  ' Function reads data from a console input buffer and 
#  ' removes it from the buffer
#  '
#  +/
# BOOL ReadConsoleInputA
# (
#     /+ IN  +/ HANDLE hConsoleInput,
#     /+ OUT +/ PINPUT_RECORD lpBuffer,
#     /+ IN  +/ DWORD nLength,
#     /+ OUT +/ LPDWORD lpNumberOfEventsRead
# );
# 
# BOOL ReadConsoleInputW
# (
#     /+ IN  +/ HANDLE hConsoleInput,
#     /+ OUT +/ PINPUT_RECORD lpBuffer,
#     /+ IN  +/ DWORD nLength,
#     /+ OUT +/ LPDWORD lpNumberOfEventsRead
# );
# 
# version( UNICODE )
# {
#     alias ReadConsoleInput ReadConsoleInputW;
# }
# else
# {
#     alias ReadConsoleInput ReadConsoleInputA;
# }

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 31 2004
next sibling parent reply parabolis <parabolis softhome.net> writes:
David L. Davis wrote:

 I'm almost finished porting over the WinCon.h which stores the NT Console APIs
 into wincon.d, but I'm stuck on one thing which I'm hoping someone can point me
 into the right direction. In many of these Windows header files there's always
 an ASCII version and a Unicode version of an API, that's nomally wrappered in
 the format below:
 
 #ifdef UNICODE
 #define ReadConsoleInput ReadConsoleInputW
 #else
 #define ReadConsoleInput ReadConsoleInputA
 #endif
 
 Which I used in turn a version( UNICODE ) .. else ... wrapper, but how do I
 assign ReadConsoleInput to be ReadConsoleInputW or ReadConsoleInputA? Below I
 tried alias, and I've even played around with using typedef, but can't figure
it
 out...so, thought I'd ask for some help.
 
 Thxs in advance.

Two points. The first is that I believe UNICODE is always defined for all D programs. The second is that what you are after seems to be accomplished in the phobos lib by ================================================================ version(FIRST) { ReadConsoleInput(..) } else { ReadConsoleInput(..) } ================================================================ I know Thread.d has version(Windows) and version(Linux) examples...
Aug 01 2004
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <ceinot$21or$1 digitaldaemon.com>, parabolis says...
Two points. The first is that I believe UNICODE is always 
defined for all D programs. The second is that what you are 
after seems to be accomplished in the phobos lib by
================================================================
version(FIRST) {
     ReadConsoleInput(..)
}
else {
     ReadConsoleInput(..)
}
================================================================

I know Thread.d has version(Windows) and version(Linux) examples...

parabolis: Thxs for the reply. On point one: Though, I agree that D uses wchar[]/dchar[] UTF-16/UTF-32 Unicode characters internal auto-magically...my point is, that Windows' API does not. That's why there are both an ASCII and a Unicode versions of many of the Windows' API free functions. On point two: There's no need for me to look at Phobos' code for more good examples of using the version(), since the one I have works just fine. My problem is, that I'm trying to use only one free function name for either the ASCII or the UNDICODE version of the function. Currently I can just use the original function names (and I have in my console example code), but it just looks cleaner, and it's more in step with how C/C++ use Windows' API calls...so why can't D? I'm pretty sure there is a way, I just haven't discovered it yet. I'm hoping someone here can say, "Yes" it can be and here how, or "No" it can't currently can't be done in D. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Aug 01 2004
parent reply parabolis <parabolis softhome.net> writes:
David L. Davis wrote:

 In article <ceinot$21or$1 digitaldaemon.com>, parabolis says...
 
Two points. The first is that I believe UNICODE is always 
defined for all D programs. The second is that what you are 
after seems to be accomplished in the phobos lib by
================================================================
version(FIRST) {
    ReadConsoleInput(..)
}
else {
    ReadConsoleInput(..)
}
================================================================

I know Thread.d has version(Windows) and version(Linux) examples...

parabolis: Thxs for the reply. On point one: Though, I agree that D uses wchar[]/dchar[] UTF-16/UTF-32 Unicode characters internal auto-magically...my point is, that Windows' API does not. That's why there are both an ASCII and a Unicode versions of many of the Windows' API free functions.

I am worried you did not include char[] as UTF-8 which is not the same as ASCII. UTF-8 works like ASCII for anything with only 7-bits. However if larger Unicode values are encoded will possibly confuse the Windows -A functions.
 
 On point two: There's no need for me to look at Phobos' code for more good
 examples of using the version(), since the one I have works just fine. My
 problem is, that I'm trying to use only one free function name for either the
 ASCII or the UNDICODE version of the function. Currently I can just use the
 original function names (and I have in my console example code), but it just
 looks cleaner, and it's more in step with how C/C++ use Windows' API calls...so
 why can't D? I'm pretty sure there is a way, I just haven't discovered it yet.
 
 I'm hoping someone here can say, "Yes" it can be and here how, or "No" it can't
 currently can't be done in D.
 

The reason I suggested looking in phobos.std.thread the whole class Thread is written twice. Once for Linux and once for Windows: ================================================================ version(Windows) { class Thread { // ... } } version(Linux) { class Thread { // ... } } ================================================================ Which suggest it is trivial to wrap a function in another as such: ================================================================ version(Unicode) dunno ReadConsoleInput( ... ) { return ReadConsoleInputW( ... ) } else { dunno ReadConsoleInput( ... ) { return ReadConsoleInputA( ... )); } ================================================================
Aug 01 2004
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <cej2mq$268r$1 digitaldaemon.com>, parabolis says...
I am worried you did not include char[] as UTF-8 which is not 
the same as ASCII. UTF-8 works like ASCII for anything with only 
7-bits. However if larger Unicode values are encoded will 
possibly confuse the Windows -A functions.

 On point two: There's no need for me to look at Phobos' code for more good
 examples of using the version(), since the one I have works just fine. My
 problem is, that I'm trying to use only one free function name for either the
 ASCII or the UNDICODE version of the function. Currently I can just use the
 original function names (and I have in my console example code), but it just
 looks cleaner, and it's more in step with how C/C++ use Windows' API calls...so
 why can't D? I'm pretty sure there is a way, I just haven't discovered it yet.
 
 I'm hoping someone here can say, "Yes" it can be and here how, or "No" it can't
 currently can't be done in D.
 

The reason I suggested looking in phobos.std.thread the whole class Thread is written twice. Once for Linux and once for Windows: ================================================================ version(Windows) { class Thread { // ... } } version(Linux) { class Thread { // ... } } ================================================================ Which suggest it is trivial to wrap a function in another as such: ================================================================ version(Unicode) dunno ReadConsoleInput( ... ) { return ReadConsoleInputW( ... ) } else { dunno ReadConsoleInput( ... ) { return ReadConsoleInputA( ... )); } ================================================================

parabolis: I stand corrected, you're right. I should have said Windows' ANSI or UNICODE API functions. ASCII (7-Bit) / ASCII Extended (8-Bit) Chart http://www.asciitable.com/ ANSI (8-Bit) Chart http://h18009.www1.hp.com/fortran/docs/vf-html/lref/pg9ansi2.htm And about point two...sorry, at first I misunderstood your point, but I'm clear on that it now. :) ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Aug 01 2004
parent parabolis <parabolis softhome.net> writes:
David L. Davis wrote:

 In article <cej2mq$268r$1 digitaldaemon.com>, parabolis says...
 
I am worried you did not include char[] as UTF-8 which is not 
the same as ASCII. UTF-8 works like ASCII for anything with only 
7-bits. However if larger Unicode values are encoded will 
possibly confuse the Windows -A functions.


On point two: There's no need for me to look at Phobos' code for more good
examples of using the version(), since the one I have works just fine. My
problem is, that I'm trying to use only one free function name for either the
ASCII or the UNDICODE version of the function. Currently I can just use the
original function names (and I have in my console example code), but it just
looks cleaner, and it's more in step with how C/C++ use Windows' API calls...so
why can't D? I'm pretty sure there is a way, I just haven't discovered it yet.

I'm hoping someone here can say, "Yes" it can be and here how, or "No" it can't
currently can't be done in D.

The reason I suggested looking in phobos.std.thread the whole class Thread is written twice. Once for Linux and once for Windows: ================================================================ version(Windows) { class Thread { // ... } } version(Linux) { class Thread { // ... } } ================================================================ Which suggest it is trivial to wrap a function in another as such: ================================================================ version(Unicode) dunno ReadConsoleInput( ... ) { return ReadConsoleInputW( ... ) } else { dunno ReadConsoleInput( ... ) { return ReadConsoleInputA( ... )); } ================================================================

parabolis: I stand corrected, you're right. I should have said Windows' ANSI or UNICODE API functions. ASCII (7-Bit) / ASCII Extended (8-Bit) Chart http://www.asciitable.com/ ANSI (8-Bit) Chart http://h18009.www1.hp.com/fortran/docs/vf-html/lref/pg9ansi2.htm And about point two...sorry, at first I misunderstood your point, but I'm clear on that it now. :)

No problem. I noticed you got a much better answer from someone else anyway :)
 
 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"

Aug 01 2004
prev sibling next sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
The standard so far is to leave the A and W version visible without defining
ReadConsoleInput and let the user decide which one to use. Since the W
version takes UTF16 typically the user code looks something like
 char[] str;
 foo = BarA(std.file.toMBSz(str)); // utf8 to local code page
or
 foo = BarW(std.utf.toUTF16z(str)); // utf8 to utf16
For more examples see std.file

-Ben

David L. Davis wrote:

 I'm almost finished porting over the WinCon.h which stores the NT Console
 APIs into wincon.d, but I'm stuck on one thing which I'm hoping someone
 can point me into the right direction. In many of these Windows header
 files there's always an ASCII version and a Unicode version of an API,
 that's nomally wrappered in the format below:
 
 #ifdef UNICODE
 #define ReadConsoleInput ReadConsoleInputW
 #else
 #define ReadConsoleInput ReadConsoleInputA
 #endif
 
 Which I used in turn a version( UNICODE ) .. else ... wrapper, but how do
 I assign ReadConsoleInput to be ReadConsoleInputW or ReadConsoleInputA?
 Below I tried alias, and I've even played around with using typedef, but
 can't figure it out...so, thought I'd ask for some help.
 
 Thxs in advance.
 
 # /+
 #  ' Function reads data from a console input buffer and
 #  ' removes it from the buffer
 #  '
 #  +/
 # BOOL ReadConsoleInputA
 # (
 #     /+ IN  +/ HANDLE hConsoleInput,
 #     /+ OUT +/ PINPUT_RECORD lpBuffer,
 #     /+ IN  +/ DWORD nLength,
 #     /+ OUT +/ LPDWORD lpNumberOfEventsRead
 # );
 # 
 # BOOL ReadConsoleInputW
 # (
 #     /+ IN  +/ HANDLE hConsoleInput,
 #     /+ OUT +/ PINPUT_RECORD lpBuffer,
 #     /+ IN  +/ DWORD nLength,
 #     /+ OUT +/ LPDWORD lpNumberOfEventsRead
 # );
 # 
 # version( UNICODE )
 # {
 #     alias ReadConsoleInput ReadConsoleInputW;
 # }
 # else
 # {
 #     alias ReadConsoleInput ReadConsoleInputA;
 # }
 
 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"

Aug 01 2004
parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <ceipff$2288$1 digitaldaemon.com>, Ben Hinkle says...
The standard so far is to leave the A and W version visible without defining
ReadConsoleInput and let the user decide which one to use. Since the W
version takes UTF16 typically the user code looks something like
 char[] str;
 foo = BarA(std.file.toMBSz(str)); // utf8 to local code page
or
 foo = BarW(std.utf.toUTF16z(str)); // utf8 to utf16
For more examples see std.file

-Ben

Ben Hinkle: Thxs for the reply and for pointing the std.utf.toUTF16z(str) call the converts UTF-8 to UTF-16. Yep, the wincon.d that I converted has both the A and W version of all the API functions, and if D's standard is to not mapped them to a common name, then that's want I'll stick with. But still, is there a way to alias one function name for another function name? It might be nice to have this option for other things I'm doing in D code. Thanks Again, David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Aug 01 2004
prev sibling parent reply Kazuhiro Inaba <Kazuhiro_member pathlink.com> writes:
# version( UNICODE )
# {
#     alias ReadConsoleInput ReadConsoleInputW;
# }
# else
# {
#     alias ReadConsoleInput ReadConsoleInputA;
# }

Maybe this should be: version( UNICODE ) { alias ReadConsoleInputW ReadConsoleInput; } else { alias ReadConsoleInputA ReadConsoleInput; } In alias declarations, the last symbol is newly defined. (same order as typedefs, not #define) -- Kaz.
Aug 01 2004
parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <ceivpk$24qo$1 digitaldaemon.com>, Kazuhiro Inaba says...
# version( UNICODE )
# {
#     alias ReadConsoleInput ReadConsoleInputW;
# }
# else
# {
#     alias ReadConsoleInput ReadConsoleInputA;
# }

Maybe this should be: version( UNICODE ) { alias ReadConsoleInputW ReadConsoleInput; } else { alias ReadConsoleInputA ReadConsoleInput; } In alias declarations, the last symbol is newly defined. (same order as typedefs, not #define) -- Kaz.

Kazuhiro Inaba: DOH! Thxs for point it out, darn it's normally something simple like this that always gets me stuck. I'm not really sure how I could've overlooked this, except that maybe playing around in "C" for the past three days converting "C" header files (which wincon.h to .d is the first one ready) has clouded my brain a bit. I'll put out a link to wincon.d with two examples sometime soon, just in case someone might find it useful. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Aug 01 2004