[fpc-devel] Using Macros for IInterface

Sven Barth pascaldragon at googlemail.com
Wed Sep 26 13:44:12 CEST 2012


Am 26.09.2012 13:27, schrieb Graeme Geldenhuys:
> On 2012-09-26 11:40, Marco van de Voort wrote:
>>
>> It means the interface will always be constref, and thus no ifdefing
>> in FPC
>> (if you don't support <2.6.0) will be necessary.
>
> For fpGUI, I only support the latest stable release of FPC, so that
> would be 2.6.0. So I should be fine with removing that IFDEF then.

Correct.

>> But users don't need to ifdef if they don't use compilers that
>> don't support constref (FPC 2.4 series and delphi)
>
> Umm, so here might be a problem. tiOPF is a shared code base between FPC
> and Delphi. Does the compiler mode (we use mode delphi in tiOPF) not
> affect the constref? After all, it seems Delphi doesn't have a constref
> correct? So in mode delphi the IInterface should always be defined in a
> Delphi compatible manner?
>
>
> Or would this mean that in fpGUI my IInterface usage can always be
> constref - because fpGUI only supports FPC and only v2.6.0.
>
> And in tiOPF the IInterface usage can always be const, because Delphi
> doesn't support constref, and we use mode delphi?

A mode switch does not and can not affect how a method is declared 
especially if the method is already compiled. After all using 
"modeswitch unicodestring" in your own unit does not change what string 
type "TStrings" uses.

So: In fpGUI you can always use "constref" if you only support 2.6.0 and 
newer.

In tiOPF you'll have to live with IFDEFs, as Delphi doesn't support 
macros. You could try the following though (not tested at all):

=== source begin ===

{$ifdef fpc}
   {$macro on}
   {$define const:=constref}
   {$ifndef windows}
     {$define stdcall:=cdecl}
   {$endif}
{$endif}

   TSomeClass = class(TObject, IInterface)
     function QueryInterface(const iid : tguid;out obj) : longint; stdcall;
     // ...
   end;

{$ifdef fpc}
   {$macro off}
   // maybe add a "undefine const" and "undefine stdcall" just to be sure
{$endif}

=== source end ===

As I said: This is not tested and I'm not even sure it works, but it's 
worth a try.

Regards,
Sven



More information about the fpc-devel mailing list