How to conditionally compile a newer Indy feature?
Asked Answered
D

1

4

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?

Dovev answered 30/6, 2019 at 16:7 Comment(2)
I imagiine Remy might be able to suggest how to do this, but meanwhile you might take a look at this docwiki.embarcadero.com/RADStudio/Rio/en/IF_directive_(Delphi) and in particular the Declared option.Arlinearlington
@Arlinearlington yes, {$IF Declared(...)} is the correct way to handle this, eg: {$IF Declared(hoWantProtocolErrorContent)} or maybe it has to be fully qualified, I don't remember : {$IF Declared(IdHTTP.TIdHTTPOption. hoWantProtocolErrorContent)Chopfallen
A
7

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;
Arlinearlington answered 1/7, 2019 at 7:0 Comment(1)
Thank you, this was solved specifically using {$IF Declared(IdHTTP.TIdHTTPOption.hoWantProtocolErrorContent)}. And a neat trick I never knew about. This will definitely come in handy in the future. Sure beats using RTTI.Dovev

© 2022 - 2024 — McMap. All rights reserved.