0

Is there a way to conditionally compile part of Delphi code in Delphi only (but not in C++Builder)?

Rationale:

OmniThreadLibrary contains overloaded properties in type TOmniValue.

property AsArrayItem[idx: integer]: TOmniValue 
  read GetAsArrayItem write SetAsArrayItem; default;
property AsArrayItem[const name: string]: TOmniValue 
  read GetAsArrayItem write SetAsArrayItem; default;

This concept does not translate to C++. (IOW, if you try to compile this code with C++Builder, compilation fails.)

One way to solve the problem is to rename one of the properties and remove the default specifier from it.

property AsArrayItem[idx: integer]: TOmniValue 
  read GetAsArrayItem write SetAsArrayItem; default;
property AsArrayItemByName[const name: string]: TOmniValue 
  read GetAsArrayItemByName write SetAsArrayItemByName;

This way, however, is not backwards compatible and users of the library would have to change the source code that was using the [string] version of this default property.

I would like to keep the code backwards compatible and make it work in C++Builder. My idea was to do something like this

property AsArrayItem[idx: integer]: TOmniValue 
  read GetAsArrayItem write SetAsArrayItem; default;
{$IF CompilingInDelphi}
property AsArrayItem[const name: string]: TOmniValue 
  read GetAsArrayItem write SetAsArrayItem; default;
{$ELSEIF CompilingInC++Builder}
property AsArrayItemByName[const name: string]: TOmniValue 
  read GetAsArrayItemByName write SetAsArrayItemByName;
{$IFEND}

but it looks like there is no useful symbol that could be used for such conditional compilation.

gabr
  • 26,580
  • 9
  • 75
  • 141
  • It looks as if you are out of luck, indeed. I guess you'll have to $DEFINE a symbol manually. There seems to be no way to detect if the Delphi compiler is compiling for Delphi or for C++Builder. – Rudy Velthuis Jun 22 '16 at 07:53
  • 1
    But perhaps you can $HPPEMIT C++ code (to the .hpp file) that ignores the faulty declarations. – Rudy Velthuis Jun 22 '16 at 07:55
  • Indeed, using an 'external' symbol (something like CppBuilderCompatibleMode) is probably a way to go. @RudyVelthuis, can you add this as an answer? – gabr Jun 22 '16 at 08:05
  • $HPPEMIT is better solution, IMO – Free Consulting Jun 22 '16 at 08:34
  • Then show me how to do that using $HPPEMIT. I have no idea. – gabr Jun 22 '16 at 08:36
  • I tried `$HPPEMIT '#define AsArrayItem AsArrayItemName'}` before the second `AsArrayItem` declaration, and an equivalent #undef after it, but the #define isn't placed there in the .hpp file. It comes much earlier, so it has no effect: the symbol is defined and then immediately undefined again. – Rudy Velthuis Jun 22 '16 at 08:55
  • 2
    [This SO answer](http://stackoverflow.com/a/2631343/95954) seems to give you a solution: "the BCB define is only set when Delphi is compiling with support for C++Builder (using the -J switches)". I tried it, and it seems to work, indeed. – Rudy Velthuis Jun 22 '16 at 09:59
  • So actually this is a duplicate, eh. I'll delete it then. Thanks! – gabr Jun 22 '16 at 10:01
  • Don't delete it. I hadn't seen the other solution if I hadn't seen this question. Just leave it as duplicate. – Rudy Velthuis Jun 22 '16 at 10:03

0 Answers0