Search code examples
delphiindyconditional-compilation

How to conditionally compile a newer Indy feature?


I've already found this answer on how to check the Indy version at run-time, and there are multiple different ways. However I'm looking how to use conditionals to check the Indy version at compile-time. There's a feature in newer versions of Indy, and I want my open-source project to use this feature if it's available. But I need to conditionally compile it.

I've found IdVers.inc, but this file only contains constants - no version conditionals.

More specifically, the TIdHTTP has a property HTTPOptions which has a new choice hoWantProtocolErrorContent. If this is available, I'd like to use it.

How can I conditionally use this option if it's available?


Solution

  • I think you can get the result you're wanting to achieve using the

    {$if declared ...
    

    construct. There is an example of its usage in SysInit.Pas in the rtl:

    function GetTlsSize: Integer;
    {$IF defined(POSIX) and defined(CPUX86) and (not defined(EXTERNALLINKER))}
    asm
            // Use assembler code not to include PIC base gain
            MOV  EAX, offset TlsLast
    end;
    {$ELSE}
    begin
      Result := NativeInt(@TlsLast);
      {$IF DECLARED(TlsStart)}
      Result := Result - NativeInt(@TlsStart);
      {$ENDIF}
      [...]
    

    As well as the article I mentioned in a comment, $If Declared, there is also this in the D2009 online help.

    $if declared works with methods of classes, e.g.

    procedure TMyClass.DoSomething;
    begin
      {$if declared(TMyClass.Added)}  // Added being a procedure of TMyClass
      Added;
      {$endif}
    end;