forked from VSoftTechnologies/DUnitX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUnitX.Utils.pas
3107 lines (2786 loc) · 86.4 KB
/
DUnitX.Utils.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
{***************************************************************************}
{ }
{ DUnitX }
{ }
{ Copyright (C) 2013 Vincent Parrett }
{ }
{ http://www.finalbuilder.com }
{ }
{ }
{***************************************************************************}
{ }
{ 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. }
{ }
{***************************************************************************}
{ }
{ Portions of the file also fall under the following license }
{ as they were taken from the DSharp Project }
{ https://bitbucket.org/sglienke/dsharp }
{ }
(*
Copyright (c) 2011-2012, Stefan Glienke
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 this library 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 HOLDER 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.
*)
unit DUnitX.Utils;
interface
uses
Generics.Collections,
TimeSpan,
Rtti,
SysUtils,
Types,
TypInfo;
{$I DUnitX.inc}
type
TCustomAttributeClass = class of TCustomAttribute;
TAttributeUtils = class
class function ContainsAttribute(const attributes : TArray<TCustomAttribute>; const AttributeClass : TCustomAttributeClass) : boolean;
class function FindAttribute(const attributes : TArray<TCustomAttribute>; const AttributeClass : TCustomAttributeClass) : TCustomAttribute;overload;
class function FindAttribute(const attributes : TArray<TCustomAttribute>; const AttributeClass : TCustomAttributeClass; var attribute : TCustomAttribute; const startIndex : integer = 0) : integer;overload;
class function FindAttributes(const attributes : TArray<TCustomAttribute>; const AttributeClass : TCustomAttributeClass) : TArray<TCustomAttribute>;
end;
{$IFDEF DELPHI_2010}
const
TicksPerMillisecond = 10000;
type
TTimeSpanHelper = record helper for TTimeSpan
public
class function Subtract(const D1, D2: TDateTime): TTimeSpan; static;
end;
{$ENDIF}
type
TStrUtils = class
class function PadString(const s: string; const totalLength: integer; const padLeft: boolean = True; padChr: Char = ' '): string;
class function SplitString(const S, Delimiters: string): TStringDynArray;
end;
type
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="System.TObject">TObject</see> for easier RTTI use.
/// </summary>
{$ENDREGION}
TObjectHelper = class helper for TObject
public
{$REGION 'Documentation'}
/// <summary>
/// Returns a list of all fields of the object.
/// </summary>
{$ENDREGION}
function GetFields: TArray<TRttiField>;
{$REGION 'Documentation'}
/// <summary>
/// Returns the field with the given name; <b>nil</b> if nothing is found.
/// </summary>
/// <param name="AName">
/// Name of the field to find
/// </param>
{$ENDREGION}
function GetField(const AName: string): TRttiField;
{$REGION 'Documentation'}
/// <summary>
/// Returns the member with the given name; <b>nil</b> if nothing is found.
/// </summary>
/// <param name="AName">
/// Name of the member to find
/// </param>
{$ENDREGION}
function GetMember(const AName: string): TRttiMember;
{$REGION 'Documentation'}
/// <summary>
/// Returns a list of all methods of the object.
/// </summary>
{$ENDREGION}
function GetMethods: TArray<TRttiMethod>;
{$REGION 'Documentation'}
/// <summary>
/// Returns the method at the given code address; <b>nil</b> if nothing
/// is found.
/// </summary>
/// <param name="ACodeAddress">
/// Code address of the method to find.
/// </param>
{$ENDREGION}
function GetMethod(ACodeAddress: Pointer): TRttiMethod; overload;
{$REGION 'Documentation'}
/// <summary>
/// Returns the method with the given name; <b>nil</b> if nothing is
/// found.
/// </summary>
/// <param name="AName">
/// Name of the method to find
/// </param>
{$ENDREGION}
function GetMethod(const AName: string): TRttiMethod; overload;
{$REGION 'Documentation'}
/// <summary>
/// Returns a list of all properties of the object.
/// </summary>
{$ENDREGION}
function GetProperties: TArray<TRttiProperty>;
{$REGION 'Documentation'}
/// <summary>
/// Returns the property with the given name; <b>nil</b> if nothing is
/// found.
/// </summary>
/// <param name="AName">
/// Name of the property to find
/// </param>
{$ENDREGION}
function GetProperty(const AName: string): TRttiProperty;
{$REGION 'Documentation'}
/// <summary>
/// Returns the type of the object; nil if nothing is found.
/// </summary>
{$ENDREGION}
function GetType: TRttiType;
{$REGION 'Documentation'}
/// <summary>
/// Returns if the object contains a field with the given name.
/// </summary>
/// <param name="AName">
/// Name of the field to find
/// </param>
{$ENDREGION}
function HasField(const AName: string): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Returns if the object contains a method with the given name.
/// </summary>
{$ENDREGION}
function HasMethod(const AName: string): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Returns if the object contains a property with the given name.
/// </summary>
{$ENDREGION}
function HasProperty(const AName: string): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the method with the given name and returns if this was
/// successful.
/// </summary>
/// <param name="AName">
/// Name of the field to find
/// </param>
/// <param name="AField">
/// Field that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetField(const AName: string; out AField: TRttiField): Boolean;
{$REGION 'Documentation'}
/// <param name="AName">
/// Name of the member to find
/// </param>
/// <param name="AMember">
/// Member that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetMember(const AName: string; out AMember: TRttiMember): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the method with the given code address and returns if this
/// was successful.
/// </summary>
/// <param name="ACodeAddress">
/// Code address of the method to find
/// </param>
/// <param name="AMethod">
/// Method that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetMethod(ACodeAddress: Pointer; out AMethod: TRttiMethod): Boolean; overload;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the method with the given name and returns if this was
/// successful.
/// </summary>
/// <param name="AName">
/// Name of the method to find
/// </param>
/// <param name="AMethod">
/// Method that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetMethod(const AName: string; out AMethod: TRttiMethod): Boolean; overload;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the property with the given name and returns if this was
/// successful.
/// </summary>
/// <param name="AName">
/// Name of the property to find
/// </param>
/// <param name="AProperty">
/// Property that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetProperty(const AName: string; out AProperty: TRttiProperty): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the type of the object and returns if this was successful.
/// </summary>
/// <param name="AType">
/// Type of the object when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetType(out AType: TRttiType): Boolean;
{$IF CompilerVersion < 23}
class function QualifiedClassName: string;
{$IFEND}
end;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiField">TRttiField</see> for easier RTTI
/// use.
/// </summary>
{$ENDREGION}
TRttiFieldHelper = class helper for TRttiField
public
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the value of the field and returns if this was successful.
/// </summary>
/// <param name="Instance">
/// Pointer to the instance of the field
/// </param>
/// <param name="Value">
/// Value of the field when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetValue(Instance: Pointer; out Value: TValue): Boolean;
end;
{$IF CompilerVersion < 23}
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiInstanceTypeHelper">TRttiInstanceTypeHelper</see>
/// for easier RTTI use.
/// </summary>
{$ENDREGION}
TRttiInstanceTypeHelper = class helper for TRttiInstanceType
public
function GetDeclaredImplementedInterfaces: TArray<TRttiInterfaceType>;
function GetImplementedInterfaces: TArray<TRttiInterfaceType>;
end;
{$IFEND}
{$IF DELPHI_XE2_UP}
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiInvokableType">TRttiInvokableType</see>
/// for easier RTTI use.
/// </summary>
{$ENDREGION}
TRttiInvokableTypeHelper = class helper for TRttiInvokableType
private
function GetParameterCount: Integer;
public
property ParameterCount: Integer read GetParameterCount;
end;
{$IFEND}
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiMember">TRttiMember</see> for easier RTTI
/// use.
/// </summary>
{$ENDREGION}
TRttiMemberHelper = class helper for TRttiMember
private
function GetMemberIsReadable: Boolean;
function GetMemberIsWritable: Boolean;
function GetMemberRttiType: TRttiType;
public
property IsReadable: Boolean read GetMemberIsReadable;
property IsWritable: Boolean read GetMemberIsWritable;
property RttiType: TRttiType read GetMemberRttiType;
end;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiMethod">TRttiMethod</see> for easier RTTI
/// use.
/// </summary>
{$ENDREGION}
TRttiMethodHelper = class helper for TRttiMethod
private
function GetParameterCount: Integer;
public
function Format(const Args: array of TValue; SkipSelf: Boolean = True): string;
property ParameterCount: Integer read GetParameterCount;
end;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiObject">TRttiObject</see> for easier RTTI
/// use.
/// </summary>
{$ENDREGION}
TRttiObjectHelper = class helper for TRttiObject
public
function GetAttributeOfType<T: TCustomAttribute>: T;
function GetAttributesOfType<T: TCustomAttribute>: TArray<T>;
function HasAttributeOfType<T: TCustomAttribute>: Boolean;
function TryGetAttributeOfType<T: TCustomAttribute>(out AAttribute: T): Boolean;
end;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiParameter">TRttiParameter</see> for easier
/// RTTI use.
/// </summary>
{$ENDREGION}
TRttiParameterHelper = class helper for TRttiParameter
public
class function Equals(const Left, Right: TArray<TRttiParameter>): Boolean; //overload;
end;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiProperty">TRttiProperty</see> for easier
/// RTTI use.
/// </summary>
{$ENDREGION}
TRttiPropertyHelper = class helper for TRttiProperty
public
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the value of the property and returns if this was
/// successful.
/// </summary>
/// <param name="Instance">
/// Pointer to the instance of the field
/// </param>
/// <param name="Value">
/// Value of the field when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetValue(Instance: Pointer; out Value: TValue): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Sets the value of the property and returns if this was successful.
/// </summary>
/// <param name="Instance">
/// Pointer to the instance of the field
/// </param>
/// <param name="Value">
/// Value the field should be set to
/// </param>
{$ENDREGION}
function TrySetValue(Instance: Pointer; Value: TValue): Boolean;
end;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TRttiType">TRttiType</see> for easier RTTI use.
/// </summary>
{$ENDREGION}
TRttiTypeHelper = class helper for TRttiType
private
function GetAsInterface: TRttiInterfaceType;
function GetIsInterface: Boolean;
function GetMethodCount: Integer;
function InheritsFrom(OtherType: PTypeInfo): Boolean;
public
function GetAttributesOfType<T: TCustomAttribute>: TArray<T>;
function GetGenericArguments: TArray<TRttiType>;
function GetGenericTypeDefinition(const AIncludeUnitName: Boolean = True): string;
function GetMember(const AName: string): TRttiMember;
{$REGION 'Documentation'}
/// <summary>
/// Returns the method at the given code address; <b>nil</b> if nothing
/// is found.
/// </summary>
/// <param name="ACodeAddress">
/// Code address of the method to find
/// </param>
{$ENDREGION}
function GetMethod(ACodeAddress: Pointer): TRttiMethod; overload;
function GetProperty(const AName: string): TRttiProperty;
function GetStandardConstructor: TRttiMethod;
function IsCovariantTo(OtherClass: TClass): Boolean; overload;
function IsCovariantTo(OtherType: PTypeInfo): Boolean; overload;
function IsGenericTypeDefinition: Boolean;
function IsGenericTypeOf(const BaseTypeName: string): Boolean;
function IsInheritedFrom(OtherType: TRttiType): Boolean; overload;
function IsInheritedFrom(const OtherTypeName: string): Boolean; overload;
function MakeGenericType(TypeArguments: array of PTypeInfo): TRttiType;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the method with the given name and returns if this was
/// successful.
/// </summary>
/// <param name="AName">
/// Name of the field to find
/// </param>
/// <param name="AField">
/// Field that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetField(const AName: string; out AField: TRttiField): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the member with the given name and returns if this was
/// successful.
/// </summary>
/// <param name="AName">
/// Name of the member to find
/// </param>
/// <param name="AMember">
/// Member that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetMember(const AName: string; out AMember: TRttiMember): Boolean;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the method with the given code address and returns if this
/// was successful.
/// </summary>
/// <param name="ACodeAddress">
/// Code address of the method to find
/// </param>
/// <param name="AMethod">
/// Method that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetMethod(ACodeAddress: Pointer; out AMethod: TRttiMethod): Boolean; overload;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the method with the given code address and returns if this
/// was successful.
/// </summary>
/// <param name="AName">
/// Name of the method to find
/// </param>
/// <param name="AMethod">
/// Method that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetMethod(const AName: string; out AMethod: TRttiMethod): Boolean; overload;
//will get the first declated constructor it finds
function TryGetConstructor(out AMethod : TRttiMethod) : boolean;
function TryGetDestructor(out AMethod : TRttiMethod) : boolean;
{$REGION 'Documentation'}
/// <summary>
/// Retrieves the property with the given name and returns if this was
/// successful.
/// </summary>
/// <param name="AName">
/// Name of the property to find
/// </param>
/// <param name="AProperty">
/// Property that was found when Result is <b>True</b>
/// </param>
{$ENDREGION}
function TryGetProperty(const AName: string; out AProperty: TRttiProperty): Boolean;
function TryGetStandardConstructor(out AMethod: TRttiMethod): Boolean;
property AsInterface: TRttiInterfaceType read GetAsInterface;
property IsInterface: Boolean read GetIsInterface;
property MethodCount: Integer read GetMethodCount;
end;
TValue = Rtti.TValue;
{$REGION 'Documentation'}
/// <summary>
/// Extends <see cref="Rtti.TValue">TValue</see> for easier RTTI use.
/// </summary>
{$ENDREGION}
TValueHelper = record helper for TValue
private
function GetRttiType: TRttiType;
class function FromFloat(ATypeInfo: PTypeInfo; AValue: Extended): TValue; static;
public
function IsFloat: Boolean;
function IsNumeric: Boolean;
function IsPointer: Boolean;
function IsString: Boolean;
function IsInstance: Boolean;
function IsInterface: Boolean;
// conversion for almost all standard types
function TryConvert(ATypeInfo: PTypeInfo; out AResult: TValue): Boolean; overload;
function TryConvert<T>(out AResult: TValue): Boolean; overload;
function AsByte: Byte;
function AsCardinal: Cardinal;
function AsCurrency: Currency;
function AsDate: TDate;
function AsDateTime: TDateTime;
function AsDouble: Double;
function AsFloat: Extended;
function AsPointer: Pointer;
function AsShortInt: ShortInt;
function AsSingle: Single;
function AsSmallInt: SmallInt;
function AsTime: TTime;
function AsUInt64: UInt64;
function AsWord: Word;
function ToObject: TObject;
function ToVarRec: TVarRec;
class function ToString(const Value: TValue): string; overload; static;
class function ToString(const Values: array of TValue): string; overload; static;
class function ToVarRecs(const Values: array of TValue): TArray<TVarRec>; static;
class function Equals(const Left, Right: TArray<TValue>): Boolean; overload; static;
class function Equals<T>(const Left, Right: T): Boolean; overload; static;
class function From(ABuffer: Pointer; ATypeInfo: PTypeInfo): TValue; overload; static;
class function From(AValue: NativeInt; ATypeInfo: PTypeInfo): TValue; overload; static;
class function From(AObject: TObject; AClass: TClass): TValue; overload; static;
class function FromBoolean(const Value: Boolean): TValue; static;
class function FromString(const Value: string): TValue; static;
class function FromVarRec(const Value: TVarRec): TValue; static;
function IsBoolean: Boolean;
function IsByte: Boolean;
function IsCardinal: Boolean;
function IsCurrency: Boolean;
function IsDate: Boolean;
function IsDateTime: Boolean;
function IsDouble: Boolean;
function IsInteger: Boolean;
function IsInt64: Boolean;
function IsShortInt: Boolean;
function IsSingle: Boolean;
function IsSmallInt: Boolean;
function IsTime: Boolean;
function IsUInt64: Boolean;
function IsVariant: Boolean;
function IsWord: Boolean;
property RttiType: TRttiType read GetRttiType;
end;
TRttiPropertyExtension = class(TRttiInstanceProperty)
private
FPropInfo: TPropInfo;
FGetter: TFunc<Pointer, TValue>;
FSetter: TProc<Pointer, TValue>;
class var
FRegister: TDictionary<TPair<PTypeInfo, string>, TRttiPropertyExtension>;
FPatchedClasses: TDictionary<TClass, TClass>;
function GetIsReadableStub: Boolean; //override;
function GetIsWritableStub: Boolean; //override;
function DoGetValueStub(Instance: Pointer): TValue; //override;
procedure DoSetValueStub(Instance: Pointer; const AValue: TValue); //override;
function GetPropInfoStub: PPropInfo; // override;
protected
class procedure InitVirtualMethodTable;
function GetIsReadable: Boolean; virtual;
function GetIsWritable: Boolean; virtual;
function DoGetValue(Instance: Pointer): TValue; virtual;
procedure DoSetValue(Instance: Pointer; const AValue: TValue); virtual;
function GetPropInfo: PPropInfo; virtual;
public
class constructor Create;
class destructor Destroy;
constructor Create(Parent: PTypeInfo; const Name: string; PropertyType: PTypeInfo);
class function FindByName(Parent: TRttiType;
const PropertyName: string): TRttiPropertyExtension; overload;
class function FindByName(const FullPropertyName: string): TRttiPropertyExtension; overload;
property Getter: TFunc<Pointer, TValue> read FGetter write FGetter;
property Setter: TProc<Pointer, TValue> read FSetter write FSetter;
end;
TArrayHelper = class
public
class function Concat<T>(const Arrays: array of TArray<T>): TArray<T>; static;
{$IF DELPHI_2010}
class function ToArray<T>(Enumerable: TEnumerable<T>; Count: Integer): TArray<T>; static;
{$IFEND}
end;
PObject = ^TObject;
function FindType(const AName: string; out AType: TRttiType): Boolean; overload;
function FindType(const AGuid: TGUID; out AType: TRttiType): Boolean; overload;
{$REGION 'Documentation'}
/// <summary>
/// Returns the RTTI type of the given TClass.
/// </summary>
{$ENDREGION}
function GetRttiType(AClass: TClass): TRttiType; overload;
{$REGION 'Documentation'}
/// <summary>
/// Returns the RTTI type of the given TypeInfo.
/// </summary>
{$ENDREGION}
function GetRttiType(ATypeInfo: PTypeInfo): TRttiType; overload;
function GetRttiTypes: TArray<TRttiType>;
function IsClassCovariantTo(ThisClass, OtherClass: TClass): Boolean;
function IsTypeCovariantTo(ThisType, OtherType: PTypeInfo): Boolean;
function TryGetRttiType(AClass: TClass; out AType: TRttiType): Boolean; overload;
function TryGetRttiType(ATypeInfo: PTypeInfo; out AType: TRttiType): Boolean; overload;
function CompareValue(const Left, Right: TValue): Integer;
function SameValue(const Left, Right: TValue): Boolean;
function StripUnitName(const s: string): string;
{$IFDEF DELPHI_2010}
function SplitString(const S: string; const Delimiters: string): TStringDynArray;
{$ENDIF}
function Supports(const Instance: TValue; const IID: TGUID; out Intf): Boolean; overload;
const
ObjCastGUID: TGUID = '{CEDF24DE-80A4-447D-8C75-EB871DC121FD}';
implementation
uses
classes,
Generics.Defaults,
Math,
StrUtils,
SysConst;
var
Context: TRttiContext;
Enumerations: TDictionary<PTypeInfo, TStrings>;
{ TAttributeUtils }
class function TAttributeUtils.ContainsAttribute(const attributes: TArray<TCustomAttribute>; const AttributeClass: TCustomAttributeClass): boolean;
begin
result := FindAttribute(attributes,AttributeClass) <> nil;
end;
class function TAttributeUtils.FindAttribute(const attributes: TArray<TCustomAttribute>; const AttributeClass: TCustomAttributeClass): TCustomAttribute;
var
attribute : TCustomAttribute;
begin
result := nil;
for attribute in attributes do
begin
if attribute.ClassType = AttributeClass then
Exit(attribute);
end;
end;
class function TAttributeUtils.FindAttribute(const attributes: TArray<TCustomAttribute>; const AttributeClass: TCustomAttributeClass;
var attribute: TCustomAttribute; const startIndex: integer): integer;
var
i : integer;
begin
result := -1;
attribute := nil;
for i := startIndex to Length(attributes) -1 do
begin
if attributes[i].ClassType = AttributeClass then
begin
attribute := attributes[i];
Exit(i);
end;
end;
end;
class function TAttributeUtils.FindAttributes( const attributes: TArray<TCustomAttribute>; const AttributeClass: TCustomAttributeClass): TArray<TCustomAttribute>;
var
i : integer;
attribute : TCustomAttribute;
begin
i := 0;
SetLength(result,0);
for attribute in attributes do
begin
if attribute.ClassType = AttributeClass then
begin
SetLength(result,i + 1);
result[i] := attribute;
Inc(i);
end;
end;
end;
class function TStrUtils.PadString(const s: string; const totalLength: integer; const padLeft: boolean = True; padChr: Char = ' '): string;
begin
Result := s;
while Length(result) < totalLength do
begin
if padLeft then
Result := padChr + Result
else
Result := Result + padChr;
end;
end;
{$REGION 'Conversion functions'}
type
TConvertFunc = function(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
function ConvFail(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
Result := False;
end;
function ConvAny2Nullable(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LType: TRttiType;
LValue: TValue;
LBuffer: array of Byte;
begin
Result := TryGetRttiType(ATarget, LType) and LType.IsGenericTypeOf('Nullable')
and ASource.TryConvert(LType.GetGenericArguments[0].Handle, LValue);
if Result then
begin
SetLength(LBuffer, LType.TypeSize);
Move(LValue.GetReferenceToRawData^, LBuffer[0], LType.TypeSize - SizeOf(string));
PString(@LBuffer[LType.TypeSize - SizeOf(string)])^ := DefaultTrueBoolStr;
TValue.Make(LBuffer, LType.Handle, AResult);
PString(@LBuffer[LType.TypeSize - SizeOf(string)])^ := '';
end
end;
function ConvClass2Class(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
Result := ASource.TryCast(ATarget, AResult);
if not Result and IsTypeCovariantTo(ASource.TypeInfo, ATarget) then
begin
AResult := TValue.From(ASource.AsObject, GetTypeData(ATarget).ClassType);
Result := True;
end;
end;
function ConvClass2Enum(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
Result := ATarget = TypeInfo(Boolean);
if Result then
AResult := ASource.AsObject <> nil;
end;
function ConvEnum2Class(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LType: TRttiType;
LStrings: TStrings;
i: Integer;
begin
Result := TryGetRttiType(ATarget, LType)
and LType.AsInstance.MetaclassType.InheritsFrom(TStrings);
if Result then
begin
if not Enumerations.TryGetValue(ASource.TypeInfo, LStrings) then
begin
LStrings := TStringList.Create;
with TRttiEnumerationType(ASource.RttiType) do
begin
for i := MinValue to MaxValue do
begin
LStrings.Add(GetEnumName(Handle, i));
end;
end;
Enumerations.Add(ASource.TypeInfo, LStrings);
end;
AResult := TValue.From(LStrings, TStrings);
Result := True;
end;
end;
function ConvFloat2Ord(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
Result := Frac(ASource.AsExtended) = 0;
if Result then
AResult := TValue.FromOrdinal(ATarget, Trunc(ASource.AsExtended));
end;
function ConvFloat2Str(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LValue: TValue;
begin
if ASource.TypeInfo = TypeInfo(TDate) then
LValue := DateToStr(ASource.AsExtended)
else if ASource.TypeInfo = TypeInfo(TDateTime) then
LValue := DateTimeToStr(ASource.AsExtended)
else if ASource.TypeInfo = TypeInfo(TTime) then
LValue := TimeToStr(ASource.AsExtended)
else
LValue := FloatToStr(ASource.AsExtended);
Result := LValue.TryCast(ATarget, AResult);
end;
function ConvIntf2Class(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
Result := ConvClass2Class(ASource.AsInterface as TObject, ATarget, AResult);
end;
function ConvIntf2Intf(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LType: TRttiType;
LMethod: TRttiMethod;
LInterface: IInterface;
begin
Result := ASource.TryCast(ATarget, AResult);
if not Result then
begin
if IsTypeCovariantTo(ASource.TypeInfo, ATarget) then
begin
AResult := TValue.From(ASource.GetReferenceToRawData, ATarget);
Result := True;
end else
if TryGetRttiType(ASource.TypeInfo, LType) and (ATarget.Name = 'IList')
and LType.IsGenericTypeOf('IList') and LType.TryGetMethod('AsList', LMethod) then
begin
LInterface := LMethod.Invoke(ASource, []).AsInterface;
AResult := TValue.From(@LInterface, ATarget);
Result := True;
end;
end;
end;
function ConvNullable2Any(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LType: TRttiType;
LValue: TValue;
begin
Result := TryGetRttiType(ASource.TypeInfo, LType)
and LType.IsGenericTypeOf('Nullable');
if Result then
begin
LValue := TValue.From(ASource.GetReferenceToRawData, LType.GetGenericArguments[0].Handle);
Result := LValue.TryConvert(ATarget, AResult);
end
end;
function ConvOrd2Float(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
AResult := TValue.FromFloat(ATarget, ASource.AsOrdinal);
Result := True;
end;
function ConvOrd2Ord(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
AResult := TValue.FromOrdinal(ATarget, ASource.AsOrdinal);
Result := True;
end;
function ConvOrd2Str(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LValue: TValue;
begin
LValue := ASource.ToString;
Result := LValue.TryCast(ATarget, AResult);
end;
function ConvRec2Meth(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
begin
if ASource.TypeInfo = TypeInfo(TMethod) then
begin
AResult := TValue.From(ASource.GetReferenceToRawData, ATarget);
Result := True;
end
else
begin
Result := ConvNullable2Any(ASource, ATarget, AResult);
end;
end;
function ConvSet2Class(const ASource: TValue; ATarget: PTypeInfo; out AResult: TValue): Boolean;
var
LType: TRttiType;
LTypeData: PTypeData;
LStrings: TStrings;
i: Integer;
begin
Result := TryGetRttiType(ATarget, LType)
and LType.AsInstance.MetaclassType.InheritsFrom(TStrings);
if Result then
begin
LTypeData := GetTypeData(ASource.TypeInfo);
if not Enumerations.TryGetValue(LTypeData.CompType^, LStrings) then
begin
LStrings := TStringList.Create;
with TRttiEnumerationType(TRttiSetType(ASource.RttiType).ElementType) do
begin
for i := MinValue to MaxValue do
begin
LStrings.Add(GetEnumName(Handle, i));
end;
end;
Enumerations.Add(LTypeData.CompType^, LStrings);
end;
AResult := TValue.From(LStrings, TStrings);