Browse Source

* Added interfacedobject

michael 7 years ago
parent
commit
17ff9cfaf5
1 changed files with 45 additions and 0 deletions
  1. 45 0
      packages/rtl/classes.pas

+ 45 - 0
packages/rtl/classes.pas

@@ -186,6 +186,20 @@ type
   end;
   TPersistentClass = Class of TPersistent;
 
+  { TInterfacedPersistent }
+
+  TInterfacedPersistent = class(TPersistent, IInterface)
+  private
+    FOwnerInterface: IInterface;
+  protected
+    function _AddRef: Integer;
+    function _Release: Integer;
+  public
+    function QueryInterface(const IID: TGUID; out Obj): integer; virtual;
+    procedure AfterConstruction; override;
+  end;
+
+
   TStrings = Class;
   { TStringsEnumerator class }
 
@@ -579,6 +593,37 @@ implementation
 
 uses JS;
 
+{ TInterfacedPersistent }
+
+function TInterfacedPersistent._AddRef: Integer;
+begin
+  Result:=-1;
+  if Assigned(FOwnerInterface) then
+    Result:=FOwnerInterface._AddRef;
+end;
+
+function TInterfacedPersistent._Release: Integer;
+
+begin
+  Result:=-1;
+  if Assigned(FOwnerInterface) then
+    Result:=FOwnerInterface._Release;
+end;
+
+function TInterfacedPersistent.QueryInterface(const IID: TGUID; out Obj): integer;
+begin
+  Result:=E_NOINTERFACE;
+  if GetInterface(IID, Obj)  then
+    Result:=0;
+end;
+
+procedure TInterfacedPersistent.AfterConstruction;
+begin
+  inherited AfterConstruction;
+  if (GetOwner<>nil) then
+    GetOwner.GetInterface(IInterface, FOwnerInterface);
+end;
+
 { TComponentEnumerator }
 
 constructor TComponentEnumerator.Create(AComponent: TComponent);