Thread overview
Constant declarations for mac OSX
Feb 23, 2006
Gareth Baker
Feb 23, 2006
Oskar Linde
Feb 23, 2006
Thomas Kuehne
Feb 23, 2006
g.j.baker
February 23, 2006
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
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
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
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
Thanks for the answers. I'll give them a go tonight.

Gareth Baker
g.j.baker at dl.ac.uk
February 23, 2006
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