delphiinterfacedelphi-xereference-counting

Is there a non-reference-counted base class like TInterfacedObject?


I need a base class like TInterfacedObject but without reference counting (so a kind of TNonRefCountedInterfacedObject).

This actually is the nth time I need such a class and somehow I always end up writing (read: copy and pasting) my own again and again. I cannot believe that there is no "official" base class I can use.

Is there a base class somewhere in the RTL implementing IInterface but without reference counting which I can derive my classes from?


Solution

  • In the unit Generics.Defaults there is a class TSingletonImplementation defined. Available in Delphi 2009 and above.

      // A non-reference-counted IInterface implementation.
      TSingletonImplementation = class(TObject, IInterface)
      protected
        function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
        function _AddRef: Integer; stdcall;
        function _Release: Integer; stdcall;
      end;