www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to translate this C++ preprocessor declaration in D?

reply "Heinz" <thor587 gmail.com> writes:
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
next sibling parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luismarques gmail.com> writes:
On Saturday, 25 May 2013 at 00:02:50 UTC, Heinz wrote:
 #define my_id 'asdf'
string my_id = "asdf";
May 24 2013
parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luismarques gmail.com> writes:
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:
 #define my_id 'asdf'
Ah, sorry, didn't notice the single quotes.
May 24 2013
parent =?UTF-8?B?Ikx1w61z?= Marques" <luismarques gmail.com> writes:
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
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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?

 Thanks
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. Ali
May 24 2013
parent reply "Heinz" <thor587 gmail.com> writes:
 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.

 Ali
Here'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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/24/2013 05:49 PM, Heinz wrote:
 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.

 Ali
Here'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.
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'; } Ali
May 24 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply "Heinz" <thor587 gmail.com> writes:
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
parent "bearophile" <bearophileHUGS lycos.com> writes:
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