Is it possible to define {$IFDEF} for more than one directive at once?
Asked Answered
U

3

48

Is it possible to define more than one conditional in one {$IFDEF} directive ?
I would like to have syntax like this:

{$IFDEF Condition1 OR Condition2} DoSomething; {$ENDIF}
{$IFDEF Condition1 AND Condition2} DoSomethingElse; {$ENDIF}

Thanks

Undertone answered 4/1, 2012 at 9:47 Comment(0)
R
68

You would need to use $IF instead:

{$IF Defined(Condition1) or Defined(Condition2)}
DoSomething;
{$IFEND}
Raama answered 4/1, 2012 at 10:3 Comment(4)
Thanks, that's exactly what I needed. I guess you're not only the lead developer but also a build manager in your company, don't you ?Undertone
@MartinReiner, also the president, the director-general and the senior janitor ;-)Arnettaarnette
{$ENDIF} instead of {$IFEND}Waylen
@Waylen not when this was written back in 2012Raama
C
20

In case you have to support old Delphis (without the support for the $IF metadirective), you can use one simple and one ugly workaround:

//AND
{$IFDEF Cond1}{$IFDEF Cond2}DoSomething{$ENDIF}{$ENDIF} 

//OR
{$UNDEF Cond1OrCond2}
{$IFDEF Cond1}{$DEFINE Cond1OrCond2}{$ENDIF}
{$IFDEF Cond2}{$DEFINE Cond1OrCond2}{$ENDIF}
{$IFDEF Cond1OrCond2}DoSomething{$ENDIF}

If you are repeating the test more than once, first case should be rewritten as follows.

{$UNDEF Cond1AndCond2}
{$IFDEF Cond1}{$IFDEF Cond2}{$DEFINE Cond1AndCond2{$ENDIF}{$ENDIF} 

{$IFDEF Cond1AndCond2}DoSomething{$ENDIF}
Cypress answered 4/1, 2012 at 11:19 Comment(1)
old delphis in this context are D5 and older IIRCRaama
D
6

hey try this from the embarcadero.com

begin
  ...
 {$IF Defined(MY_DEFINE) and (LibVersion > 2.0) }
  Writeln(1);
 {$ELSE}
  Writeln(2);  

  ... 
  {$IFEND}
 end;
Dannydannye answered 4/1, 2012 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.