Does that mean I have the weekend off?

[edit] Added CORBA too, again based on your own code.[/edit]
The first example leaks in FPC and not in Delphi.
Here a leak-free version - both FPC and Delphi - that uses TAggregatedObject.
TClassA has no refcounting, the controller, TClassB does that:
{$ifdef fpc}{$mode delphi}{$interfaces com}{$endif}
uses classes;
type
IMyInterface = interface
['{2D9E9882-BB2F-4AB7-880D-4B82C2C4C425}']
procedure DoStuff;
end;
TClassA = class(TAggregatedObject ,IMyInterface)
procedure DoStuff;
end;
TClassB = class(TInterfacedObject,IMyInterface)
private
FClassA: TClassA;
public
constructor Create;
destructor destroy;override;
property ClassA: TClassA read FClassA implements IMyInterface;
end;
procedure TClassA.DoStuff;
begin
writeln('Whatever I have to do');
end;
constructor TClassB.Create;
begin
inherited create;
FClassA:= TClassA.Create(self as IInterface);
end;
destructor TClassB.Destroy;
begin
FClassA.Free;
inherited destroy;
end;
var
test:IMyInterface;
begin
test := TClassB.Create;// Created as IMyInterface, not as class
test.DoStuff; // delegates to ClassA through QueryInterface, hence COM not CORBA
end.
Will add a CORBA version later.
Done
Corba version:
{$ifdef fpc}{$mode delphi}{$endif}
{$interfaces corba}
type
IMyInterface = interface
['WhyUseCorba']
procedure DoStuff;
end;
TClassA = class(IMyInterface)
public
procedure DoStuff;
end;
TClassB = class(TObject, IMyInterface)
private
FClassA: TClassA;
public
constructor create;
destructor destroy;override;
property MyInterface: TClassA read FClassA implements IMyInterface;
end;
procedure TClassA.Dostuff;
begin
writeln('Whatever I have to do');
end;
constructor TCLassB.Create;
begin
inherited create;
FClassA := TClassA.Create;
end;
destructor TClassB.Destroy;
begin
FClassA.Free;
inherited destroy;
end;
var
MyClass: TClassB;
MyInterface: IMyInterface;
begin
MyClass := TClassB.Create;
MyInterface := MyClass;
MyInterface.DoStuff;
MyClass.Free;
end.
Much simpler than I thought. "implements" is sufficiently implemented for CORBA nowadays.
You still can't use
as though, just hard casts (ugly):
var
MyClass: TClassB;
begin
MyClass := TClassB.Create;
IMyInterface(MyClass).DoStuff;
MyClass.Free;
end.
CORBA does not compile in Delphi.
But for people who want delegates and CORBA the above should provide a correct example.