[fpc-pascal]no refcounted interfaces
Florian.Klaempfl at gmx.de
Fri Apr 9 14:02:19 CEST 2004
Dean Zobec wrote:
> I've talked with some friends of mine about the use of Interface base classes
> without reference counting in Delphi
> like this
> TNoRefCount = class(TObject, IUnknown)
> function QueryInterface(const IID:TGUID; out Obj):HResult; stdcall;
> function _AddRef: Integer; stdcall;
> function _Release: Integer; stdcall;
> function TNoRefCount.QueryInterface(const IID:TGUID; out Obj): HResult;
> if GetInterface(IID, Obj) then
> Result := 0
> Result := Windows.E_NoInterface;
> function TNoRefCount._AddRef: Integer;
> Result := -1
> function TNoRefCount._Release: Integer;
> Result := -1
> We are all experiencing the same kind of problems:
> 1) there's no easy way to obtain a TObject from an interface (whereas there's
> no particular reason not to have it. A solution proposed was to implement an
> AsObject method in all the interfaces used.
> 2) there's no FreeAndNil function that would free the interface and the object
> It would be nice to be able to do something like this in an abstract factory:
> MyRef: ISomeInterface;
> MyRef := SomeFactory.CreateObject as ISomeInterface;
> ...use MyRef...
> MyRef := nil;
> Any Idea of a solution in free pascal?
From the fpc wiki
Interface type selection: The commandline switch -SI and the directive
$interfaces (possible values: corba and com) allow you to choose the
type of interfaces which has no parent. Interfaces with parent inherit
the style from their parent regardless of the currently selected
* COM style interfaces work like Delphi interfaces: they inherit
implicitly from IUnknown and they are reference counted.
* CORBA style interfaces are neither ref. counted nor do they
inherit from IUnknown.
More information about the fpc-pascal