forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cromis.AnyValue.pas
4150 lines (3653 loc) · 117 KB
/
Cromis.AnyValue.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* This software is distributed under BSD license.
*
* Copyright (c) 2006-2010 Iztok Kacin, Cromis (iztok.kacin@gmail.com).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of the Iztok Kacin nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ==================================================================================
* Common unit for Cromis library
* ==================================================================================
* 08/03/2010 (1.0.0)
* - Initial release
* - IAnyValue, TAnyValue implementations
* ==================================================================================
* 26/07/2010 (1.0.1)
* - Added avtDateTime
* ==================================================================================
* 15/08/2010 (1.0.2)
* - Added Items property to enable iteration over all values
* - Added Count property
* ==================================================================================
* 08/10/2010 (1.0.3)
* - Use generic list instead of interface list for newer compilers
* ==================================================================================
* 28/12/2010 (1.0.4)
* - Create new interface object for each assignment to avoid interface sharing
* ==================================================================================
* 13/06/2011 (1.0.5)
* - Added AnsiString support
* ==================================================================================
* 16/06/2011 (1.1.0)
* - TAnyValue detached from interfaces. Uses Variants as internal storage
* - AsPointer, AsVariant properties added to TAnyValue and IAnyValue
* ==================================================================================
* 17/06/2011 (1.1.1)
* - IAnyValue uses TAnyValue now internally
* ==================================================================================
* 21/06/2011 (1.2.0)
* - TAnyValue uses TVarRec for simple data types and Variants for complex ones
* ==================================================================================
* 23/06/2011 (1.3.0)
* - TAnyValue uses TVarRec for simple data types and array of byes for complex ones
* ==================================================================================
* 03/02/2013 (1.4.0)
* - Compiler defines to finetune speed vs memory consumption
* ==================================================================================
* 05/02/2013 (1.4.1)
* - Controlled type conversions
* - Redesigned defines to control memory consumption
* ==================================================================================
* 06/02/2013 (1.4.2)
* - Speed optimizations
* ==================================================================================
* 07/02/2013 (1.4.3)
* - Speed optimizations
* ==================================================================================
* 17/02/2013 (2.0.0)
* - Complete rewrite, smaller memory footprint, faster speed
* - Array, Variants and Exception support
* ==================================================================================
* 18/02/2013 (2.0.1)
* - TAnyArray implementation added
* - Array enumerator
* ==================================================================================
* 20/02/2013 (2.0.2)
* - TAnyArray gets streams support
* - More casts added for TAnyValue
* ==================================================================================
* 23/02/2013 (2.0.3)
* - TAnyValue gets name-value pairs support
* ==================================================================================
* 27/02/2013 (2.1.0)
* - TAnyArray uses sliced arrays for data structure
* - LoadFromStream / SaveToStream can now take callback procedure if needed
* ==================================================================================
* 10/03/2013 (2.2.0)
* - IAnyArray sliced array rewritten to be more efficient
* ==================================================================================
* 08/07/2013 (2.2.1)
* - Fixed bug in sorting algorithm
* - Additional CreateAnyArray overload
* ==================================================================================
* 12/09/2013 (2.3.0)
* - "AnyValue_HookingOn" and "AnyValue_HookingOff" defines added
* - Under x64 automatically swithces to safe mode. Manual control over hooking
* ==================================================================================
*)
unit Cromis.AnyValue;
interface
{$IFDEF CPUX64}
{$IFNDEF AnyValue_HookingOn}
{$DEFINE AnyValue_HookingOff}
{$ENDIF}
{$ENDIF}
{$IFDEF AnyValue_HookingOn}
{$UNDEF AnyValue_HookingOff}
{$ENDIF}
uses
Windows, SysUtils, Classes, TypInfo, Variants, Math,
// cromis units
{$IFNDEF AnyValue_HookingOff}Cromis.Detours,{$ENDIF}Cromis.Unicode;
const
atInteger = 0;
atBoolean = 1;
atChar = 2;
atExtended = 3;
atString = 4;
atPointer = 5;
atPChar = 6;
atObject = 7;
atClass = 8;
atWideChar = 9;
atPWideChar = 10;
atAnsiString = 11;
atCurrency = 12;
atVariant = 13;
atInterface = 14;
atWideString = 15;
atInt64 = 16;
atUnicodeString = 17;
atCardinal = 18;
atDouble = 19;
const
cDynArrayGrowthFactor = 1.6180339887498948482045868343656;
cDefArraySliceSize = 5000;
cMinSliceDataSize = 100;
cSliceBufferMpl = 1.1;
type
TValueType =
(
avtNone,
avtBoolean,
avtInteger,
avtInt64,
avtCardinal,
avtFloat,
avtString,
avtObject,
avtPointer,
avtInterface,
avtAnsiString,
avtWideString,
avtDateTime,
avtDouble,
avtArray,
avtVariant,
avtException,
avtNamedValue
);
PValueData = ^TValueData;
TValueData = record { do not pack this record; it is compiler-generated }
case Byte of
atCardinal: (VCardinal: Cardinal);
atInteger: (VInteger: Integer);
atBoolean: (VBoolean: Boolean);
atPointer: (VPointer: Pointer);
atObject: (VObject: TObject);
atInt64: (VInt64: Int64);
atDouble: (VDouble: Double);
end;
// value data type depends on the SafeMode define
TValueDataType = {$IFDEF AnyValue_HookingOff}TValueData{$ELSE}IInterface{$ENDIF};
// predeclare the enumerators
IAnyArrayEnumerator = Interface;
IAnyArrayEnumerable = Interface;
IAnyArrayEnumerate = Interface;
// predeclare the array
IAnyArray = Interface;
// pointers to records
PNamedValue = ^TNamedValue;
PAnyValue = ^TAnyValue;
TAnyValue = packed record
private
ValueData: TValueDataType;
{$IFDEF AnyValue_HookingOff}
IntfData : IInterface;
{$ELSE}
{$IFNDEF CPUX64}
Padding : array [0..3] of Byte;
{$ENDIF}
{$ENDIF}
ValueType: TValueType;
function GetAsInt64: Int64; inline;
function GetAsFloat: Extended; inline;
function GetAsDouble: Double; inline;
function GetAsString: string; inline;
function GetAsObject: TObject; inline;
function GetAsBoolean: Boolean; inline;
function GetAsInteger: Integer; inline;
function GetAsPointer: Pointer; inline;
function GetAsVariant: Variant; inline;
function GetAsCardinal: Cardinal; inline;
function GetAsDateTime: TDateTime; inline;
function GetAsException: Exception; inline;
function GetAsInterface: IInterface; inline;
function GetAsWideString: WideString; inline;
function GetAsNamedValue: PNamedValue; inline;
function GetAsArrayItem(const Idx: Integer): TAnyValue; overload;
function GetAsArrayItem(const Name: string): TAnyValue; overload;
// complex non inline getters
function GetAsInt64WithCast: Int64;
function GetAsFloatWithCast: Extended;
function GetAsStringWithCast: string;
function GetAsDoubleWithCast: Double;
function GetAsObjectWithCast: TObject;
function GetAsBooleanWithCast: Boolean;
function GetAsIntegerWithCast: Integer;
function GetAsPointerWithCast: Pointer;
function GetAsVariantWithCast: Variant;
function GetAsCardinalWithCast: Cardinal;
function GetAsDateTimeWithCast: TDateTime;
function GetAsExceptionWithCast: Exception;
function GetAsInterfaceWithCast: IInterface;
function GetAsWideStringWithCast: WideString;
{$IFDEF UNICODE}
function GetAsAnsiString: AnsiString; inline;
function GetAsAnsiStringWithCast: AnsiString;
{$ENDIF}
procedure SetAsInt64(const Value: Int64); inline;
procedure SetAsFloat(const Value: Extended); inline;
procedure SetAsDouble(const Value: Double); inline;
procedure SetAsString(const Value: string); inline;
procedure SetAsObject(const Value: TObject); inline;
procedure SetAsBoolean(const Value: Boolean); inline;
procedure SetAsInteger(const Value: Integer); inline;
procedure SetAsPointer(const Value: Pointer); inline;
procedure SetAsVariant(const Value: Variant); inline;
procedure SetAsCardinal(const Value: Cardinal); inline;
procedure SetAsDateTime(const Value: TDateTime); inline;
procedure SetAsException(const Value: Exception); inline;
procedure SetAsInterface(const Value: IInterface); inline;
procedure SetAsWideString(const Value: WideString); inline;
procedure SetAsNamedValue(const Name: string; const Value: TAnyValue);
procedure SetAsArrayItem(const Idx: Integer; const Value: TAnyValue); overload; inline;
procedure SetAsArrayItem(const Name: string; const Value: TAnyValue); overload; inline;
{$IFDEF UNICODE}
procedure SetAsAnsiString(const Value: AnsiString); inline;
{$ENDIF}
class procedure RemoveWarnings; inline; static;
public
procedure Clear;
function IsNil: Boolean; inline;
function IsEmpty: Boolean; inline;
function ValueSize: Integer; inline;
function GetAsArray: IAnyArray; inline;
function EnsureAsArray: IAnyArray;
function GetValueType: TValueType; inline;
function Enum: IAnyArrayEnumerate; inline;
class function Null: TAnyValue; static; inline;
procedure Assign(const Value: PAnyValue); inline;
function Equal(const Value: TAnyValue): Boolean; overload; inline;
function Equal(const Value: PAnyValue): Boolean; overload; inline;
class operator Implicit(const Value: Int64): TAnyValue;
class operator Implicit(const Value: Boolean): TAnyValue;
class operator Implicit(const Value: Variant): TAnyValue;
class operator Implicit(const Value: Cardinal): TAnyValue;
class operator Implicit(const Value: Extended): TAnyValue;
class operator Implicit(const Value: Double): TAnyValue;
class operator Implicit(const Value: Integer): TAnyValue;
class operator Implicit(const Value: string): TAnyValue;
class operator Implicit(const Value: Exception): TAnyValue;
class operator Implicit(const Value: IInterface): TAnyValue;
class operator Implicit(const Value: WideString): TAnyValue;
{$IFDEF UNICODE}
class operator Implicit(const Value: AnsiString): TAnyValue;
{$ENDIF}
class operator Implicit(const Value: Pointer): TAnyValue;
class operator Implicit(const Value: TObject): TAnyValue;
class operator Implicit(const Value: TDateTime): TAnyValue;
class operator Implicit(const Value: array of TAnyValue): TAnyValue;
class operator Implicit(const Value: TAnyValue): Int64; inline;
class operator Implicit(const Value: TAnyValue): Double; inline;
class operator Implicit(const Value: TAnyValue): Variant; inline;
class operator Implicit(const Value: TAnyValue): Cardinal; inline;
class operator Implicit(const Value: TAnyValue): Extended; inline;
class operator Implicit(const Value: TAnyValue): TObject; inline;
class operator Implicit(const Value: TAnyValue): string; inline;
class operator Implicit(const Value: TAnyValue): Integer; inline;
class operator Implicit(const Value: TAnyValue): Exception; inline;
class operator Implicit(const Value: TAnyValue): WideString; inline;
{$IFDEF UNICODE}
class operator Implicit(const Value: TAnyValue): AnsiString; inline;
{$ENDIF}
class operator Implicit(const Value: TAnyValue): Boolean; inline;
class operator Implicit(const Value: TAnyValue): Pointer; inline;
class operator Implicit(const Value: TAnyValue): TDateTime; inline;
class operator Implicit(const Value: TAnyValue): IInterface; inline;
property AsInt64: Int64 read GetAsInt64 write SetAsInt64;
property AsFloat: Extended read GetAsFloat write SetAsFloat;
property AsDouble: Double read GetAsDouble write SetAsDouble;
property AsString: string read GetAsString write SetAsString;
property AsObject: TObject read GetAsObject write SetAsObject;
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsInteger: Integer read GetAsInteger write SetAsInteger;
property AsPointer: Pointer read GetAsPointer write SetAsPointer;
property AsVariant: Variant read GetAsVariant write SetAsVariant;
property AsCardinal: Cardinal read GetAsCardinal write SetAsCardinal;
property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
property AsException: Exception read GetAsException write SetAsException;
property AsInterface: IInterface read GetAsInterface write SetAsInterface;
property AsWideString: WideString read GetAsWideString write SetAsWideString;
property AsArrayItem[const Idx: Integer]: TAnyValue read GetAsArrayItem write SetAsArrayItem; default;
property AsArrayItem[const Name: string]: TAnyValue read GetAsArrayItem write SetAsArrayItem; default;
{$IFDEF UNICODE}
property AsAnsiString: AnsiString read GetAsAnsiString write SetAsAnsiString;
{$ENDIF}
end;
// declare the array of TAnyValue
TArrayMode = (amSlicedArray, amDynamicArray);
TAnyValues = array of TAnyValue;
PArraySlice = ^TArraySlice;
PAnyValues = ^TAnyValues;
TAnyValuesLoadCallback = procedure(const Stream: TStream;
const ValueType: TValueType;
const Value: PAnyValue;
var Handled: Boolean);
TAnyValuesSaveCallback = procedure(const Stream: TStream;
const Value: PAnyValue;
var Handled: Boolean);
TAnyValuesCompareCallback = function(Item1, Item2: PAnyValue): Integer;
{$IF CompilerVersion >= 20}
TAnyValuesLoadFunc = reference to procedure(const Stream: TStream;
const ValueType: TValueType;
const Value: PAnyValue;
var Handled: Boolean);
TAnyValuesSaveFunc = reference to procedure(const Stream: TStream;
const Value: PAnyValue;
var Handled: Boolean);
TAnyValuesCompareFunc = reference to function(Item1, Item2: PAnyValue): Integer;
{$IFEND}
TAnyValuesLoadHandler = {$IF CompilerVersion >= 20}TAnyValuesLoadFunc{$ELSE}TAnyValuesLoadCallback{$IFEND};
TAnyValuesSaveHandler = {$IF CompilerVersion >= 20}TAnyValuesSaveFunc{$ELSE}TAnyValuesSaveCallback{$IFEND};
TAnyValuesCompare = {$IF CompilerVersion >= 20}TAnyValuesCompareFunc{$ELSE}TAnyValuesCompareCallback{$IFEND};
TNamedValue = packed record
Name: string;
Value: TAnyValue;
end;
TArraySlice = record
Last: Integer;
Start: Integer;
Index: Integer;
Data: TAnyValues;
end;
// array of slices
PSliceData = ^TSliceData;
TSliceData = array of PArraySlice;
{$IFDEF AnyValue_HookingOff}
IAnyValueStringData = interface
['{EEB17601-043D-4409-80A0-67C5133FB510}']
function GetValue: string;
procedure SetValue(const value: string);
property Value: string read GetValue write SetValue;
end; { IAnyValueStringData }
TAnyValueStringData = class(TInterfacedObject, IAnyValueStringData)
strict private
FValue: string;
public
constructor Create(const Value: string);
function GetValue: string;
procedure SetValue(const Value: string);
property Value: string read GetValue write SetValue;
end; { TAnyValueStringData }
IAnyValueWideStringData = interface
['{7368C7E5-FB8A-45B7-8FBB-D1C19287A0C9}']
function GetValue: WideString;
procedure SetValue(const value: WideString);
property Value: WideString read GetValue write SetValue;
end; { IAnyValueWideStringData }
TAnyValueWideStringData = class(TInterfacedObject, IAnyValueWideStringData)
strict private
FValue: WideString;
public
constructor Create(const Value: WideString);
function GetValue: WideString;
procedure SetValue(const Value: WideString);
property Value: WideString read GetValue write SetValue;
end; { TAnyValueWideStringData }
{$IFDEF UNICODE}
IAnyValueAnsiStringData = interface
['{DD94000B-0B09-460E-883D-1318974A7381}']
function GetValue: AnsiString;
procedure SetValue(const value: AnsiString);
property Value: AnsiString read GetValue write SetValue;
end; { IAnyValueAnsiStringData }
TAnyValueAnsiStringData = class(TInterfacedObject, IAnyValueAnsiStringData)
strict private
FValue: AnsiString;
public
constructor Create(const Value: AnsiString);
function GetValue: AnsiString;
procedure SetValue(const Value: AnsiString);
property Value: AnsiString read GetValue write SetValue;
end; { TAnyValueAnsiStringData }
{$ENDIF}
IAnyValueExtendedData = interface
['{AE431567-96EB-4BF6-BAF1-7F8DF9C73711}']
function GetValue: Extended;
procedure SetValue(const value: Extended);
property Value: Extended read GetValue write SetValue;
end; { IOmniExtendedData }
TAnyValueExtendedData = class(TInterfacedObject, IAnyValueExtendedData)
strict private
FValue: Extended;
public
constructor Create(const Value: Extended);
function GetValue: Extended;
procedure SetValue(const Value: Extended);
property Value: Extended read GetValue write SetValue;
end; { TOmniExtendedData }
IAnyValueVariantData = interface
['{C36013E8-ED17-4AB0-96F6-2B181D129D6B}']
function GetValue: Variant;
procedure SetValue(const Value: Variant);
property Value: Variant read GetValue write SetValue;
end; { IAnyValueVariantData }
TAnyValueVariantData = class(TInterfacedObject, IAnyValueVariantData)
strict private
FValue: Variant;
public
constructor Create(const Value: Variant);
function GetValue: Variant;
procedure SetValue(const Value: Variant);
property Value: Variant read GetValue write SetValue;
end; { TAnyValueVariantData }
IAnyValueNamedData = interface
['{5E97C74F-DB95-4E5B-B33D-5E9DDF72DBA6}']
function GetValue: TNamedValue;
function GetAsPNamedValue: PNamedValue;
procedure SetValue(const Value: TNamedValue);
property Value: TNamedValue read GetValue write SetValue;
end; { IAnyValueVariantData }
TAnyValueNamedData = class(TInterfacedObject, IAnyValueNamedData)
strict private
FValue: TNamedValue;
public
function GetValue: TNamedValue;
function GetAsPNamedValue: PNamedValue;
procedure SetValue(const Value: TNamedValue);
property Value: TNamedValue read GetValue write SetValue;
end; { TAnyValueNamedData }
{$ENDIF}
IAnyArray = Interface(IInterface)
['{3E194356-2097-419F-A7A7-B388C2C404C1}']
function GetCount: Integer;
function GetValues: TAnyValues;
function GetSliceSize: Integer;
function GetArrayMode: TArrayMode;
function GetSliceCount: Integer;
function GetSliceBufferMpl: Double;
function GetItem(const Idx: Integer): TAnyValue;
procedure SetItem(const Idx: Integer; const Value: TAnyValue);
procedure SetSliceBufferMpl(const Value: Double);
procedure SetArrayMode(const Value: TArrayMode);
procedure SetSliceCount(const Value: Integer);
procedure SetSliceSize(const Value: Integer);
procedure Grow;
procedure Clear;
procedure Reverse;
function Pop: TAnyValue;
function Shift: TAnyValue;
function Clone: IAnyArray;
function PushNull: PAnyValue;
function RawData: PSliceData;
procedure Assign(const AnyArray: IAnyArray);
procedure SaveToStream(const Stream: TStream; const SaveCallback: TAnyValuesSaveCallback = nil); overload;
procedure LoadFromStream(const Stream: TStream; const LoadCallback: TAnyValuesLoadCallback = nil); overload;
{$IF CompilerVersion >= 20}
procedure SaveToStream(const Stream: TStream; const SaveFunc: TAnyValuesSaveFunc); overload;
procedure LoadFromStream(const Stream: TStream; const LoadFunc: TAnyValuesLoadFunc); overload;
{$IFEND}
procedure Exchange(const Index1, Index2: Integer);
function Equal(const Value: IAnyArray): Boolean;
procedure Push(const Value: TAnyValue); overload;
procedure Push(const Value: array of TAnyValue); overload;
procedure Push(const Value: string; const Delimiter: Char); overload;
procedure Unshift(const Value: TAnyValue); overload;
procedure Unshift(const Value: array of TAnyValue); overload;
procedure Unshift(const Value: string; const Delimiter: Char); overload;
procedure AddNamed(const Name: string; const Value: TAnyValue);
procedure DeleteIndex(const Index: Integer); overload;
procedure DeleteIndex(const Index: Integer; const Elements: Integer); overload;
procedure DeleteValue(const Value: TAnyValue; const AllInstances: Boolean = True); overload;
procedure DeleteValue(const Value: array of TAnyValue; const AllInstances: Boolean = True); overload;
procedure Insert(const Index: Integer; const Value: string; const Delimiter: Char); overload;
procedure Insert(const Index: Integer; const Value: array of TAnyValue); overload;
procedure Insert(const Index: Integer; const Value: TAnyValue); overload;
procedure Sort(const Compare: TAnyValuesCompareCallback); overload;
{$IF CompilerVersion >= 20}
procedure Sort(const Compare: TAnyValuesCompareFunc); overload;
{$IFEND}
function Last: TAnyValue;
function First: TAnyValue;
function Enum: IAnyArrayEnumerate;
function RawItem(const Index: Integer): PAnyValue;
function IndexOf(const Value: TAnyValue): Integer;
function IndexOfNamed(const Name: string): Integer;
function LastIndexOf(const Value: TAnyValue): Integer;
function FindNamed(const Name: string): TAnyValue;
function Contains(const Value: TAnyValue): Boolean;
function GetAsString(const Delimiter: Char = ','): string;
function Slice(Start: Integer; Stop: Integer = -1): IAnyArray;
property Item[const Idx: Integer]: TAnyValue read GetItem write SetItem; default;
property SliceBufferMpl: Double read GetSliceBufferMpl write SetSliceBufferMpl;
property ArrayMode: TArrayMode read GetArrayMode write SetArrayMode;
property SliceCount: Integer read GetSliceCount write SetSliceCount;
property SliceSize: Integer read GetSliceSize write SetSliceSize;
property Values: TAnyValues read GetValues;
property Count: Integer read GetCount;
end;
IAnyArrayEnumerator = Interface(IInterface)
['{C87850CB-3FBB-45C5-967A-6FA389BCCBDC}']
// getters and setters
function _GetCurrent: PAnyValue;
// iterator function and procedures
function MoveNext: Boolean;
property Current: PAnyValue read _GetCurrent;
end;
IAnyArrayEnumerable = Interface(IInterface)
['{3F2CB92A-847A-4692-B314-E3A9CEF66F4D}']
function GetEnumerator: IAnyArrayEnumerator;
end;
IAnyArrayEnumerate = Interface(IInterface)
['{E3AFE3FC-6EED-40D5-831F-C1E90EB0404E}']
function Forward: IAnyArrayEnumerable;
function Reverse: IAnyArrayEnumerable;
end;
IAnyValue = Interface(IInterface)
['{9D866D8B-6FEC-4633-B968-AF8677AF6B40}']
function GetName: string;
function GetAsInt64: Int64;
function GetAsFloat: Extended;
function GetAsDouble: Double;
function GetAsString: string;
function GetAsObject: TObject;
function GetAsInteger: Integer;
function ValueType: TValueType;
function GetAsBoolean: Boolean;
function GetAsPointer: Pointer;
function GetAsCardinal: Cardinal;
function GetAsDateTime: TDateTime;
function GetAsException: Exception;
function GetAsInterface: IInterface;
{$IFDEF UNICODE}
function GetAsAnsiString: AnsiString;
{$ENDIF}
function GetAsWideString: WideString;
procedure SetName(const Value: string);
procedure SetAsString(const Value: string);
procedure SetAsInt64(const Value: Int64);
procedure SetAsFloat(const Value: Extended);
procedure SetAsDouble(const Value: Double);
procedure SetAsObject(const Value: TObject);
procedure SetAsInteger(const Value: Integer);
procedure SetAsBoolean(const Value: Boolean);
procedure SetAsPointer(const Value: Pointer);
procedure SetAsCardinal(const Value: Cardinal);
procedure SetAsDateTime(const Value: TDateTime);
procedure SetAsException(const Value: Exception);
procedure SetAsInterface(const Value: IInterface);
{$IFDEF UNICODE}
procedure SetAsAnsiString(const Value: AnsiString);
{$ENDIF}
procedure SetAsWideString(const Value: WideString);
property Name: string read GetName write SetName;
property AsInt64: Int64 read GetAsInt64 write SetAsInt64;
property AsFloat: Extended read GetAsFloat write SetAsFloat;
property AsDouble: Double read GetAsDouble write SetAsDouble;
property AsString: string read GetAsString write SetAsString;
property AsObject: TObject read GetAsObject write SetAsObject;
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsInteger: Integer read GetAsInteger write SetAsInteger;
property AsPointer: Pointer read GetAsPointer write SetAsPointer;
property AsCardinal: Cardinal read GetAsCardinal write SetAsCardinal;
property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
property AsException: Exception read GetAsException write SetAsException;
property AsInterface: IInterface read GetAsInterface write SetAsInterface;
{$IFDEF UNICODE}
property AsAnsiString: AnsiString read GetAsAnsiString write SetAsAnsiString;
{$ENDIF}
property AsWideString: WideString read GetAsWideString write SetAsWideString;
end;
IValueList = Interface(IInterface)
['{54B01683-17B0-4719-B620-48FDF31BC574}']
function GetCount: Integer;
function GetItems(const Index: Integer): IAnyValue;
function Get(const Name: string): IAnyValue;
function Remove(const Name: string): Boolean;
function Exists(const Name: string): Boolean;
function Ensure(const Name: string): IAnyValue;
property Items[const Index: Integer]: IAnyValue read GetItems;
property Count: Integer read GetCount;
procedure Clear;
end;
function CreateAnyArray(const Mode: TArrayMode; const Size: Integer = cDefArraySliceSize): IAnyArray; overload;
function CreateAnyArray(const Values: string; const Delimiter: Char): IAnyArray; overload;
function CreateAnyArray(const Values: array of TAnyValue): IAnyArray; overload;
function CreateAnyArray(const Values: TAnyValues): IAnyArray; overload;
function CreateAnyArray(const SliceSize: Integer): IAnyArray; overload;
function CreateAnyArray(const Values: TAnyValue): IAnyArray; overload;
function CreateAnyArray: IAnyArray; overload;
// return the array of TAnyValue as PAnyValueArray pointer
function NamedValue(const Name: string; const Value: TAnyValue): TNamedValue;
function AnyValues(const Value: array of TAnyValue): TAnyValue;
procedure CopyAnyValue(dest, source: PAnyValue);
procedure FinalizeAnyValue(p : PAnyValue);
function AcquireValueList: IValueList;
{$IFNDEF AnyValue_HookingOff}
// direct access to hooking
procedure InitializeHooks;
procedure FinalizeHooks;
{$ENDIF}
implementation
type
TAnyArray = class(TInterfacedOBject, IAnyArray)
private
FSumCount: Integer;
FSliceSize: Integer;
FArrayMode: TArrayMode;
FSliceData: TSliceData;
FSliceCount: Integer;
FSliceBufferMpl: Double;
function GetCount: Integer;
function GetValues: TAnyValues;
function GetSliceSize: Integer;
function GetArrayMode: TArrayMode;
function GetSliceCount: Integer;
function GetSliceBufferMpl: Double;
procedure SetSliceSize(const Value: Integer);
procedure SetSliceCount(const Value: Integer);
procedure SetArrayMode(const Value: TArrayMode);
procedure SetSliceBufferMpl(const Value: Double);
function GetItem(const Idx: Integer): TAnyValue; inline;
procedure SetItem(const Idx: Integer; const Value: TAnyValue); inline;
function GetSliceByIndex(const Index: Integer; var SliceIndex: Integer): PArraySlice; inline;
function GetSliceByValue(const Value: TAnyValue; var SliceIndex: Integer): PArraySlice;
function GetSliceByName(const Name: string; var SliceIndex: Integer): PArraySlice;
procedure DoInitializeArray(const SliceSize: Integer = cDefArraySliceSize);
procedure UpdateUpperSlices(const ItemSlice: PArraySlice; Delta: Integer);
procedure RepositionSliceData(const ItemSlice: PArraySlice);
procedure DoLoadFromStream(const Stream: TStream;
const UseHandler: Boolean;
const LoadCallback: TAnyValuesLoadHandler);
procedure DoSaveToStream(const Stream: TStream;
const UseHandler: Boolean;
const SaveCallback: TAnyValuesSaveHandler);
function ArrayIsInvalid: Boolean;
function GetLastSlice: PArraySlice;
procedure RestructureSlicedArray;
procedure DoAcquireNewSlice;
public
constructor Create(const aMode: TArrayMode; const aSize: Integer = cDefArraySliceSize); overload;
constructor Create(const Values: TAnyValues; const aLastPos: Integer); overload;
constructor Create(const Values: string; const Delimiter: Char); overload;
constructor Create(const Values: array of TAnyValue); overload;
constructor Create(const Values: TAnyValues); overload;
constructor Create(const SliceSize: Integer); overload;
constructor Create(const Values: TAnyValue); overload;
constructor Create; overload;
destructor Destroy; override;
procedure Grow; inline;
procedure Clear;
procedure Reverse;
function Pop: TAnyValue;
function Shift: TAnyValue;
function Clone: IAnyArray;
function PushNull: PAnyValue;
function RawData: PSliceData;
procedure Assign(const AnyArray: IAnyArray);
procedure SaveToStream(const Stream: TStream; const SaveCallback: TAnyValuesSaveCallback = nil); overload;
procedure LoadFromStream(const Stream: TStream; const LoadCallback: TAnyValuesLoadCallback = nil); overload;
{$IF CompilerVersion >= 20}
procedure SaveToStream(const Stream: TStream; const SaveFunc: TAnyValuesSaveFunc); overload;
procedure LoadFromStream(const Stream: TStream; const LoadFunc: TAnyValuesLoadFunc); overload;
{$IFEND}
procedure Exchange(const Index1, Index2: Integer);
function Equal(const Value: IAnyArray): Boolean;
procedure Push(const Value: TAnyValue); overload;
procedure Push(const Value: array of TAnyValue); overload;
procedure Push(const Value: string; const Delimiter: Char); overload;
procedure Unshift(const Value: TAnyValue); overload;
procedure Unshift(const Value: array of TAnyValue); overload;
procedure Unshift(const Value: string; const Delimiter: Char); overload;
procedure AddNamed(const Name: string; const Value: TAnyValue);
procedure DeleteIndex(const Index: Integer); overload;
procedure DeleteIndex(const Index: Integer; const Elements: Integer); overload;
procedure DeleteValue(const Value: TAnyValue; const AllInstances: Boolean = True); overload;
procedure DeleteValue(const Value: array of TAnyValue; const AllInstances: Boolean = True); overload;
procedure Insert(const Index: Integer; const Value: string; const Delimiter: Char); overload;
procedure Insert(const Index: Integer; const Value: array of TAnyValue); overload;
procedure Insert(const Index: Integer; const Value: TAnyValue); overload;
procedure Sort(const Compare: TAnyValuesCompareCallback); overload;
{$IF CompilerVersion >= 20}
procedure Sort(const Compare: TAnyValuesCompareFunc); overload;
{$IFEND}
function Last: TAnyValue;
function First: TAnyValue;
function Enum: IAnyArrayEnumerate;
function RawItem(const Index: Integer): PAnyValue;
function IndexOf(const Value: TAnyValue): Integer;
function IndexOfNamed(const Name: string): Integer;
function LastIndexOf(const Value: TAnyValue): Integer;
function FindNamed(const Name: string): TAnyValue;
function Contains(const Value: TAnyValue): Boolean;
function Slice(Start: Integer; Stop: Integer = -1): IAnyArray;
function GetAsString(const Delimiter: Char = ','): string;
property Item[const Idx: Integer]: TAnyValue read GetItem write SetItem; default;
property SliceBufferMpl: Double read GetSliceBufferMpl write SetSliceBufferMpl;
property ArrayMode: TArrayMode read GetArrayMode write SetArrayMode;
property SliceCount: Integer read GetSliceCount write SetSliceCount;
property SliceSize: Integer read GetSliceSize write SetSliceSize;
property Values: TAnyValues read GetValues;
property Count: Integer read GetCount;
end;
//**************************************************************************************//
//******************** ENUMERATORS FOR THE VALUES AND POINTERS *************************//
//**************************************************************************************//
TAnyArrayForwardEnumerator = class(TInterfacedObject, IAnyArrayEnumerator)
private
FIndex: Integer;
FSliceIdx: Integer;
FSliceData: PSliceData;
FSliceCount: Integer;
function _GetCurrent: PAnyValue; inline;
public
constructor Create(const SliceData: PSliceData; const SliceCount: Integer);
property Current: PAnyValue read _GetCurrent;
function MoveNext: Boolean; inline;
end;
TAnyArrayReverseEnumerator = class(TInterfacedObject, IAnyArrayEnumerator)
private
FIndex: Integer;
FSliceIdx: Integer;
FSliceData: PSliceData;
FSliceCount: Integer;
function _GetCurrent: PAnyValue;
public
constructor Create(const SliceData: PSliceData; const SliceCount: Integer);
property Current: PAnyValue read _GetCurrent;
function MoveNext: Boolean; inline;
end;
TAnyArrayEnumerable = class(TInterfacedObject, IAnyArrayEnumerable)
private
FEnumerator: IAnyArrayEnumerator;
public
constructor Create(const Enumerator: IAnyArrayEnumerator);
function GetEnumerator: IAnyArrayEnumerator;
end;
TAnyArrayEnumerate = class(TInterfacedObject, IAnyArrayEnumerate)
private
FSliceData: PSliceData;
FSliceCount: Integer;
public
constructor Create(const SliceData: PSliceData; const SliceCount: Integer);
function Forward: IAnyArrayEnumerable;
function Reverse: IAnyArrayEnumerable;
end;
//**************************************************************************************//
//**************************************************************************************//
TAnyValueObject = class(TInterfacedObject, IAnyValue)
private
FName: string;
FValue: TAnyValue;
function GetName: string;
function GetAsInt64: Int64;
function GetAsFloat: Extended;
function GetAsDouble: Double;
function GetAsString: string;
function GetAsObject: TObject;
function GetAsInteger: Integer;
function GetAsBoolean: Boolean;
function GetAsPointer: Pointer;
function GetAsCardinal: Cardinal;
function GetAsDateTime: TDateTime;
function GetAsException: Exception;
function GetAsInterface: IInterface;
{$IFDEF UNICODE}
function GetAsAnsiString: AnsiString;
{$ENDIF}
function GetAsWideString: WideString;
procedure SetName(const Value: string);
procedure SetAsString(const Value: string);
procedure SetAsInt64(const Value: Int64);
procedure SetAsFloat(const Value: Extended);
procedure SetAsDouble(const Value: Double);
procedure SetAsObject(const Value: TObject);
procedure SetAsInteger(const Value: Integer);
procedure SetAsBoolean(const Value: Boolean);
procedure SetAsPointer(const Value: Pointer);
procedure SetAsCardinal(const Value: Cardinal);
procedure SetAsDateTime(const Value: TDateTime);
procedure SetAsException(const Value: Exception);
procedure SetAsInterface(const Value: IInterface);
{$IFDEF UNICODE}
procedure SetAsAnsiString(const Value: AnsiString);
{$ENDIF}
procedure SetAsWideString(const Value: WideString);
public
constructor Create;
function ValueType: TValueType;
property Name: string read GetName write SetName;
property AsInt64: Int64 read GetAsInt64 write SetAsInt64;
property AsFloat: Extended read GetAsFloat write SetAsFloat;
property AsDouble: Double read GetAsDouble write SetAsDouble;
property AsString: string read GetAsString write SetAsString;
property AsObject: TObject read GetAsObject write SetAsObject;
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsInteger: Integer read GetAsInteger write SetAsInteger;
property AsPointer: Pointer read GetAsPointer write SetAsPointer;
property AsCardinal: Cardinal read GetAsCardinal write SetAsCardinal;
property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
property AsException: Exception read GetAsException write SetAsException;
property AsInterface: IInterface read GetAsInterface write SetAsInterface;
{$IFDEF UNICODE}
property AsAnsiString: AnsiString read GetAsAnsiString write SetAsAnsiString;
{$ENDIF}
property AsWideString: WideString read GetAsWideString write SetAsWideString;
end;
TValueList = class(TInterfacedObject, IValueList)
private
FValuesList: TInterfaceList;
function GetItems(const Index: Integer): IAnyValue;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
function Get(const Name: string): IAnyValue;
function Remove(const Name: string): Boolean;
function Exists(const Name: string): Boolean;
function Ensure(const Name: string): IAnyValue;
property Items[const Index: Integer]: IAnyValue read GetItems;
property Count: Integer read GetCount;
procedure Clear;
end;
{$IFNDEF AnyValue_HookingOff}
var
IsHooked: Boolean;
vValueInfo: PTypeInfo;
// old address of System._FinalizeRecord
OldCopyRecord: procedure(Dest, Source, TypeInfo: Pointer);
OldAddRefRecord: procedure(p: Pointer; typeInfo: Pointer);
OldFinalizeRecord: procedure(p: Pointer; typeInfo: Pointer);
OldInitializeRecord: procedure(p: Pointer; typeInfo: Pointer);
{$ENDIF}
function CreateAnyArray(const Mode: TArrayMode; const Size: Integer): IAnyArray;
begin
Result := TAnyArray.Create(Mode, Size);
end;
function CreateAnyArray(const Values: string; const Delimiter: Char): IAnyArray;
begin
Result := TAnyArray.Create(Values, Delimiter);
end;
function CreateAnyArray(const Values: array of TAnyValue): IAnyArray;
begin
Result := TAnyArray.Create(Values);
end;
function CreateAnyArray(const Values: TAnyValues): IAnyArray;
begin
Result := TAnyArray.Create(Values);
end;
function CreateAnyArray(const Values: TAnyValue): IAnyArray;
begin
Result := TAnyArray.Create(Values);
end;
function CreateAnyArray(const SliceSize: Integer): IAnyArray;
begin
Result := TAnyArray.Create(SliceSize);
end;
function CreateAnyArray: IAnyArray;
begin
Result := TAnyArray.Create;
end;
function AnyValues(const Value: array of TAnyValue): TAnyValue;
begin
Result.EnsureAsArray.Push(Value);
end;
function NamedValue(const Name: string; const Value: TAnyValue): TNamedValue;
begin
Result.Name := Name;
Result.Value := Value;
end;
function AcquireValueList: IValueList;
begin
Result := TValueList.Create;
end;
procedure SlicedQuickSort(Values: IAnyArray; L, R: Integer; SCompare: TAnyValuesCompare);
var
I, J: Integer;
T: TAnyValue;
P: TAnyValue;
begin
repeat
I := L;
J := R;
CopyAnyValue(@P, Values.RawItem((L + R) shr 1));
repeat
while SCompare(Values.RawItem(I), @P) < 0 do
Inc(I);
while SCompare(Values.RawItem(J), @P) > 0 do
Dec(J);
if I <= J then
begin
if I <> J then
begin
CopyAnyValue(@T, Values.RawItem(I));
CopyAnyValue(Values.RawItem(I), Values.RawItem(J));
CopyAnyValue(Values.RawItem(J), @T);
end;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then
SlicedQuickSort(Values, L, J, SCompare);
L := I;
until I >= R;