digitalmars.D.learn - How to translate this C++ preprocessor declaration in D?
- Heinz (6/6) May 24 2013 Hi,
- =?UTF-8?B?Ikx1w61z?= Marques" (2/3) May 24 2013 string my_id = "asdf";
- =?UTF-8?B?Ikx1w61z?= Marques" (2/4) May 24 2013 Ah, sorry, didn't notice the single quotes.
- =?UTF-8?B?Ikx1w61z?= Marques" (5/5) May 24 2013 I remember that there was a smarter way to do this, but you can
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/11) May 24 2013 If it really has single quotes then it is a multi-character literal,
- Heinz (9/14) May 24 2013 Here're some examples:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (44/57) May 24 2013 I took liberty in renaming the last one of those macros. ;)
- bearophile (4/6) May 24 2013 Also, D defines version(BigEndian) and version(LittleEndian).
- Heinz (23/23) May 24 2013 Guys, i also did a templated version that yields the same output
- bearophile (11/16) May 25 2013 Generally it's more descriptive to use enum (or even a CT
Hi, I'm porting a C++ header (i'm not a C++ programmer) and came with the following declaration: #define my_id 'asdf' How does this translate to D? Thanks
May 24 2013
On Saturday, 25 May 2013 at 00:02:50 UTC, Heinz wrote:#define my_id 'asdf'string my_id = "asdf";
May 24 2013
On Saturday, 25 May 2013 at 00:07:02 UTC, Luís Marques wrote:On Saturday, 25 May 2013 at 00:02:50 UTC, Heinz wrote:Ah, sorry, didn't notice the single quotes.#define my_id 'asdf'
May 24 2013
I remember that there was a smarter way to do this, but you can do it manually. Something like: immutable long my_id = 'a' << 24 + 'b' << 16 + 'c' << 8 + 'd'; Or you can create a CTFE function to do this from a string, which should be nicer, if you don't find an existing utility for this.
May 24 2013
On 05/24/2013 05:02 PM, Heinz wrote:Hi, I'm porting a C++ header (i'm not a C++ programmer) and came with the following declaration: #define my_id 'asdf' How does this translate to D? ThanksIf it really has single quotes then it is a multi-character literal, value of which happens to be implementation-dependent. What is actually in place of asdf there? May be we can guess the intent better. Ali
May 24 2013
If it really has single quotes then it is a multi-character literal, value of which happens to be implementation-dependent. What is actually in place of asdf there? May be we can guess the intent better. AliHere're some examples: #define kPIHostBlendModeSignature '8BIM' #define PIPowerPCCarbonCodeProperty 'ppcb' #define PIPowerPCMachOCodeProperty 'mach' #define PICodeMacIntel32Property 'mi32' #define PICodeMacIntel32Property 'mi64' I'm porting the Photoshop SDK (CS6) to D. I already compiled a hybrid plugin with DMC and DMD (it works) but now i want to make native D plugins.
May 24 2013
On 05/24/2013 05:49 PM, Heinz wrote:I took liberty in renaming the last one of those macros. ;) If the multi-character literals are evaluated big-endian as Luís Marques and I guess, then you can use the following code: import std.stdio; uint makeId(string s) { uint result = 0; foreach (c; s) { result <<= 8; result += c; } return result; } enum kPIHostBlendModeSignature = makeId("8BIM"); enum PIPowerPCCarbonCodeProperty = makeId("ppcb"); enum PIPowerPCMachOCodeProperty = makeId("mach"); enum PICodeMacIntel32Property = makeId("mi32"); enum PICodeMacIntel64Property = makeId("mi64"); void main() { writeln(kPIHostBlendModeSignature); writeln(PIPowerPCCarbonCodeProperty); writeln(PIPowerPCMachOCodeProperty); writeln(PICodeMacIntel32Property); writeln(PICodeMacIntel64Property); } It produces the same output as the following C++ program on my system: #include <iostream> using namespace std; #define kPIHostBlendModeSignature '8BIM' #define PIPowerPCCarbonCodeProperty 'ppcb' #define PIPowerPCMachOCodeProperty 'mach' #define PICodeMacIntel32Property 'mi32' #define PICodeMacIntel64Property 'mi64' int main() { cout << kPIHostBlendModeSignature << '\n' << PIPowerPCCarbonCodeProperty << '\n' << PIPowerPCMachOCodeProperty << '\n' << PICodeMacIntel32Property << '\n' << PICodeMacIntel64Property << '\n'; } AliIf it really has single quotes then it is a multi-character literal, value of which happens to be implementation-dependent. What is actually in place of asdf there? May be we can guess the intent better. AliHere're some examples: #define kPIHostBlendModeSignature '8BIM' #define PIPowerPCCarbonCodeProperty 'ppcb' #define PIPowerPCMachOCodeProperty 'mach' #define PICodeMacIntel32Property 'mi32' #define PICodeMacIntel32Property 'mi64' I'm porting the Photoshop SDK (CS6) to D. I already compiled a hybrid plugin with DMC and DMD (it works) but now i want to make native D plugins.
May 24 2013
Ali Çehreli:If the multi-character literals are evaluated big-endian as Luís Marques and I guess, then you can use the following code:Also, D defines version(BigEndian) and version(LittleEndian). Bye, bearophile
May 24 2013
Guys, i also did a templated version that yields the same output as the C++ program: //////////////////////////////////////////////////////////////////////// import std.stdio; template makeId(char[4] id) { const makeId = id[0] << 24 | id[1] << 16 | id[2] << 8 | id[3]; } const kPIHostBlendModeSignature = makeId!("8BIM"); const PIPowerPCCarbonCodeProperty = makeId!("ppcb"); const PIPowerPCMachOCodeProperty = makeId!("mach"); const PICodeMacIntel32Property = makeId!("mi32"); const PICodeMacIntel64Property = makeId!("mi64"); void main() { writefln(kPIHostBlendModeSignature); writefln(PIPowerPCCarbonCodeProperty); writefln(PIPowerPCMachOCodeProperty); writefln(PICodeMacIntel32Property); writefln(PICodeMacIntel64Property); } //////////////////////////////////////////////////////////////////////// Thanks for your help.
May 24 2013
Heinz:template makeId(char[4] id) { const makeId = id[0] << 24 | id[1] << 16 | id[2] << 8 | id[3]; } const kPIHostBlendModeSignature = makeId!("8BIM");Generally it's more descriptive to use enum (or even a CT function): template makeId(char[4] id) { enum makeId = (id[0] << 24) | (id[1] << 16) | (id[2] << 8) | id[3]; } enum PIPowerPCCarbonCodeProperty = makeId!"ppcb"; Bye, bearophile
May 25 2013