-
Notifications
You must be signed in to change notification settings - Fork 1
/
storable.pas
762 lines (684 loc) · 20.9 KB
/
storable.pas
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit storable;
interface
uses
hashtable, genericutils, sysutils;
type
TStorable = class;
StorableClass = class of TStorable;
PPendingFixupItem = ^TPendingFixupItem;
TPendingFixupItem = record
Destination: PPointer;
ObjectID: PtrUInt;
Next: PPendingFixupItem;
end;
TReferenceFixerProcedure = procedure (const Data: Pointer; const Value: Pointer) of object;
TMethodFixerProcedure = procedure (const Data: Pointer; const Value: TMethod) of object;
TFinalFixerProcedure = procedure () of object;
PMethod = ^TMethod;
PPendingReferenceFixerItem = ^TPendingReferenceFixerItem;
TPendingReferenceFixerItem = record
Fixer: TReferenceFixerProcedure;
Data, Value: Pointer;
Next: PPendingReferenceFixerItem;
end;
PPendingMethodFixerItem = ^TPendingMethodFixerItem;
TPendingMethodFixerItem = record
Fixer: TMethodFixerProcedure;
Data: Pointer;
Value: TMethod;
Next: PPendingMethodFixerItem;
end;
PFinalFixerItem = ^TFinalFixerItem;
TFinalFixerItem = record
Fixer: TFinalFixerProcedure;
Next: PFinalFixerItem;
end;
TFixupHashTable = specialize THashTable<PtrUInt, Pointer, PtrUIntUtils>;
TRawByteStringArray = array of RawByteString;
TReadStream = class
protected
type
PMethodFixupData = ^TMethodFixupData;
TMethodFixupData = record
Field: PMethod;
MethodName: RawByteString;
end;
var
FActive: Boolean;
FObjectsRead: TFixupHashTable;
FPendingFixups: PPendingFixupItem;
FPendingReferenceFixers: PPendingReferenceFixerItem;
FPendingMethodFixers: PPendingMethodFixerItem;
FFinalFixers: PFinalFixerItem;
FInput: File;
FVersion: Cardinal;
procedure VerifyFieldType(FieldType: Byte);
function GetFilename(): RawByteString;
procedure FixupMethod(const Data: Pointer; const Value: Pointer);
public
constructor Create(var AInput: File);
destructor Destroy; override;
procedure DisableChecks();
function ReadByte: Byte;
function ReadBoolean: Boolean;
function ReadCardinal: Cardinal;
function ReadInteger: Integer;
function ReadPtrUInt: PtrUInt;
function ReadDouble: Double;
function ReadString: RawByteString;
procedure ReadByteStream(var Buffer; Length: Cardinal);
function ReadClass: StorableClass;
function ReadObject: TStorable;
procedure ReadReference(Destination: PPointer);
procedure ReadReference(Fixer: TReferenceFixerProcedure; const Data: Pointer);
procedure ReadMethod(Destination: PMethod);
procedure ReadMethod(Fixer: TMethodFixerProcedure; const Data: Pointer);
procedure AddFinalFixer(Fixer: TFinalFixerProcedure);
procedure VerifySentinel();
procedure FixupReferences();
property Version: Cardinal read FVersion;
property Filename: RawByteString read GetFilename;
end;
TWriteStream = class
protected
FOutput: File;
procedure WriteFieldType(FieldType: Byte);
public
constructor Create(var AOutput: File; Version: Cardinal);
destructor Destroy; override;
procedure WriteByte(Value: Byte);
procedure WriteBoolean(Value: Boolean);
procedure WriteCardinal(Value: Cardinal);
procedure WriteInteger(Value: Integer);
procedure WritePtrUInt(Value: PtrUInt);
procedure WriteDouble(Value: Double);
procedure WriteString(Value: RawByteString);
procedure WriteByteStream(var Buffer; Length: Cardinal);
procedure WriteClass(Value: StorableClass);
procedure WriteObject(Value: TStorable);
procedure WriteReference(Value: Pointer);
procedure WriteMethod(Value: TMethod);
procedure WriteSentinel();
end;
TStorable = class abstract
protected
{$IFOPT C+} FDebugCalledInherited: Boolean; {$ENDIF}
public
constructor Create();
constructor Read(Stream: TReadStream); virtual;
procedure Write(Stream: TWriteStream); virtual;
end;
EStorageError = class(Exception)
end;
procedure RegisterStorableClass(AClass: StorableClass);
procedure RegisterStorableClassSynonym(AClassName: RawByteString; RestoreClass: StorableClass);
function GetRegisteredClass(AClassName: RawByteString): StorableClass;
function GetRegisteredClasses(Superclass: TClass): TRawByteStringArray;
procedure StoreObjectToFile(FileName: RawByteString; Value: TStorable; Version: Cardinal);
function ReadObjectFromFile(FileName: RawByteString): TStorable;
implementation
uses
exceptions, hashfunctions;
type
TStorableClassesHashTable = specialize THashTable<RawByteString, StorableClass, RawByteStringUtils>;
var
RegisteredClasses: TStorableClassesHashTable;
const { values with bit patterns unlikely to be found in typical data (so e.g. not $00, $01, $FF) }
btSignature = $AA;
btStream = $BB;
btStreamEnd = $DD;
btBoolean = $60;
btCardinal = $61;
btInteger = $62;
btPtrUInt = $63;
btDouble = $63;
btString = $65;
btByteStream = $66;
btClass = $80;
btReference = $81;
btMethod = $82;
btObject = $51;
btObjectData = $52;
btObjectEnd = $54;
btSentinel = $58;
const
ciNoClass = 'nil';
{$IFOPT C+}
function HasStorableClassName(AClass: StorableClass): Boolean;
begin
Result := Pos('$', AClass.ClassName) = 0;
end;
{$ENDIF}
procedure RegisterStorableClass(AClass: StorableClass);
begin
Assert(not RegisteredClasses.Has(AClass.ClassName), AClass.ClassName + ' registered twice');
{$IFOPT C+} Assert(HasStorableClassName(AClass), AClass.ClassName + ' has a mangled name; storing it would not be portable across compiler versions'); {$ENDIF}
RegisteredClasses[AClass.ClassName] := AClass;
end;
procedure RegisterStorableClassSynonym(AClassName: RawByteString; RestoreClass: StorableClass);
begin
Assert(not RegisteredClasses.Has(AClassName), AClassName + ' registered twice');
RegisteredClasses[AClassName] := RestoreClass;
end;
function GetRegisteredClass(AClassName: RawByteString): StorableClass;
begin
Result := RegisteredClasses[AClassName];
end;
function GetRegisteredClasses(Superclass: TClass): TRawByteStringArray;
var
Name: RawByteString;
Index: Cardinal;
AClass: TClass;
begin
SetLength(Result, RegisteredClasses.Count); {BOGUS Warning: Function result variable of a managed type does not seem to be initialized}
Index := 0;
for Name in RegisteredClasses do
begin
AClass := RegisteredClasses[Name];
if ((AClass = Superclass) or (AClass.InheritsFrom(Superclass))) then
begin
Result[Index] := Name;
Inc(Index);
end;
end;
SetLength(Result, Index);
end;
procedure StoreObjectToFile(FileName: RawByteString; Value: TStorable; Version: Cardinal);
var
F, OldF: File;
Stream: TWriteStream;
IOResultValue: Word;
begin
Assign(F, FileName + '.$$$');
{$I-} Rewrite(F, 1); {$I+}
IOResultValue := IOResult();
if (IOResultValue <> 0) then
RunError(IOResultValue);
Stream := nil;
try
Stream := TWriteStream.Create(F, Version);
Stream.WriteObject(Value);
finally
Stream.Free();
Close(F);
end;
Assign(OldF, FileName);
if (FileExists(FileName)) then
begin
{$I-} Erase(OldF); {$I+}
if (IOResultValue <> 0) then
RunError(IOResultValue);
end;
{$I-} Rename(F, FileName); {$I+}
if (IOResultValue <> 0) then
RunError(IOResultValue);
end;
function ReadObjectFromFile(FileName: RawByteString): TStorable;
var
F: File;
Stream: TReadStream;
IOResultValue: Word;
begin
Assign(F, FileName);
{$I-} Reset(F, 1); {$I+}
IOResultValue := IOResult();
if (IOResultValue = 2) then
begin
Result := nil;
exit;
end;
if (IOResultValue <> 0) then
RunError(IOResultValue);
Stream := nil;
try
Stream := TReadStream.Create(F);
try
Result := Stream.ReadObject();
Stream.FixupReferences();
except
Stream.DisableChecks();
raise;
end;
finally
Stream.Free();
Close(F);
end;
end;
constructor TReadStream.Create(var AInput: File);
begin
inherited Create();
FActive := True;
FInput := AInput;
FObjectsRead := TFixupHashTable.Create(@PtrUIntHash32);
VerifyFieldType(btStream);
FVersion := ReadCardinal();
if (ReadByte() <> SizeOf(PtrUInt)) then
raise EStorageError.Create('Stream was written on a platform with a different pointer size.');
end;
destructor TReadStream.Destroy;
var
NextFixup: PPendingFixupItem;
NextReferenceFixer: PPendingReferenceFixerItem;
NextMethodFixer: PPendingMethodFixerItem;
NextFinalFixer: PFinalFixerItem;
begin
if (FActive) then
VerifyFieldType(btStreamEnd);
while (Assigned(FPendingFixups)) do
begin
NextFixup := FPendingFixups^.Next;
Dispose(FPendingFixups);
FPendingFixups := NextFixup;
end;
while (Assigned(FPendingReferenceFixers)) do
begin
NextReferenceFixer := FPendingReferenceFixers^.Next;
Dispose(FPendingReferenceFixers);
FPendingReferenceFixers := NextReferenceFixer;
end;
while (Assigned(FPendingMethodFixers)) do
begin
NextMethodFixer := FPendingMethodFixers^.Next;
Dispose(FPendingMethodFixers);
FPendingMethodFixers := NextMethodFixer;
end;
while (Assigned(FFinalFixers)) do
begin
NextFinalFixer := FFinalFixers^.Next;
Dispose(FFinalFixers);
FFinalFixers := NextFinalFixer;
end;
FObjectsRead.Free();
inherited;
end;
procedure TReadStream.DisableChecks();
begin
FActive := False;
end;
procedure TReadStream.VerifyFieldType(FieldType: Byte);
var
Signature: Byte;
begin
if (ReadByte() <> btSignature) then
raise EStorageError.Create('Stream inconsistent - type signature marker not found');
Signature := ReadByte();
if (Signature <> FieldType) then
raise EStorageError.Create('Stream inconsistent - expected type signature ' + IntToHex(FieldType, 2) + ' but found ' + IntToHex(Signature, 2));
end;
function TReadStream.ReadByte: Byte;
begin
{$HINTS OFF} // The following statement is guaranteed to either set Result or throw an exception, but compiler doesn't know that
BlockRead(FInput, Result, SizeOf(Result));
{$HINTS ON}
end;
function TReadStream.ReadBoolean: Boolean;
begin
VerifyFieldType(btBoolean);
Result := ReadByte() = $01;
end;
function TReadStream.ReadCardinal: Cardinal;
begin
VerifyFieldType(btCardinal);
{$HINTS OFF} // The following statement is guaranteed to either set Result or throw an exception, but compiler doesn't know that
BlockRead(FInput, Result, SizeOf(Result));
{$HINTS ON}
end;
function TReadStream.ReadInteger: Integer;
begin
VerifyFieldType(btInteger);
{$HINTS OFF} // The following statement is guaranteed to either set Result or throw an exception, but compiler doesn't know that
BlockRead(FInput, Result, SizeOf(Result));
{$HINTS ON}
end;
function TReadStream.ReadPtrUInt: PtrUInt;
begin
VerifyFieldType(btPtrUInt);
{$HINTS OFF} // The following statement is guaranteed to either set Result or throw an exception, but compiler doesn't know that
BlockRead(FInput, Result, SizeOf(Result));
{$HINTS ON}
end;
function TReadStream.ReadDouble: Double;
begin
VerifyFieldType(btDouble);
{$HINTS OFF} // The following statement is guaranteed to either set Result or throw an exception, but compiler doesn't know that
BlockRead(FInput, Result, SizeOf(Result));
{$HINTS ON}
end;
function TReadStream.ReadString: RawByteString;
var
Length: Cardinal;
begin
VerifyFieldType(btString);
Result := '';
Length := ReadCardinal();
// XXX should also read dynamic code page
if (Length > 0) then
begin
SetLength(Result, Length);
BlockRead(FInput, Result[1], Length);
end;
end;
procedure TReadStream.ReadByteStream(var Buffer; Length: Cardinal);
begin
VerifyFieldType(btByteStream);
BlockRead(FInput, Buffer, Length);
end;
function TReadStream.ReadClass: StorableClass;
var
RestoreClassName: RawByteString;
begin
VerifyFieldType(btClass);
RestoreClassName := ReadString();
if (RestoreClassName <> ciNoClass) then
begin
Assert(RegisteredClasses.Has(RestoreClassName), 'Tried to restore unregistered class ' + RestoreClassName);
Result := GetRegisteredClass(RestoreClassName);
end
else
begin
Result := nil;
end;
end;
function TReadStream.ReadObject: TStorable;
var
ClassValue: StorableClass;
ObjectID: PtrUInt;
begin
VerifyFieldType(btObject);
ClassValue := ReadClass();
if (not Assigned(ClassValue)) then
begin
Result := nil;
end
else
begin
ObjectID := ReadPtrUInt();
Result := ClassValue.Read(Self);
{$IFOPT C+} Assert(Result.FDebugCalledInherited); {$ENDIF} // it's initialised to false when the object is created
FObjectsRead.Add(ObjectID, Result);
end;
VerifyFieldType(btObjectEnd);
end;
procedure TReadStream.ReadReference(Destination: PPointer);
var
Item: PPendingFixupItem;
ObjectID: PtrUInt;
begin
VerifyFieldType(btReference);
ObjectID := ReadPtrUInt();
{$HINTS OFF} // Compiler thinks casting 'nil' to PtrUInt might be non-portable
if (ObjectID <> PtrUInt(nil)) then
{$HINTS ON}
begin
New(Item);
Item^.Destination := Destination;
Item^.ObjectID := ObjectID;
Item^.Next := FPendingFixups;
FPendingFixups := Item;
end
else
begin
Destination^ := nil;
end;
end;
procedure TReadStream.ReadReference(Fixer: TReferenceFixerProcedure; const Data: Pointer);
var
Item: PPendingReferenceFixerItem;
begin
New(Item);
Item^.Fixer := Fixer;
Item^.Data := Data;
ReadReference(@(Item^.Value));
Item^.Next := FPendingReferenceFixers;
FPendingReferenceFixers := Item;
end;
procedure TReadStream.ReadMethod(Destination: PMethod);
var
MethodData: PMethodFixupData;
begin
Assert(Assigned(Destination));
VerifyFieldType(btMethod);
New(MethodData);
MethodData^.Field := Destination;
MethodData^.MethodName := ReadString();
ReadReference(@FixupMethod, MethodData);
end;
procedure TReadStream.FixupMethod(const Data: Pointer; const Value: Pointer);
var
MethodData: PMethodFixupData;
ResolvedMethodAddress: Pointer;
begin
Assert(Assigned(Data));
MethodData := PMethodFixupData(Data);
Assert(Assigned(MethodData^.Field));
if (Assigned(Value)) then
begin
ResolvedMethodAddress := TObject(Value).MethodAddress(MethodData^.MethodName);
if (not Assigned(ResolvedMethodAddress)) then
raise EStorageError.Create('Stream references non-existent method name "' + MethodData^.MethodName + '" for class ' + TObject(Value).ClassName);
MethodData^.Field^.Data := Value;
MethodData^.Field^.Code := ResolvedMethodAddress;
end
else
begin
if (MethodData^.MethodName <> '') then
raise EStorageError.Create('Stream inconsistent - method name present ("'+ MethodData^.MethodName + '") but reference is missing');
MethodData^.Field^.Data := nil;
MethodData^.Field^.Code := nil;
end;
Dispose(MethodData);
end;
procedure TReadStream.ReadMethod(Fixer: TMethodFixerProcedure; const Data: Pointer);
var
Item: PPendingMethodFixerItem;
begin
New(Item);
Item^.Fixer := Fixer;
Item^.Data := Data;
ReadMethod(@(Item^.Value));
Item^.Next := FPendingMethodFixers;
FPendingMethodFixers := Item;
end;
procedure TReadStream.AddFinalFixer(Fixer: TFinalFixerProcedure);
var
Item: PFinalFixerItem;
begin
New(Item);
Item^.Fixer := Fixer;
Item^.Next := FFinalFixers;
FFinalFixers := Item;
end;
procedure TReadStream.VerifySentinel();
begin
VerifyFieldType(btSentinel);
end;
procedure TReadStream.FixupReferences();
var
NextFixup: PPendingFixupItem;
NextReferenceFixer: PPendingReferenceFixerItem;
NextMethodFixer: PPendingMethodFixerItem;
NextFinalFixer: PFinalFixerItem;
begin
while (Assigned(FPendingFixups)) do
begin
FPendingFixups^.Destination^ := FObjectsRead[FPendingFixups^.ObjectID];
NextFixup := FPendingFixups^.Next;
Dispose(FPendingFixups);
FPendingFixups := NextFixup;
end;
while (Assigned(FPendingReferenceFixers)) do
begin
FPendingReferenceFixers^.Fixer(FPendingReferenceFixers^.Data, FPendingReferenceFixers^.Value);
NextReferenceFixer := FPendingReferenceFixers^.Next;
Dispose(FPendingReferenceFixers);
FPendingReferenceFixers := NextReferenceFixer;
end;
while (Assigned(FPendingMethodFixers)) do
begin
FPendingMethodFixers^.Fixer(FPendingMethodFixers^.Data, FPendingMethodFixers^.Value);
NextMethodFixer := FPendingMethodFixers^.Next;
Dispose(FPendingMethodFixers);
FPendingMethodFixers := NextMethodFixer;
end;
while (Assigned(FFinalFixers)) do
begin
FFinalFixers^.Fixer();
NextFinalFixer := FFinalFixers^.Next;
Dispose(FFinalFixers);
FFinalFixers := NextFinalFixer;
end;
end;
function TReadStream.GetFilename(): RawByteString;
begin
Result := FileRec(FInput).Name;
end;
constructor TWriteStream.Create(var AOutput: File; Version: Cardinal);
begin
inherited Create();
FOutput := AOutput;
WriteFieldType(btStream);
WriteCardinal(Version);
WriteByte(SizeOf(PtrUInt));
end;
destructor TWriteStream.Destroy;
begin
WriteFieldType(btStreamEnd);
inherited;
end;
procedure TWriteStream.WriteFieldType(FieldType: Byte);
begin
WriteByte(btSignature);
WriteByte(FieldType);
end;
procedure TWriteStream.WriteByte(Value: Byte);
begin
BlockWrite(FOutput, Value, SizeOf(Value));
end;
procedure TWriteStream.WriteBoolean(Value: Boolean);
begin
WriteFieldType(btBoolean);
if (Value) then
WriteByte($01)
else
WriteByte($00);
end;
procedure TWriteStream.WriteCardinal(Value: Cardinal);
begin
WriteFieldType(btCardinal);
BlockWrite(FOutput, Value, SizeOf(Value));
end;
procedure TWriteStream.WriteInteger(Value: Integer);
begin
WriteFieldType(btInteger);
BlockWrite(FOutput, Value, SizeOf(Value));
end;
procedure TWriteStream.WritePtrUInt(Value: PtrUInt);
begin
WriteFieldType(btPtrUInt);
BlockWrite(FOutput, Value, SizeOf(Value));
end;
procedure TWriteStream.WriteDouble(Value: Double);
begin
WriteFieldType(btDouble);
BlockWrite(FOutput, Value, SizeOf(Value));
end;
procedure TWriteStream.WriteString(Value: RawByteString);
var
Result: Cardinal;
begin
WriteFieldType(btString);
WriteCardinal(Length(Value));
// XXX should also write dynamic code page
if (Length(Value) > 0) then
begin
Result := 0;
BlockWrite(FOutput, Value[1], Length(Value), Result); // $R-
Assert(Result = Length(Value));
end;
end;
procedure TWriteStream.WriteByteStream(var Buffer; Length: Cardinal);
begin
WriteFieldType(btByteStream);
Assert(Length <= High(LongInt)); // BlockWrite(3 args) last arg is LongInt, not Cardinal
BlockWrite(FOutput, Buffer, Length); // $R-
end;
procedure TWriteStream.WriteClass(Value: StorableClass);
begin
WriteFieldType(btClass);
if (not Assigned(Value)) then
begin
WriteString(ciNoClass);
end
else
begin
Assert(RegisteredClasses.Has(Value.ClassName), 'Tried to store unregistered class ' + Value.ClassName);
WriteString(Value.ClassName);
end;
end;
procedure TWriteStream.WriteObject(Value: TStorable);
begin
WriteFieldType(btObject);
if (not Assigned(Value)) then
begin
WriteClass(nil);
end
else
begin
WriteClass(StorableClass(Value.ClassType));
WritePtrUInt(PtrUInt(Value));
{$IFOPT C+} Value.FDebugCalledInherited := False; {$ENDIF}
Value.Write(Self);
{$IFOPT C+} Assert(Value.FDebugCalledInherited); {$ENDIF}
end;
WriteFieldType(btObjectEnd);
end;
procedure TWriteStream.WriteReference(Value: Pointer);
begin
WriteFieldType(btReference);
{$HINTS OFF} // Compiler thinks casting a pointer to PtrUInt might not be portable
WritePtrUInt(PtrUInt(Value));
{$HINTS ON}
end;
procedure TWriteStream.WriteMethod(Value: TMethod);
var
ResolvedMethodName: RawByteString;
begin
WriteFieldType(btMethod);
if (Assigned(Value.Data)) then
begin
ResolvedMethodName := TObject(Value.Data).MethodName(Value.Code);
Assert(ResolvedMethodName <> '', 'Methods to be saved by TStorable.WriteMethod() must be published');
WriteString(ResolvedMethodName);
end
else
begin
WriteString('');
end;
WriteReference(Value.Data);
end;
procedure TWriteStream.WriteSentinel();
begin
WriteFieldType(btSentinel);
end;
constructor TStorable.Create();
begin
inherited;
Assert(RegisteredClasses.Has(ClassName), 'Class ' + ClassName + ' has not been registered with RegisterStorableClass().');
end;
constructor TStorable.Read(Stream: TReadStream);
begin
{$IFOPT C+} FDebugCalledInherited := True; {$ENDIF}
Stream.VerifyFieldType(btObjectData);
end;
procedure TStorable.Write(Stream: TWriteStream);
begin
{$IFOPT C+} FDebugCalledInherited := True; {$ENDIF}
Stream.WriteFieldType(btObjectData);
end;
initialization
RegisteredClasses := TStorableClassesHashTable.Create(@RawByteStringHash32);
finalization
RegisteredClasses.Free();
end.