-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathNeon.Core.Nullables.pas
239 lines (200 loc) · 7.32 KB
/
Neon.Core.Nullables.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
{******************************************************************************}
{ }
{ Neon: Serialization Library for Delphi }
{ Copyright (c) 2018 Paolo Rossi }
{ https://github.com/paolo-rossi/neon-library }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit Neon.Core.Nullables;
{$I Neon.inc}
interface
uses
System.SysUtils, System.Variants, System.Classes, System.Generics.Defaults, System.Rtti,
System.TypInfo, System.JSON;
type
ENullableException = class(Exception);
{$RTTI EXPLICIT FIELDS([vcPrivate]) METHODS([vcPrivate])}
Nullable<T> = record
private
FValue: T;
FHasValue: string;
procedure Clear;
function GetValueType: PTypeInfo;
function GetValue: T;
procedure SetValue(const AValue: T);
function GetHasValue: Boolean;
public
constructor Create(const Value: T); overload;
constructor Create(const Value: Variant); overload;
function Equals(const Value: Nullable<T>): Boolean; overload;
function Equals(const Value: T): Boolean; overload;
function GetValueOrDefault: T; overload;
function GetValueOrDefault(const Default: T): T; overload;
property HasValue: Boolean read GetHasValue;
function IsNull: Boolean;
property Value: T read GetValue;
class operator Implicit(const Value: Nullable<T>): T;
class operator Implicit(const Value: Nullable<T>): Variant;
class operator Implicit(const Value: Pointer): Nullable<T>;
class operator Implicit(const Value: T): Nullable<T>;
class operator Implicit(const Value: Variant): Nullable<T>;
class operator Implicit(const Value: TValue): Nullable<T>;
class operator Equal(const Left, Right: Nullable<T>): Boolean; overload;
class operator Equal(const Left: Nullable<T>; Right: T): Boolean; overload;
class operator Equal(const Left: T; Right: Nullable<T>): Boolean; overload;
class operator NotEqual(const Left, Right: Nullable<T>): Boolean; overload;
class operator NotEqual(const Left: Nullable<T>; Right: T): Boolean; overload;
class operator NotEqual(const Left: T; Right: Nullable<T>): Boolean; overload;
class operator GreaterThan(const Left: Nullable<T>; Right: T): Boolean; overload;
class operator LessThan(const Left: Nullable<T>; Right: T): Boolean; overload;
end;
NullString = Nullable<string>;
NullBoolean = Nullable<Boolean>;
NullInteger = Nullable<Integer>;
NullInt64 = Nullable<Int64>;
NullDouble = Nullable<Double>;
NullDateTime = Nullable<TDateTime>;
NullCurrency = Nullable<Currency>;
implementation
uses
Neon.Core.Utils;
{ Nullable<T> }
constructor Nullable<T>.Create(const Value: T);
begin
FValue := Value;
FHasValue := DefaultTrueBoolStr;
end;
constructor Nullable<T>.Create(const Value: Variant);
begin
if not VarIsNull(Value) and not VarIsEmpty(Value) then
Create(TValue.FromVariant(Value).AsType<T>)
else
Clear;
end;
procedure Nullable<T>.Clear;
begin
FValue := Default(T);
FHasValue := '';
end;
class operator Nullable<T>.Equal(const Left: Nullable<T>; Right: T): Boolean;
begin
Result := Left.Equals(Right);
end;
class operator Nullable<T>.Equal(const Left: T; Right: Nullable<T>): Boolean;
begin
Result := Right.Equals(Left);
end;
function Nullable<T>.Equals(const Value: T): Boolean;
begin
Result := HasValue and TEqualityComparer<T>.Default.Equals(Self.Value, Value)
end;
function Nullable<T>.Equals(const Value: Nullable<T>): Boolean;
begin
if HasValue and Value.HasValue then
Result := TEqualityComparer<T>.Default.Equals(Self.Value, Value.Value)
else
Result := HasValue = Value.HasValue;
end;
function Nullable<T>.GetHasValue: Boolean;
begin
Result := FHasValue <> '';
end;
function Nullable<T>.GetValueType: PTypeInfo;
begin
Result := TypeInfo(T);
end;
class operator Nullable<T>.GreaterThan(const Left: Nullable<T>; Right: T): Boolean;
begin
Result := Left.HasValue and (TComparer<T>.Default.Compare(Left.Value, Right) > 0);
end;
function Nullable<T>.GetValue: T;
begin
if not HasValue then
raise ENullableException.Create('Nullable type has no value');
Result := FValue;
end;
function Nullable<T>.GetValueOrDefault(const Default: T): T;
begin
if HasValue then
Result := FValue
else
Result := Default;
end;
function Nullable<T>.GetValueOrDefault: T;
begin
Result := GetValueOrDefault(Default(T));
end;
class operator Nullable<T>.Implicit(const Value: Nullable<T>): T;
begin
Result := Value.Value;
end;
class operator Nullable<T>.Implicit(const Value: Nullable<T>): Variant;
begin
if Value.HasValue then
Result := TValue.From<T>(Value.Value).AsVariant
else
Result := Null;
end;
class operator Nullable<T>.Implicit(const Value: Pointer): Nullable<T>;
begin
if Value = nil then
Result.Clear
else
Result := Nullable<T>.Create(T(Value^));
end;
class operator Nullable<T>.Implicit(const Value: T): Nullable<T>;
begin
Result := Nullable<T>.Create(Value);
end;
class operator Nullable<T>.Implicit(const Value: Variant): Nullable<T>;
begin
Result := Nullable<T>.Create(Value);
end;
function Nullable<T>.IsNull: Boolean;
begin
Result := FHasValue = '';
end;
class operator Nullable<T>.LessThan(const Left: Nullable<T>; Right: T): Boolean;
begin
Result := Left.HasValue and (TComparer<T>.Default.Compare(Left.Value, Right) < 0);
end;
class operator Nullable<T>.NotEqual(const Left: Nullable<T>; Right: T): Boolean;
begin
Result := not Left.Equals(Right);
end;
class operator Nullable<T>.NotEqual(const Left: T; Right: Nullable<T>): Boolean;
begin
Result := not Right.Equals(Left);
end;
class operator Nullable<T>.Equal(const Left, Right: Nullable<T>): Boolean;
begin
Result := Left.Equals(Right);
end;
class operator Nullable<T>.NotEqual(const Left, Right: Nullable<T>): Boolean;
begin
Result := not Left.Equals(Right);
end;
procedure Nullable<T>.SetValue(const AValue: T);
begin
FValue := AValue;
FHasValue := DefaultTrueBoolStr;
end;
class operator Nullable<T>.Implicit(const Value: TValue): Nullable<T>;
begin
Result := Nullable<T>.Create(Value.AsType<T>);
end;
end.