-
Notifications
You must be signed in to change notification settings - Fork 1
/
autostorable.pas
169 lines (156 loc) · 6.79 KB
/
autostorable.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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit autostorable;
interface
uses
storable, typinfo;
type
{$TYPEINFO ON}
TAutoStorable = class abstract (TStorable)
private
procedure FixupReferenceProperty(const PropInfo: Pointer; const Value: Pointer);
procedure FixupMethodProperty(const PropInfo: Pointer; const Value: TMethod);
public
constructor Read(Stream: TReadStream); override;
procedure Write(Stream: TWriteStream); override;
end;
{$TYPEINFO OFF}
implementation
uses
{$IFDEF DEBUG} debug, {$ENDIF} exceptions;
{$IF SizeOf(Int64) < SizeOf(TClass)} {$FATAL GetOrdProp returns data too small for a pointer} {$ENDIF}
constructor TAutoStorable.Read(Stream: TReadStream);
function GetNextPropertyName(out Name: AnsiString): Boolean;
begin
Name := Stream.ReadAnsiString();
Result := Name <> '';
end;
var
Name, Value: AnsiString;
AClass: TClass;
PropInfo: PPropInfo;
{$IFDEF DEBUG} OldHeapInfo: THeapInfo; {$ENDIF}
begin
inherited;
while (GetNextPropertyName(Name)) do
begin
{$IFDEF DEBUG} OldHeapInfo := SetHeapInfo('TAutoStorable.Read() reading ' + ClassName + '.' + Name); {$ENDIF}
PropInfo := GetPropInfo(Self, Name);
if (IsStoredProp(Self, PropInfo)) then
case PropInfo^.PropType^.Kind of
tkInteger, tkChar, tkBool: SetOrdProp(Self, PropInfo, Stream.ReadCardinal());
tkSString, tkAString: SetStrProp(Self, PropInfo, Stream.ReadAnsiString());
tkEnumeration: SetEnumProp(Self, PropInfo, Stream.ReadAnsiString());
tkFloat: SetFloatProp(Self, PropInfo, Stream.ReadDouble());
tkClass:
begin
AClass := GetTypeData(PropInfo^.PropType)^.ClassType;
if (AClass.InheritsFrom(TAutoStorable)) then
SetObjectProp(Self, PropInfo, Stream.ReadObject())
else
Stream.ReadReference(@FixupReferenceProperty, PropInfo);
end;
tkClassRef:
begin
Assert(GetTypeData(GetTypeData(PropInfo^.PropType)^.InstanceType)^.ClassType.InheritsFrom(TStorable));
Value := Stream.ReadAnsiString();
if (Value = '') then
begin
{$HINTS OFF} // "Conversion between ordinals and pointers is not portable" (but PtrUInt is designed for that purpose)
SetOrdProp(Self, PropInfo, PtrUInt(nil));
{$HINTS ON}
end
else
begin
AClass := GetRegisteredClass(Value);
Assert(Assigned(AClass), 'Couldn''t find class ' + Value);
Assert(AClass.InheritsFrom(GetTypeData(GetTypeData(PropInfo^.PropType)^.InstanceType)^.ClassType));
SetOrdProp(Self, PropInfo, PtrUInt(AClass));
end;
end;
tkMethod:
begin
Stream.ReadMethod(@FixupMethodProperty, PropInfo);
end;
else
Assert(False, 'Property ' + PropInfo^.Name + ' uses a type (' + GetEnumName(TypeInfo(TTypeKind), Ord(PropInfo^.PropType^.Kind)) + ') that TAutoStorable does not yet support');
end;
{$IFDEF DEBUG} SetHeapInfo(OldHeapInfo); {$ENDIF}
end;
end;
procedure TAutoStorable.FixupReferenceProperty(const PropInfo: Pointer; const Value: Pointer);
begin
{$HINTS OFF} // "Conversion between ordinals and pointers is not portable" (but PtrUInt is designed for that purpose)
SetOrdProp(Self, PPropInfo(PropInfo), PtrUInt(Value));
{$HINTS ON}
end;
procedure TAutoStorable.FixupMethodProperty(const PropInfo: Pointer; const Value: TMethod);
begin
SetMethodProp(Self, PPropInfo(PropInfo), Value);
end;
procedure TAutoStorable.Write(Stream: TWriteStream);
var
OurTypeInfo: PTypeInfo;
OurTypeData: PTypeData;
PropertyList: PPropList;
PropIndex: Cardinal;
PropInfo: PPropInfo;
AClass: TClass;
begin
inherited;
OurTypeInfo := ClassInfo();
OurTypeData := GetTypeData(OurTypeInfo);
if (OurTypeData^.PropCount > 0) then
begin
GetMem(PropertyList, OurTypeData^.PropCount * SizeOf(Pointer));
GetPropInfos(OurTypeInfo, PropertyList);
for PropIndex := 0 to OurTypeData^.PropCount-1 do
begin
PropInfo := PropertyList^[PropIndex];
if (IsStoredProp(Self, PropInfo)) then
begin
Stream.WriteAnsiString(PropInfo^.Name);
case PropInfo^.PropType^.Kind of
tkInteger, tkChar, tkBool: Stream.WriteCardinal(Cardinal(GetOrdProp(Self, PropInfo)));
tkSString, tkAString: Stream.WriteAnsiString(GetStrProp(Self, PropInfo));
tkEnumeration: Stream.WriteAnsiString(GetEnumProp(Self, PropInfo));
tkFloat: Stream.WriteDouble(GetFloatProp(Self, PropInfo));
tkClass:
begin
AClass := GetTypeData(PropInfo^.PropType)^.ClassType;
if (AClass.InheritsFrom(TAutoStorable)) then
Stream.WriteObject(GetObjectProp(Self, PropInfo) as TAutoStorable)
else
Stream.WriteReference(GetObjectProp(Self, PropInfo));
end;
tkClassRef:
begin
{$IFOPT C+}
AClass := GetTypeData(GetTypeData(PropInfo^.PropType)^.InstanceType)^.ClassType;
Assert(AClass.InheritsFrom(TStorable));
{$ENDIF}
if (not Assigned(TClass(GetOrdProp(Self, PropInfo)))) then {BOGUS Hint: Conversion between ordinals and pointers is not portable} // bogus only because we do a test higher up so we'll hit a $FATAL if this is a problem
begin
Stream.WriteAnsiString('');
end
else
begin
Assert(GetTypeData(PropInfo^.PropType)^.InstanceType^.Kind = tkClass);
Assert(Assigned(TClass(GetOrdProp(Self, PropInfo)))); {BOGUS Hint: Conversion between ordinals and pointers is not portable} // bogus only because we do a test higher up so we'll hit a $FATAL if this is a problem
Stream.WriteAnsiString(TClass(GetOrdProp(Self, PropInfo)).ClassName); {BOGUS Hint: Conversion between ordinals and pointers is not portable} // bogus only because we do a test higher up so we'll hit a $FATAL if this is a problem
end;
end;
tkMethod:
begin
Stream.WriteMethod(GetMethodProp(Self, PropInfo));
end;
else
Assert(False, 'Property ' + PropInfo^.Name + ' uses a type (' + GetEnumName(TypeInfo(TTypeKind), Ord(PropInfo^.PropType^.Kind)) + ') that TAutoStorable does not yet support');
end;
end;
end;
FreeMem(PropertyList);
end;
Stream.WriteAnsiString('');
end;
end.