How to translate this C++ preprocessor declaration in D?

Ali Çehreli acehreli at yahoo.com
Fri May 24 18:15:16 PDT 2013


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


More information about the Digitalmars-d-learn mailing list