summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/objpas/classes/persist.inc
blob: 438170952b53edc348e44da808a23f606017436b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 1999-2000 by the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{****************************************************************************}
{*                             TPersistent                                  *}
{****************************************************************************}

procedure TPersistent.AssignError(Source: TPersistent);

Var SourceName : String;

begin
  If Source<>Nil then
    SourceName:=Source.ClassName
  else
    SourceName:='Nil';
  raise EConvertError.CreateFmt (SAssignError,[SourceName,ClassName]);
end;



procedure TPersistent.AssignTo(Dest: TPersistent);


begin
  Dest.AssignError(Self);
end;


procedure TPersistent.DefineProperties(Filer: TFiler);

begin
end;


function  TPersistent.GetOwner: TPersistent;

begin
  Result:=Nil;
end;

destructor TPersistent.Destroy;
begin
  If Assigned(FObservers) then
    begin
    FPONotifyObservers(Self,ooFree,Nil);
    FreeAndNil(FObservers);
    end;
  inherited Destroy;
end;

procedure TPersistent.FPOAttachObserver(AObserver: TObject);
Var
   I : IFPObserver;

begin
   If Not AObserver.GetInterface(SGUIDObserver,I) then
     Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
   If not Assigned(FObservers) then
     FObservers:=TFPList.Create;
   FObservers.Add(I);
end;

procedure TPersistent.FPODetachObserver(AObserver: TObject);
Var
  I : IFPObserver;

begin
  If Not AObserver.GetInterface(SGUIDObserver,I) then
    Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
  If Assigned(FObservers) then
    begin
    FObservers.Remove(I);
    If (FObservers.Count=0) then
      FreeAndNil(FObservers);
    end;
end;

procedure TPersistent.FPONotifyObservers(ASender: TObject;
  AOperation: TFPObservedOperation; Data : Pointer);
Var
  I : Integer;
  Obs : IFPObserver;

begin
  If Assigned(FObservers) then
    For I:=FObservers.Count-1 downto 0 do
      begin
      Obs:=IFPObserver(FObservers[i]);
      Obs.FPOObservedChanged(Self,AOperation,Data);
      end;
end;

procedure TPersistent.Assign(Source: TPersistent);

begin
  If Source<>Nil then
    Source.AssignTo(Self)
  else
    AssignError(Nil);
end;

function  TPersistent.GetNamePath: string;

Var OwnerName :String;
    TheOwner: TPersistent;

begin
 Result:=ClassName;
 TheOwner:=GetOwner;
 If TheOwner<>Nil then
   begin
   OwnerName:=TheOwner.GetNamePath;
   If OwnerName<>'' then Result:=OwnerName+'.'+Result;
   end;
end;


{****************************************************************************}
{*                          TInterfacedPersistent                           *}
{****************************************************************************}

procedure TInterfacedPersistent.AfterConstruction;

Var TheOwner: TPersistent;

begin
  inherited;
  TheOwner:=GetOwner;
  if assigned(TheOwner) then
    TheOwner.GetInterface(IUnknown,FOwnerInterface);
end;


function TInterfacedPersistent._AddRef: Integer;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
  if assigned(FOwnerInterface) then
    Result:=FOwnerInterface._AddRef
  else
    Result:=-1;
end;


function TInterfacedPersistent._Release: Integer;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
  if assigned(FOwnerInterface) then
    Result:=FOwnerInterface._Release
  else
    Result:=-1;
end;


function TInterfacedPersistent.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
  if GetInterface(IID, Obj) then
    Result:=0
  else
    Result:=HResult($80004002);
end;


{****************************************************************************}
{*                                TRecall                                   *}
{****************************************************************************}

constructor TRecall.Create(AStorage,AReference: TPersistent);
begin
  inherited Create;
  FStorage:=AStorage;
  FReference:=AReference;
  Store;
end;


destructor TRecall.Destroy;
begin
  if Assigned(FReference) then
   FReference.Assign(FStorage);
  Forget;
  inherited;
end;


procedure TRecall.Forget;
begin
  FReference:=nil;
  FreeAndNil(FStorage);
end;


procedure TRecall.Store;
begin
  if Assigned(FStorage) and Assigned(FReference) then
    FStorage.Assign(FReference);
end;