www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Constant declarations for mac OSX

reply Gareth Baker <Gareth_member pathlink.com> writes:
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
Feb 23 2006
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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: 0000000 636d 6473 0a00 0000005 Which means that the value of your constant is 0x636d6473 --anders
Feb 23 2006
prev sibling next sibling parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
Feb 23 2006
prev sibling parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFD/e/73w+/yD4P9tIRAmxyAJ96Kr/16xTztRgsQryIl+AESM/K5wCbBLqB 5PCfgZCLXC0RS4rXVw4IGSg= =2eJL -----END PGP SIGNATURE-----
Feb 23 2006
next sibling parent g.j.baker dl.ac.uk writes:
Thanks for the answers. I'll give them a go tonight.

Gareth Baker
g.j.baker at dl.ac.uk
Feb 23 2006
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Feb 23 2006