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
| ///////////////////////////////////////////////////////////////////////////
// //
// 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 Type_;
interface
uses
Boolean,
Collection.List,
Comparator,
StructuredDocument.Node,
Value;
type
TStorageType = class;
TType = class(TValue)
protected
function IsValueSubtype(Type_: TType): TBoolean;
public
function GetStorageType(): TStorageType; virtual; abstract;
function GetDecoratedType(): TType; virtual;
function GetCaption(): TDocumentNode; virtual;
function IsNullable(): TBoolean; virtual;
function IsNumeric(): TBoolean; virtual;
function CheckValue(Messages: TList<TDocumentNode>; Value: TValue): TBoolean; virtual;
function IsValidValue(Messages: TList<TDocumentNode>; Value: TValue): TBoolean; overload;
function IsValidValue(Value: TValue): TBoolean; overload;
function GetValueComparator(): TComparator<TValue>; virtual;
function IsEquivalentTo(OtherType: TType): TBoolean; virtual;
function IsSupertypeOf(OtherType: TType): TBoolean; virtual;
function OverlapsWith(OtherType: TType): TBoolean; virtual;
function CommonType(OtherType: TType): TType; virtual;
function SharesTypePath(OtherType: TType): TBoolean;
function IsProperSupertypeOf(OtherType: TType): TBoolean;
function IsSubtypeOf(OtherType: TType): TBoolean;
function IsProperSubtypeOf(OtherType: TType): TBoolean;
function Optional(): TType;
end;
// TODO: TStorageType should be replaced by something different. Storage types are dependent on the system that stores the
// values and values of a certain type probably can be stored in many different ways. Indicating one specific storage
// type with the type itself is both limited and cumbersome.
TStorageType = class(TType);
TUnstoredType = class(TStorageType)
public
function GetStorageType(): TStorageType; override;
end;
implementation
uses
Object_.List,
Object_.Reference.Counter,
StructuredDocument.Node.Text,
Type_.Nullable,
Type_.Value;
{ TType }
function TType.CheckValue(Messages: TList<TDocumentNode>; Value: TValue): TBoolean;
begin
Result := False;
end;
function TType.CommonType(OtherType: TType): TType;
begin
// TODO: This is not correct, we should provide a real implementation
Result := Self;
if (Result is TValueType) and not (OtherType is TValueType) then
Result := OtherType;
end;
function TType.GetValueComparator(): TComparator<TValue>;
begin
Result := TValue.TCompare.Instance;
end;
function TType.GetCaption(): TDocumentNode;
begin
Result := TDocumentText.Create(ToText());
end;
function TType.GetDecoratedType(): TType;
begin
Result := Self;
end;
function TType.IsEquivalentTo(OtherType: TType): TBoolean;
begin
Result := Self = OtherType;
end;
function TType.IsNullable(): TBoolean;
begin
Result := False;
end;
function TType.IsNumeric(): TBoolean;
begin
Result := False;
end;
function TType.IsProperSubtypeOf(OtherType: TType): TBoolean;
begin
Result := OtherType.IsSupertypeOf(Self) and not IsEquivalentTo(OtherType);
end;
function TType.IsProperSupertypeOf(OtherType: TType): TBoolean;
begin
Result := IsSupertypeOf(OtherType) and not IsEquivalentTo(OtherType);
end;
function TType.IsSubtypeOf(OtherType: TType): TBoolean;
begin
Result := OtherType.IsSupertypeOf(Self);
end;
function TType.IsSupertypeOf(OtherType: TType): TBoolean;
begin
Result := IsEquivalentTo(OtherType) or IsValueSubtype(OtherType);
end;
function TType.IsValidValue(Value: TValue): TBoolean;
var
Messages: TList<TDocumentNode>;
begin
Messages := TObjectList<TDocumentNode>.Create();
Messages.ElementManager := TReferenceCounter<TDocumentNode>.Create();
Result := CheckValue(Messages, Value);
Messages.Free();
end;
function TType.IsValidValue(Messages: TList<TDocumentNode>; Value: TValue): TBoolean;
begin
Result := CheckValue(Messages, Value);
end;
function TType.IsValueSubtype(Type_: TType): TBoolean;
begin
Result := (Type_ is TValueType) and IsValidValue((Type_ as TValueType).Value);
end;
function TType.Optional(): TType;
begin
Result := TNullableType.Create(Self);
end;
function TType.OverlapsWith(OtherType: TType): TBoolean;
begin
Result := SharesTypePath(OtherType);
end;
function TType.SharesTypePath(OtherType: TType): TBoolean;
begin
Result := IsSupertypeOf(OtherType) or OtherType.IsSupertypeOf(Self);
end;
{ TUnstoredType }
function TUnstoredType.GetStorageType(): TStorageType;
begin
Result := Self;
end;
end.
|