2
0

persist.inc 4.6 KB

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