forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uContainerDataHandler.pas
525 lines (465 loc) · 15.8 KB
/
uContainerDataHandler.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
unit uContainerDataHandler;
interface
uses
iContainers;
const
vtSmallAnsiString = 255;
type
{$IFNDEF DELPHIXE}
// We must define NativeInt for platforms lesser than DelphiXE
// to make things even worse, in Delphi 2007 NativeInt is a 64 bits integer when on later
// versions this type mutates depending on the target platform x86 or x64
NativeInt = Integer;
NativeUInt = Cardinal;
{$ENDIF}
TSmallAnsiStringBuffer = array [0..sizeof(Extended) - 1] of byte;
PContainerData = ^TContainerData;
TContainerData = record
case VType : byte of
vtPointer, vtObject, vtInterface : (AsPointer : Pointer);
vtInteger : (AsInteger : integer);
vtString : (AsShortString : PShortString);
vtAnsiString : (AsAnsiString : PAnsiString);
vtInt64 : (AsInt64 : Int64);
vtExtended : (AsExtended : Extended);
vtCurrency : (AsCurrency : Currency);
vtBoolean : (AsBoolean : Boolean);
vtChar : (AsAnsiChar : AnsiChar);
vtWideChar : (AsWideChar : WideChar);
vtWideString : (AsWideString : PWideString);
vtSmallAnsiString : (AsBuffer : TSmallAnsiStringBuffer);
end;
TContainerDataHandler = class (TInterfacedObject, IDataHandler)
private
FAutoFreeObjects: Boolean;
procedure CheckContext(AContext: Pointer);
protected
procedure CleanData(AContext: Pointer); virtual;
function CheckType(AContext: Pointer; AType: Byte): Byte; virtual;
function GetAsAnsiChar(AContext: Pointer): AnsiChar; virtual;
function GetAsAnsiString(AContext: Pointer): AnsiString; virtual;
function GetAsBoolean(AContext: Pointer): Boolean; virtual;
function GetAsCurrency(AContext: Pointer): Currency; virtual;
function GetAsExtended(AContext: Pointer): Extended; virtual;
function GetAsInt64(AContext: Pointer): Int64; virtual;
function GetAsInteger(AContext: Pointer): integer; virtual;
function GetAsInterface(AContext: Pointer): IUnknown; virtual;
function GetAsObject(AContext: Pointer): TObject; virtual;
function GetAsPointer(AContext: Pointer): Pointer; virtual;
function GetAsShortString(AContext: Pointer): ShortString; virtual;
function GetAsWideChar(AContext: Pointer): WideChar; virtual;
function GetAsWideString(AContext: Pointer): WideString; virtual;
procedure SetAnsiChar(AContext: Pointer; Value : AnsiChar); virtual;
procedure SetAnsiString(AContext: Pointer; const Value : AnsiString); virtual;
procedure SetBoolean(AContext: Pointer; Value : Boolean); virtual;
procedure SetCurrency(AContext: Pointer; const Value : Currency); virtual;
procedure SetExtended(AContext: Pointer; const Value : Extended); virtual;
procedure SetInt64(AContext: Pointer; const Value : Int64); virtual;
procedure SetInteger(AContext: Pointer; Value : integer); virtual;
procedure SetInterface(AContext: Pointer; Value : IUnknown); virtual;
procedure SetObject(AContext: Pointer; Value : TObject); virtual;
procedure SetPointer(AContext: Pointer; Value : Pointer); virtual;
procedure SetShortString(AContext: Pointer; const Value : ShortString); virtual;
procedure SetWideChar(AContext: Pointer; Value : WideChar); virtual;
procedure SetWideString(AContext: Pointer; const Value : WideString); virtual;
function Compare(AContext : Pointer; Value : integer): integer; overload; virtual;
function Compare(AContext : Pointer; Value : Pointer): integer; overload; virtual;
function Compare(AContext : Pointer; Value : TObject): integer; overload; virtual;
function Compare(AContext : Pointer; Value : AnsiChar): integer; overload; virtual;
function Compare(AContext : Pointer; Value : Boolean): integer; overload; virtual;
function Compare(AContext : Pointer; const Value : Currency): integer; overload; virtual;
function Compare(AContext : Pointer; const Value : Extended): integer; overload; virtual;
function Compare(AContext : Pointer; const Value : Int64): integer; overload; virtual;
function Compare(AContext : Pointer; Value : IUnknown): integer; overload; virtual;
function Compare(AContext : Pointer; Value : WideChar): integer; overload; virtual;
function CompareAnsiString(AContext : Pointer; const Value : AnsiString): integer; virtual;
function CompareShortString(AContext: Pointer; const Value: ShortString): integer; virtual;
function CompareWideString(AContext : Pointer; const Value : WideString): integer; virtual;
function GetAutoFreeObjects: Boolean;
function GetType(AContext: Pointer): Byte; virtual;
procedure SetAutoFreeObjects(Value: Boolean);
end;
implementation
uses
SysUtils
{$IFDEF UNICODE},AnsiStrings{$ENDIF};
// START resource string wizard section
resourcestring
SAContextShouldBeNil = 'AContext should be <> nil';
STryingToAccessObjectPointedByCon = 'Trying to access object pointed by context using wrong access method';
// END resource string wizard section
procedure TContainerDataHandler.CheckContext(AContext: Pointer);
begin
if AContext = nil then
raise EContainer.Create (SAContextShouldBeNil);
end;
function TContainerDataHandler.CheckType(AContext: Pointer; AType: Byte): Byte;
begin
CheckContext (AContext);
if (PContainerData (AContext).VType <> AType) and
((PContainerData (AContext).VType <> vtSmallAnsiString) or (AType <> vtAnsiString)) and
((PContainerData (AContext).VType <> vtSmallAnsiString) or (AType <> vtString)) then
raise EContainer.Create (STryingToAccessObjectPointedByCon);
Result := PContainerData (AContext).VType;
end;
procedure TContainerDataHandler.CleanData(AContext: Pointer);
begin
with PContainerData (AContext)^ do
case VType of
vtAnsiString: AnsiString(AsAnsiString) := '';
vtWideString: WideString(AsWideString) := '';
vtString : Dispose (AsShortString);
vtInterface :
begin
if AsPointer <> nil then
IUnknown(AsPointer)._Release;
AsPointer := nil;
end;
vtObject : if FAutoFreeObjects then
TObject(AsPointer).Free;
end;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : integer):
integer;
begin
if GetAsInteger (AContext) > Value
then Result := 1
else if GetAsInteger (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : Pointer):
integer;
begin
if integer (GetAsPointer (AContext)) > integer (Value)
then Result := 1
else if GetAsPointer (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : TObject):
integer;
begin
if integer (GetAsObject (AContext)) > integer (Value)
then Result := 1
else if GetAsObject (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : AnsiChar):
integer;
begin
if GetAsAnsiChar (AContext) > Value
then Result := 1
else if GetAsAnsiChar (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : Boolean):
integer;
begin
if GetAsBoolean (AContext) > Value
then Result := 1
else if GetAsBoolean (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; const Value :
Currency): integer;
begin
if GetAsCurrency (AContext) > Value
then Result := 1
else if GetAsCurrency (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; const Value :
Extended): integer;
begin
if GetAsExtended (AContext) > Value
then Result := 1
else if GetAsExtended (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; const Value :
Int64): integer;
begin
if GetAsInt64(AContext) > Value
then Result := 1
else if GetAsInt64 (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : IUnknown):
integer;
begin
if NativeUInt(Pointer(GetAsInterface(AContext))) > NativeUInt(Pointer(Value))
then Result := 1
else if GetAsInterface (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.Compare(AContext : Pointer; Value : WideChar):
integer;
begin
if GetAsWideChar (AContext) > Value
then Result := 1
else if GetAsWideChar (AContext) = Value
then Result := 0
else Result := -1;
end;
function TContainerDataHandler.CompareAnsiString(AContext : Pointer; const
Value : AnsiString): integer;
begin
Result := {$IFDEF UNICODE}AnsiStrings.{$ENDIF}CompareStr (GetAsAnsiString (AContext), Value);
end;
function TContainerDataHandler.CompareShortString(AContext: Pointer; const
Value: ShortString): integer;
begin
Result := {$IFDEF UNICODE}AnsiStrings.{$ENDIF}CompareStr (GetAsShortString (AContext), Value);
end;
function TContainerDataHandler.CompareWideString(AContext : Pointer; const
Value : WideString): integer;
begin
Result := {$IFDEF UNICODE}SysUtils.{$ENDIF}CompareStr (GetAsWideString (AContext), Value);
end;
function TContainerDataHandler.GetAsAnsiChar(AContext: Pointer): AnsiChar;
begin
CheckType (AContext, vtChar);
Result := PContainerData (AContext).AsAnsiChar;
end;
function TContainerDataHandler.GetAsAnsiString(AContext: Pointer): AnsiString;
begin
case CheckType (AContext, vtAnsiString) of
vtAnsiString : Result := AnsiString (PContainerData (AContext).AsAnsiString);
vtSmallAnsiString :
begin
SetLength (Result, PContainerData (AContext).AsBuffer[0]);
move (PContainerData (AContext).AsBuffer[1], PAnsiChar(Result)^, PContainerData (AContext).AsBuffer[0]);
end;
end;
end;
function TContainerDataHandler.GetAsBoolean(AContext: Pointer): Boolean;
begin
CheckType (AContext, vtBoolean);
Result := PContainerData (AContext).AsBoolean;
end;
function TContainerDataHandler.GetAsCurrency(AContext: Pointer): Currency;
begin
CheckType (AContext, vtCurrency);
Result := PContainerData (AContext).AsCurrency;
end;
function TContainerDataHandler.GetAsExtended(AContext: Pointer): Extended;
begin
CheckType (AContext, vtExtended);
Result := PContainerData (AContext).AsExtended;
end;
function TContainerDataHandler.GetAsInt64(AContext: Pointer): Int64;
begin
CheckType (AContext, vtInt64);
Result := PContainerData (AContext).AsInt64;
end;
function TContainerDataHandler.GetAsInteger(AContext: Pointer): integer;
begin
CheckType (AContext, vtInteger);
Result := PContainerData (AContext).AsInteger;
end;
function TContainerDataHandler.GetAsInterface(AContext: Pointer): IUnknown;
begin
CheckType (AContext, vtInterface);
Result := IUnknown (PContainerData (AContext).AsPointer);
end;
function TContainerDataHandler.GetAsObject(AContext: Pointer): TObject;
begin
CheckType (AContext, vtObject);
Result := PContainerData (AContext).AsPointer;
end;
function TContainerDataHandler.GetAsPointer(AContext: Pointer): Pointer;
begin
CheckType (AContext, vtPointer);
Result := PContainerData (AContext).AsPointer;
end;
function TContainerDataHandler.GetAsShortString(AContext: Pointer): ShortString;
begin
case CheckType (AContext, vtString) of
vtString : Result := PContainerData (AContext).AsShortString^;
vtSmallAnsiString :
begin
SetLength (Result, PContainerData (AContext).AsBuffer[0]);
move (PContainerData (AContext).AsBuffer[1], Result[1], PContainerData (AContext).AsBuffer[0]);
end;
end;
end;
function TContainerDataHandler.GetAsWideChar(AContext: Pointer): WideChar;
begin
CheckType (AContext, vtWideChar);
Result := PContainerData (AContext).AsWideChar;
end;
function TContainerDataHandler.GetAsWideString(AContext: Pointer): WideString;
begin
CheckType (AContext, vtWideString);
Result := WideString (PContainerData (AContext).AsWideString);
end;
function TContainerDataHandler.GetAutoFreeObjects: Boolean;
begin
Result := FAutoFreeObjects;
end;
function TContainerDataHandler.GetType(AContext: Pointer): Byte;
begin
CheckContext (AContext);
Result := PContainerData (AContext).VType;
end;
procedure TContainerDataHandler.SetAnsiChar(AContext: Pointer; Value :
AnsiChar);
begin
with PContainerData (AContext)^ do
begin
VType := vtChar;
AsAnsiChar := Value;
end;
end;
procedure TContainerDataHandler.SetAnsiString(AContext: Pointer; const Value :
AnsiString);
var
Len : integer;
begin
Len := length (Value);
with PContainerData (AContext)^ do
if Len < sizeof (TSmallAnsiStringBuffer)
then
begin
VType := vtSmallAnsiString;
move (PAnsiChar (Value)^, AsBuffer[1], Len);
AsBuffer[0] := Len;
end
else
begin
VType := vtAnsiString;
AsAnsiString := nil;
AnsiString(AsAnsiString) := Value;
UniqueString(AnsiString(AsAnsiString));
end;
end;
procedure TContainerDataHandler.SetAutoFreeObjects(Value: Boolean);
begin
FAutoFreeObjects := Value;
end;
procedure TContainerDataHandler.SetBoolean(AContext: Pointer; Value : Boolean);
begin
with PContainerData (AContext)^ do
begin
VType := vtBoolean;
AsBoolean := Value;
end;
end;
procedure TContainerDataHandler.SetCurrency(AContext: Pointer; const Value :
Currency);
begin
with PContainerData (AContext)^ do
begin
VType := vtCurrency;
AsCurrency := Value;
end;
end;
procedure TContainerDataHandler.SetExtended(AContext: Pointer; const Value :
Extended);
begin
with PContainerData (AContext)^ do
begin
VType := vtExtended;
AsExtended := Value;
end;
end;
procedure TContainerDataHandler.SetInt64(AContext: Pointer; const Value :
Int64);
begin
with PContainerData (AContext)^ do
begin
VType := vtInt64;
AsInt64 := Value;
end;
end;
procedure TContainerDataHandler.SetInteger(AContext: Pointer; Value : integer);
begin
with PContainerData (AContext)^ do
begin
VType := vtInteger;
AsInteger := Value;
end;
end;
procedure TContainerDataHandler.SetInterface(AContext: Pointer; Value :
IUnknown);
begin
with PContainerData (AContext)^ do
begin
VType := vtInterface;
if AsPointer <> nil then
IUnknown(AsPointer)._Release;
AsPointer := Pointer(Value);
if AsPointer <> nil then
IUnknown(AsPointer)._AddRef;
end;
end;
procedure TContainerDataHandler.SetObject(AContext: Pointer; Value : TObject);
begin
with PContainerData (AContext)^ do
begin
VType := vtObject;
AsPointer := Value;
end;
end;
procedure TContainerDataHandler.SetPointer(AContext: Pointer; Value : Pointer);
begin
with PContainerData (AContext)^ do
begin
VType := vtPointer;
AsPointer := Value;
end;
end;
procedure TContainerDataHandler.SetShortString(AContext: Pointer; const Value :
ShortString);
var
Len : integer;
begin
Len := length (Value);
with PContainerData (AContext)^ do
if Len < sizeof (TSmallAnsiStringBuffer)
then
begin
VType := vtSmallAnsiString;
move (Value[1], AsBuffer[1], Len);
AsBuffer[0] := Len;
end
else
begin
VType := vtString;
New (AsShortString);
PShortString (AsShortString)^ := Value;
end;
end;
procedure TContainerDataHandler.SetWideChar(AContext: Pointer; Value :
WideChar);
begin
with PContainerData (AContext)^ do
begin
VType := vtWideChar;
AsWideChar := Value;
end;
end;
procedure TContainerDataHandler.SetWideString(AContext: Pointer; const Value :
WideString);
begin
{$IFDEF VER130}
raise Exception.Create ('WideString not yet supported on TContainerDataHandler');
{$ELSE}
with PContainerData (AContext)^ do
begin
VType := vtWideString;
AsWideString := nil;
WideString (AsWideString) := Value;
UniqueString (WideString (AsWideString));
end;
{$ENDIF}
end;
end.