Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 23, 2006 Constant declarations for mac OSX | ||||
---|---|---|---|---|
| ||||
I'm running GDC on mac OSX. The mac tags events with a four character code which is converted to a UInt32 along the lines of: (s[0] << 24) || (s[1] << 16) || (s[2] << 8) || (s[3]) where s[n] is the ascii charater number. I need to declare: const OSType kEventClassCommand = FOUR_CHAR_CODE("cmds"); Browsing the documentation seems to indicate I need a template but I can't seem to work out how. Can anybody help? Thanks Gareth Baker g.j.baker at dl.ac.uk |
February 23, 2006 Re: Constant declarations for mac OSX | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gareth Baker | Gareth Baker wrote:
> I need to declare:
>
> const OSType kEventClassCommand = FOUR_CHAR_CODE("cmds");
>
> Browsing the documentation seems to indicate I need a template but I can't seem
> to work out how. Can anybody help?
Not what you want (since it isn't automatic), but I used:
# echo "cmds" | hexdump
0000000 636d 6473 0a00
0000005
Which means that the value of your constant is 0x636d6473
--anders
|
February 23, 2006 Re: Constant declarations for mac OSX | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gareth Baker | Gareth Baker wrote: > I'm running GDC on mac OSX. The mac tags events with a four character code which > is converted to a UInt32 along the lines of: > > (s[0] << 24) || (s[1] << 16) || (s[2] << 8) || (s[3]) > > where s[n] is the ascii charater number. > > I need to declare: > > const OSType kEventClassCommand = FOUR_CHAR_CODE("cmds"); template FourCharCode(char[] s) { static assert(s.length == 4); const uint FourCharCode = (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]); } int main() { writefln("%x",FourCharCode!("cmds")); return 0; } /Oskar |
February 23, 2006 Re: Constant declarations for mac OSX | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gareth Baker Attachments: | Gareth Baker schrieb am 2006-02-23:
> I'm running GDC on mac OSX. The mac tags events with a four character code which
> is converted to a
> UInt32 along the lines of:
>
> (s[0] << 24) || (s[1] << 16) || (s[2] << 8) || (s[3])
>
> where s[n] is the ascii charater number.
>
> I need to declare:
>
> const OSType kEventClassCommand = FOUR_CHAR_CODE("cmds");
>
> Browsing the documentation seems to indicate I need a template but I can't seem
> to work out how. Can
> anybody help?
| template FOUR_CHAR_CODE(char[] cmd){
| static if(cmd.length != 4){
| pragma(msg, "require 4 chars");
| static assert(0);
| }else{
| const uint FOUR_CHAR_CODE = (cmd[0] << 24) | (cmd[1] << 16) | (cmd[2] << 8) | cmd[3];
| }
| }
...
| const OSType kEventClassCommand = FOUR_CHAR_CODE!("cmds");
Thomas
|
February 23, 2006 Re: Constant declarations for mac OSX | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | Thanks for the answers. I'll give them a go tonight. Gareth Baker g.j.baker at dl.ac.uk |
February 23, 2006 Re: Constant declarations for mac OSX | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | Thomas Kuehne wrote:
> | template FOUR_CHAR_CODE(char[] cmd){
> | static if(cmd.length != 4){
> | pragma(msg, "require 4 chars");
> | static assert(0);
> | }else{
> | const uint FOUR_CHAR_CODE = (cmd[0] << 24) | (cmd[1] << 16) | (cmd[2] << 8) | cmd[3];
> | }
> | }
Seems to do the trick. (returns 0x636d6473)
--anders
|
Copyright © 1999-2021 by the D Language Foundation