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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Object_;
interface
{$IF Defined(CHARTA_PASCAL)}
uses
Boolean,
Character.String_,
Integer._32.Positive,
Memory.Pointer,
System.Class_.Property_;
type
PMethodInformation = ^TMethodInformation;
TMethodInformation = record
public
Name: TAnsiString;
Address: TPointer;
end;
type
TClass = class of TObject;
TObject = class
private
class var
FSuperclass: TClass;
FInstanceSize: TPositiveInteger32;
FClassName: TAnsiString;
FQualifiedClassName: TAnsiString;
private
FClass: TClass;
public
constructor Create();
destructor Destroy(); virtual;
procedure AfterConstruction(); virtual;
procedure BeforeDestruction(); virtual;
procedure CleanupInstance();
procedure FreeInstance(); virtual;
procedure Free();
property Class_: TClass read FClass;
public
class var
Manager: TObject; // TODO: This should be a TValueManager
Methods: array of TMethodInformation;
Properties: TPropertyArray;
TypeParameters: array of TObject; // TODO: This should be array of TType
class function GetMethodInformation(const Name: TAnsiString): PMethodInformation;
class function InheritsFrom(Class_: TClass): TBoolean;
class property Superclass: TClass read FSuperclass;
class property InstanceSize: TPositiveInteger32 read FInstanceSize;
class property ClassName: TAnsiString read FClassName;
class property QualifiedClassName: TAnsiString read FQualifiedClassName;
public
// The following functions are for Delphi compatibility
class function MethodAddress(const Name: TAnsiString): Pointer;
class function ClassParent(): TClass;
public
function ClassType(): TClass;
end;
TMethod = record
public
Code: TPointer;
Data: TObject;
end;
function ClassCreate(Class_: TClass; Allocate: TBoolean): TObject;
procedure ClassDestroy(Instance: TObject; Deallocate: TBoolean);
function AsClass(Instance: TObject; Class_: TClass): TPointer;
function IsClass(Instance: TObject; Class_: TClass): TBoolean;
procedure AbstractError();
{$ENDIF}
implementation
{$IF Defined(CHARTA_PASCAL)}
uses
Exception,
Integer._32,
System.Class_.Exception,
Text,
Text.Collation,
Value.Manager;
function ClassCreate(Class_: TClass; Allocate: TBoolean): TObject;
begin
if Allocate then
begin
GetMem(TPointer(Result), Class_.InstanceSize);
FillChar(TPointer(Result)^, Class_.InstanceSize, 0);
Result.FClass := Class_;
end
else
Result := TObject(Class_);
end;
procedure ClassDestroy(Instance: TObject; Deallocate: TBoolean);
begin
if Deallocate then
Instance.FreeInstance();
end;
function AsClass(Instance: TObject; Class_: TClass): TPointer;
begin
if (Instance = nil) or (Instance is Class_) then
Result := Instance
else
raise EClassCast.Create('Cannot cast class ''' + Instance.ClassName + ''' to ''' + Class_.ClassName + '''');
end;
function IsClass(Instance: TObject; Class_: TClass): TBoolean;
begin
Result := (Instance <> nil) and Instance.Class_.InheritsFrom(Class_);
end;
procedure AbstractError();
begin
raise EAbstractMethodCalled.Create('Abstract method called');
end;
{ TObject }
procedure TObject.AfterConstruction();
begin
end;
procedure TObject.BeforeDestruction();
begin
end;
class function TObject.ClassParent(): TClass;
begin
Result := Superclass;
end;
function TObject.ClassType(): TClass;
begin
Result := Class_;
end;
procedure TObject.CleanupInstance();
var
Manager: TValueManager;
Class_: TClass;
begin
Class_ := Self.Class_;
while Class_ <> nil do
begin
Manager := Class_.Manager as TValueManager;
Manager.ReleaseReference(TPointer(Self)^);
Class_ := Class_.Superclass;
end;
end;
constructor TObject.Create();
begin
end;
destructor TObject.Destroy();
begin
end;
procedure TObject.Free();
begin
if Self <> nil then
Destroy();
end;
procedure TObject.FreeInstance();
begin
CleanupInstance();
FreeMem(TPointer(Self));
end;
class function TObject.GetMethodInformation(const Name: TAnsiString): PMethodInformation;
var
i: TInteger32;
MethodInformation: PMethodInformation;
NameText: TText; // TODO: We should in fact accept a Name: TText parameter
begin
Result := nil;
i := 0;
NameText := Name;
while (Result = nil) and (i < Length(Methods)) do
begin
MethodInformation := @Methods[i];
if TTextCollation.DefaultCaseInsensitive.Equal(NameText, MethodInformation.Name) then
Result := MethodInformation
else
Inc(i);
end;
if (Result = nil) and (Superclass <> nil) then
Result := Superclass.GetMethodInformation(Name);
end;
class function TObject.InheritsFrom(Class_: TClass): TBoolean;
var
Superclass: TClass;
begin
Result := False;
Superclass := Self;
while not Result and (Superclass <> nil) do
begin
Result := Superclass = Class_;
Superclass := Superclass.Superclass;
end;
end;
class function TObject.MethodAddress(const Name: TAnsiString): Pointer;
var
MethodInformation: PMethodInformation;
begin
MethodInformation := GetMethodInformation(Name);
if MethodInformation <> nil then
Result := MethodInformation.Address
else
Result := nil;
end;
{$ENDIF}
end.
|