Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-05

Interface delegation - workaround?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi

Regarding bug #12778
http://bugs.freepascal.org/view.php?id=12778

Like I said before, I am no expert in using Interfaces. But why can't I do the following (see code below), which works perfectly when I run the program. Using delegation define or usecorba define in any combination. It's the same code as in the bug report, but I modified the property Hook to be an interface type.

What's the difference having

  property Hook: ITestInterface ... implements...
vs
  property Hook: THook ... implements..."

On strange thing I have noticed in the code below, is that I get 1 memory leak. FHook is not freed in some cases.

========================================
program testdelegation;
// Use to enable/disable delegation. For FPC/Delphi
{$define usedelegation}
{$ifdef fpc}
  // Use to enable/disable corba only for fpc
  {.$define usecorba}
{$endif}
{$ifdef fpc}
  {$ifdef usecorba}
    {$interfaces corba}
  {$endif}
{$endif}
const
  STestInterface = '{3FB19775-F5FA-464C-B10C-D8137D742088}';
type
  ITestInterface = interface
    [STestInterface]
    procedure DoSomething;
  end;
  {$ifndef usecorba}
  TBaseObject = class(TInterfacedObject);
  {$else}
  TBaseObject = class(TObject);
  {$endif}
  THook = class(TBaseObject, ITestInterface)
    procedure DoSomething;
  end;
  TRealClass = class(TBaseObject, ITestInterface)
  private
    FHook: ITestInterface;
  public
    constructor Create;
    destructor Destroy; override;
    {$ifndef usedelegation}
    procedure DoSomething;
    {$else}
    property Hook: ITestInterface read FHook implements ITestInterface;
    {$endif}
  end;
  { TRealClass }
  constructor TRealClass.Create;
  begin
    FHook := THook.Create;
  end;
  destructor TRealClass.Destroy;
  begin
    {$ifdef usedelegation}
//    FHook.Free;
    {$endif}
    inherited Destroy;
  end;
  {$ifndef usedelegation}
  procedure TRealClass.DoSomething;
  begin
    Writeln('Manual delegation');
    FHook.DoSomething;
  end;
  {$endif}
  { THook }
  procedure THook.DoSomething;
  begin
    Writeln('Doing something !');
  end;
var
  R: TRealClass;
  I: ITestInterface;
begin
{$ifdef usecorba}
  Writeln('Using corba interfaces');
{$else}
  Writeln('Using regular interfaces');
{$endif}
  R := TRealClass.Create;
{$ifdef fpc}
  if R.GetInterface(STestInterface, I) then
{$else}
    if R.GetInterface(ITestInterface, I) then
{$endif}
    begin
      Writeln('Got interface OK. Calling it');
      I.DoSomething;
    end
    else
      Writeln('Interface not implemented !');
end.

========================================

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 17 lines
Hi Regarding bug #12778 http://bugs.freepascal.org/view.php?id=12778 Like I said before, I am no expert in using Interfaces. But why can't I do the following (see code below), which works perfectly when I run the program. Using delegation define or usecorba define in any combination. It's the same code as in the bug report, but I modified the property Hook to be an interface type. What's the difference having property Hook: ITestInterface ... implements... vs property Hook: THook ... implements..."

The difference is that you have access to the THook methods/properties. THook just 'happens' also to implement the interface you need.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
The difference is that you have access to the THook methods/properties. THook just 'happens' also to implement the interface you need.

Ah, I get it now. So with delegation of a class you get two-in-one. Access to the class methods and properties and it resolves the interface implementation. Thanks. :-)

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email