Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 25, 2013 How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | On Saturday, 25 May 2013 at 00:02:50 UTC, Heinz wrote:
> #define my_id 'asdf'
string my_id = "asdf";
|
May 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luís Marques | 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luís Marques | 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | > 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 25, 2013 Re: How to translate this C++ preprocessor declaration in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | 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
|
Copyright © 1999-2021 by the D Language Foundation