persist.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************}
  11. {* TPersistent *}
  12. {****************************************************************************}
  13. procedure TPersistent.AssignError(Source: TPersistent);
  14. Var SourceName : String;
  15. begin
  16. If Source<>Nil then
  17. SourceName:=Source.ClassName
  18. else
  19. SourceName:='Nil';
  20. raise EConvertError.CreateFmt (SAssignError,[SourceName,ClassName]);
  21. end;
  22. procedure TPersistent.AssignTo(Dest: TPersistent);
  23. begin
  24. Dest.AssignError(Self);
  25. end;
  26. procedure TPersistent.DefineProperties(Filer: TFiler);
  27. begin
  28. end;
  29. function TPersistent.GetOwner: TPersistent;
  30. begin
  31. Result:=Nil;
  32. end;
  33. destructor TPersistent.Destroy;
  34. begin
  35. If Assigned(FObservers) then
  36. begin
  37. FPONotifyObservers(Self,ooFree,Nil);
  38. FreeAndNil(FObservers);
  39. end;
  40. inherited Destroy;
  41. end;
  42. procedure TPersistent.FPOAttachObserver(AObserver: TObject);
  43. Var
  44. I : IFPObserver;
  45. begin
  46. If Not AObserver.GetInterface(SGUIDObserver,I) then
  47. Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
  48. If not Assigned(FObservers) then
  49. FObservers:=TFPList.Create;
  50. FObservers.Add(I);
  51. end;
  52. procedure TPersistent.FPODetachObserver(AObserver: TObject);
  53. Var
  54. I : IFPObserver;
  55. begin
  56. If Not AObserver.GetInterface(SGUIDObserver,I) then
  57. Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
  58. If Assigned(FObservers) then
  59. begin
  60. FObservers.Remove(I);
  61. If (FObservers.Count=0) then
  62. FreeAndNil(FObservers);
  63. end;
  64. end;
  65. procedure TPersistent.FPONotifyObservers(ASender: TObject;
  66. AOperation: TFPObservedOperation; Data : Pointer);
  67. Var
  68. I : Integer;
  69. Obs : IFPObserver;
  70. begin
  71. If Assigned(FObservers) then
  72. For I:=FObservers.Count-1 downto 0 do
  73. begin
  74. Obs:=IFPObserver(FObservers[i]);
  75. Obs.FPOObservedChanged(Self,AOperation,Data);
  76. end;
  77. end;
  78. procedure TPersistent.Assign(Source: TPersistent);
  79. begin
  80. If Source<>Nil then
  81. Source.AssignTo(Self)
  82. else
  83. AssignError(Nil);
  84. end;
  85. function TPersistent.GetNamePath: string;
  86. Var OwnerName :String;
  87. TheOwner: TPersistent;
  88. begin
  89. Result:=ClassName;
  90. TheOwner:=GetOwner;
  91. If TheOwner<>Nil then
  92. begin
  93. OwnerName:=TheOwner.GetNamePath;
  94. If OwnerName<>'' then Result:=OwnerName+'.'+Result;
  95. end;
  96. end;
  97. {****************************************************************************}
  98. {* TInterfacedPersistent *}
  99. {****************************************************************************}
  100. procedure TInterfacedPersistent.AfterConstruction;
  101. Var TheOwner: TPersistent;
  102. begin
  103. inherited;
  104. TheOwner:=GetOwner;
  105. if assigned(TheOwner) then
  106. TheOwner.GetInterface(IUnknown,FOwnerInterface);
  107. end;
  108. function TInterfacedPersistent._AddRef: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  109. begin
  110. if assigned(FOwnerInterface) then
  111. Result:=FOwnerInterface._AddRef
  112. else
  113. Result:=-1;
  114. end;
  115. function TInterfacedPersistent._Release: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  116. begin
  117. if assigned(FOwnerInterface) then
  118. Result:=FOwnerInterface._Release
  119. else
  120. Result:=-1;
  121. end;
  122. function TInterfacedPersistent.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  123. begin
  124. if GetInterface(IID, Obj) then
  125. Result:=0
  126. else
  127. Result:=HResult($80004002);
  128. end;
  129. {****************************************************************************}
  130. {* TRecall *}
  131. {****************************************************************************}
  132. constructor TRecall.Create(AStorage,AReference: TPersistent);
  133. begin
  134. inherited Create;
  135. FStorage:=AStorage;
  136. FReference:=AReference;
  137. Store;
  138. end;
  139. destructor TRecall.Destroy;
  140. begin
  141. if Assigned(FReference) then
  142. FReference.Assign(FStorage);
  143. Forget;
  144. inherited;
  145. end;
  146. procedure TRecall.Forget;
  147. begin
  148. FReference:=nil;
  149. FreeAndNil(FStorage);
  150. end;
  151. procedure TRecall.Store;
  152. begin
  153. if Assigned(FStorage) and Assigned(FReference) then
  154. FStorage.Assign(FReference);
  155. end;