forked from MagicFoundation/Alcinoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALRTTI.pas
1582 lines (1399 loc) · 64.7 KB
/
ALRTTI.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
{*********************************************************************
Description: i create the TALRttiType object because i found then the
TRTTI.getfields and/or the TRTTI.getmethods was very slow and
even call internally some criticalsections :(
**********************************************************************}
unit ALRtti;
interface
uses System.Rtti,
System.RTLConsts,
System.TypInfo,
System.Generics.Collections;
{$R-}
// http://docwiki.embarcadero.com/RADStudio/en/Conditional_compilation_(Delphi)
// http://docwiki.embarcadero.com/RADStudio/en/Compiler_Versions
{$IFDEF CPUX86}
{$DEFINE X86ASM}
{$ELSE !CPUX86}
{$DEFINE PUREPASCAL}
{$DEFINE PUREPASCAL_X64ONLY}
{$ENDIF !CPUX86}
Type
{******************}
TALRttiType = Class;
TALRttiOrdinalType = class;
TALRttiSetType = class;
{****************************}
TALRttiObject = class(TObject)
private
fAttributes: TArray<TCustomAttribute>;
FHandle: Pointer;
//FRttiDataSize: Integer;
//[Weak] FPackage: TRttiPackage;
//[Weak] FParent: TRttiObject;
//FAttributeGetter: TFunc<TArray<TCustomAttribute>>;
private
// TRttiObject descendants should only be retrieved via an RTTI context.
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); virtual;
public
constructor Create(const aRttiObject: TRttiObject);
//destructor Destroy; override;
// The starting address of the raw data in the executable image for this RTTI.
property Handle: Pointer read FHandle;
// Size of data pointed to by Handle.
//property RttiDataSize: Integer read FRttiDataSize;
//property Parent: TRttiObject read FParent;
//property Package: TRttiPackage read FPackage;
function GetAttributes: TArray<TCustomAttribute>; virtual;
end;
{***************************************}
TALRttiNamedObject = class(TALRttiObject)
private
fName: ansiString;
//function GetName: string; virtual; abstract;
public
constructor Create(Const aRttiNamedObject: TRttiNamedObject);
property Name: AnsiString read fName;
end;
{***************************************}
TALRttiMember = class(TALRttiNamedObject)
private
fVisibility: TMemberVisibility;
fOrder: integer;
//function GetParent: TRttiType;
//function GetVisibility: TMemberVisibility; virtual;
public
constructor Create(Const aRttiMember: TRttiMember);
//property Parent: TRttiType read GetParent;
property Visibility: TMemberVisibility read fVisibility;
property Order: integer read fOrder write fOrder; // The list returned by GetMethods/getProperties/etc. are ordered by the class/interface hierarchy.
// This means that the most recently included methods or properties are located at the top of the list.
// the property Order is just a "hint" to know the hierarchy. more close to 0 it is, more higher it is
// in the hierarchy
end;
{*********************************}
TALRttiField = class(TALRttiMember)
private
fRttiField: TRttiField;
FFieldType: TALRttiType;
fOffset: Integer;
//function GetFieldType: TRttiType; virtual;
//function GetOffset: Integer; virtual;
public
constructor Create(const aRttiField: TRttiField);
property FieldType: TALRttiType read FFieldType;
property Offset: Integer read fOffset;
function GetValue(Instance: Pointer): TValue; virtual;
procedure SetValue(Instance: Pointer; const AValue: TValue); virtual;
//function ToString: string; override;
end;
{************************************}
TALRttiProperty = class(TALRttiMember)
private
fRttiProperty: TRttiProperty;
fPropertyType: TALRttiType;
fIsReadable: Boolean;
fIsWritable: Boolean;
//function GetPropertyType: TRttiType; virtual; abstract;
//function GetIsReadable: Boolean; virtual; abstract;
//function GetIsWritable: Boolean; virtual; abstract;
//function DoGetValue(Instance: Pointer): TValue; virtual; abstract;
//procedure DoSetValue(Instance: Pointer; const AValue: TValue); virtual; abstract;
public
constructor Create(const aRttiProperty: TRttiProperty);
property PropertyType: TALRttiType read fPropertyType;
function GetValue(Instance: Pointer): TValue;
procedure SetValue(Instance: Pointer; const AValue: TValue);
property IsReadable: Boolean read fIsReadable;
property IsWritable: Boolean read fIsWritable;
end;
{**********************************************}
TALRttiInstanceProperty = class(TALRttiProperty)
private
fIndex: integer;
fDefault: integer;
fNameIndex: Smallint;
fPropInfo: PPropInfo;
//function GetDefault: Integer; virtual;
//function GetIndex: Integer; virtual;
//function GetNameIndex: Smallint; virtual;
//function GetPropertyType: TRttiType; override;
//function GetPropInfo: PPropInfo; virtual; // abstract;
//function GetName: string; override;
//function GetIsReadable: Boolean; override;
//function GetIsWritable: Boolean; override;
//function DoGetValue(Instance: Pointer): TValue; override;
//procedure DoSetValue(Instance: Pointer; const AValue: TValue); override;
public
constructor Create(const aRttiInstanceProperty: TRttiInstanceProperty);
//function ToString: string; override;
//property PropertyType: TRttiType read GetPropertyType;
property Index: Integer read fIndex;
property Default: Integer read fDefault;
property NameIndex: Smallint read fNameIndex;
property PropInfo: PPropInfo read fPropInfo;
end;
{******************************************}
TALRttiParameter = class(TALRttiNamedObject)
private
fParamType: TALRttiType;
FFlags: TParamFlags;
//function GetFlags: TParamFlags; virtual; abstract;
//function GetParamType: TRttiType; virtual; abstract;
public
constructor Create(const aRttiParameter: TRttiParameter);
//function ToString: string; override;
property Flags: TParamFlags read fFlags;
// ParamType may be nil if it's an untyped var or const parameter.
property ParamType: TALRttiType read fParamType;
end;
{**********************************}
TALRttiMethod = class(TALRttiMember)
private
fRttiMethod: TRttiMethod;
fRttiParameters: TArray<TALRttiParameter>;
fReturnType: TALRttiType;
fCodeAddress: Pointer;
fIsConstructor: Boolean;
fIsDestructor: boolean;
fHasExtendedInfo: Boolean;
fMethodKind: TMethodKind;
fDispatchKind: TDispatchKind;
fIsClassMethod: Boolean;
fIsStatic: Boolean;
fVirtualIndex: Smallint;
fCallingConvention: TCallConv;
//FInvokeInfo: TMethodImplementation.TInvokeInfo;
//function GetIsConstructor: Boolean;
//function GetIsDestructor: Boolean;
//function GetMethodKind: TMethodKind; virtual; abstract;
//function GetCallingConvention: TCallConv; virtual; abstract;
//function GetReturnType: TRttiType; virtual; abstract;
//function GetDispatchKind: TDispatchKind; virtual;
//function GetHasExtendedInfo: Boolean; virtual;
//function GetVirtualIndex: Smallint; virtual;
//function GetCodeAddress: Pointer; virtual;
//function GetIsClassMethod: Boolean; virtual;
//function GetIsStatic: Boolean; virtual;
//function DispatchInvoke(Instance: TValue; const Args: array of TValue): TValue; virtual; abstract;
//function GetInvokeInfo: TMethodImplementation.TInvokeInfo;
public
constructor Create(const aRttiMethod: TRttiMethod);
destructor Destroy; override;
function Invoke(Instance: TObject; const Args: array of TValue): TValue; overload;
function Invoke(Instance: TClass; const Args: array of TValue): TValue; overload;
function Invoke(Instance: TValue; const Args: array of TValue): TValue; overload;
// Create an implementation of a method with this signature, which delegates
// implementation to the passed-in callback.
//function CreateImplementation(AUserData: Pointer;
// const ACallback: TMethodImplementationCallback): TMethodImplementation;
function GetParameters: TArray<TALRttiParameter>; virtual; // abstract;
//function ToString: string; override;
property ReturnType: TALRttiType read fReturnType;
property HasExtendedInfo: Boolean read fHasExtendedInfo;
property MethodKind: TMethodKind read fMethodKind;
property DispatchKind: TDispatchKind read fDispatchKind;
property IsConstructor: Boolean read fIsConstructor;
property IsDestructor: Boolean read fIsDestructor;
property IsClassMethod: Boolean read fIsClassMethod;
// Static: No 'Self' parameter
property IsStatic: Boolean read fIsStatic;
// Vtable slot for virtual methods.
// Message index for message methods (non-negative).
// Dynamic index for dynamic methods (negative).
property VirtualIndex: Smallint read fVirtualIndex;
property CallingConvention: TCallConv read fCallingConvention;
property CodeAddress: Pointer read fCodeAddress;
function FinalCodeAddress(Cls: TClass): Pointer; // CodeAddress calculated => no virtuals etc.
end;
{*******************************************}
TALRttiIndexedProperty = class(TALRttiMember)
private
fRTTIIndexedProperty: TRttiIndexedProperty;
fPropertyType: TALRttiType;
fReadMethod: TALRttiMethod;
fWriteMethod: TALRttiMethod;
fHandle: PArrayPropInfo;
fIsReadable: Boolean;
fIsWritable: Boolean;
fIsDefault: Boolean;
//FReadMethod: TRttiMethod;
//FWriteMethod: TRttiMethod;
//procedure GetAccessors;
//function GetPropertyType: TRttiType;
//function GetIsReadable: Boolean;
//function GetIsWritable: Boolean;
//function GetIsDefault: Boolean;
//function GetReadMethod: TRttiMethod;
//function GetWriteMethod: TRttiMethod;
//function GetHandle: PArrayPropInfo;
//function GetName: string; override;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
//function GetVisibility: TMemberVisibility; override;
public
constructor Create(const aRTTIIndexedProperty: TRttiIndexedProperty);
destructor Destroy; override;
property Handle: PArrayPropInfo read fHandle;
property PropertyType: TALRttiType read fPropertyType;
property ReadMethod: TALRttiMethod read fReadMethod;
property WriteMethod: TALRttiMethod read fWriteMethod;
function GetValue(Instance: Pointer; const Args: array of TValue): TValue;
procedure SetValue(Instance: Pointer; const Args: array of TValue; const Value: TValue);
property IsReadable: Boolean read fIsReadable;
property IsWritable: Boolean read fIsWritable;
property IsDefault: Boolean read fIsDefault;
//function ToString: string; override;
end;
{*************************************}
TALRttiType = class(TALRttiNamedObject)
private
fQualifiedName: ansiString;
fHandle: PTypeInfo;
fTypeKind: TTypeKind;
fIsOrdinal: Boolean;
fIsPublicType: Boolean;
fTypeSize: Integer;
fIsManaged: Boolean;
fIsRecord: Boolean;
fIsSet: Boolean;
fIsInstance: Boolean;
fAsOrdinal: TALRttiOrdinalType;
fAsSet: TALRttiSetType;
//-----
fPrivateIndexedProperties: TArray<TALRttiIndexedProperty>;
fPrivateProperties: TArray<TALRttiProperty>;
fPrivateFields: TArray<TALRttiField>;
fPrivateMethods: TArray<TALRttiMethod>;
//-----
fProtectedIndexedProperties: TArray<TALRttiIndexedProperty>;
fProtectedProperties: TArray<TALRttiProperty>;
fProtectedFields: TArray<TALRttiField>;
fProtectedMethods: TArray<TALRttiMethod>;
//-----
fPublicIndexedProperties: TArray<TALRttiIndexedProperty>;
fPublicProperties: TArray<TALRttiProperty>;
fPublicFields: TArray<TALRttiField>;
fPublicMethods: TArray<TALRttiMethod>;
//-----
fPublishedIndexedProperties: TArray<TALRttiIndexedProperty>;
fPublishedProperties: TArray<TALRttiProperty>;
fPublishedFields: TArray<TALRttiField>;
fPublishedMethods: TArray<TALRttiMethod>;
//-----
//function GetName: string; override;
//function GetTypeKind: TTypeKind;
//function GetTypeData: PTypeData;
//function GetIsManaged: Boolean;
//function GetAsInstance: TRttiInstanceType;
//function GetAsOrdinal: TRttiOrdinalType;
//function GetAsRecord: TRttiRecordType;
//function GetIsInstance: Boolean;
//function GetIsOrdinal: Boolean;
//function GetIsRecord: Boolean;
//function GetHandle: PTypeInfo;
//function GetAsSet: TRttiSetType;
//function GetIsSet: Boolean;
//function GetTypeSize: Integer; virtual;
//function GetQualifiedName: ansiString;
//function GetBaseType: TRttiType; virtual;
//function GetIsPublicType: Boolean;
//property TypeData: PTypeData read GetTypeData;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
protected
function Find(const FromArray: TArray<TALRttiNamedObject>; const S: AnsiString; var Index: Integer): Boolean;
procedure init(const aRttiType: TRttiType);
public
constructor Create;
destructor Destroy; override;
//function ToString: string; override;
property Handle: PTypeInfo read fHandle;
// QualifiedName is only available on types declared in interface section of units;
// i.e. IsPublicType is true.
property QualifiedName: ansiString read fQualifiedName;
property IsPublicType: Boolean read fIsPublicType;
property TypeKind: TTypeKind read fTypeKind;
// The size of a location (variable) of this type.
property TypeSize: Integer read fTypeSize;
property IsManaged: Boolean read fIsManaged;
// To make writing query code easier, hoist some methods from descendants
// into this type. These return elements flattened across the type hierarchy
// in order from most derived to least derived.
//function GetMethods: TArray<TRttiMethod>; overload; virtual;
//function GetFields: TArray<TRttiField>; virtual;
//function GetProperties: TArray<TRttiProperty>; virtual;
//function GetIndexedProperties: TArray<TRttiIndexedProperty>; virtual;
//function GetMethod(const AName: string): TRttiMethod; virtual;
//function GetMethods(const AName: string): TArray<TRttiMethod>; overload; virtual;
//function GetField(const AName: string): TRttiField; virtual;
//function GetProperty(const AName: string): TRttiProperty; virtual;
//function GetIndexedProperty(const AName: string): TRttiIndexedProperty; virtual;
function GetMethods(const aVisibility: TMemberVisibility): TArray<TALRttiMethod>; overload;
function GetMethods(const AName: ansistring; const aVisibility: TMemberVisibility): TArray<TALRttiMethod>; overload;
function GetFields(const aVisibility: TMemberVisibility): TArray<TALRttiField>;
function GetField(const AName: ansistring; const aVisibility: TMemberVisibility): TALRttiField;
function GetProperties(const aVisibility: TMemberVisibility): TArray<TALRttiProperty>;
function GetProperty(const AName: ansistring; const aVisibility: TMemberVisibility): TALRttiProperty; overload;
function GetProperty(const AIndex: integer; const aVisibility: TMemberVisibility): TALRttiProperty; overload;
function GetIndexedProperties(const aVisibility: TMemberVisibility): TArray<TALRttiIndexedProperty>; overload;
function GetIndexedProperties(const AName: ansistring; const aVisibility: TMemberVisibility): TArray<TALRttiIndexedProperty>; overload;
//function GetDeclaredMethods: TArray<TRttiMethod>; virtual;
//function GetDeclaredProperties: TArray<TRttiProperty>; virtual;
//function GetDeclaredFields: TArray<TRttiField>; virtual;
//function GetDeclaredIndexedProperties: TArray<TRttiIndexedProperty>; virtual;
// The ancestor for types with ancestors.
//property BaseType: TRttiType read GetBaseType;
//property AsInstance: TRttiInstanceType read GetAsInstance;
property IsInstance: Boolean read fIsInstance;
property AsOrdinal: TALRttiOrdinalType read fAsOrdinal;
property IsOrdinal: Boolean read fIsOrdinal;
//property AsRecord: TRttiRecordType read GetAsRecord;
property IsRecord: Boolean read fIsRecord;
property IsSet: Boolean read fIsSet;
property AsSet: TALRttiSetType read fAsSet;
End;
{*************************************}
TALRttiOrdinalType = class(TALRttiType)
private
fOrdType: TOrdType;
fMinValue: Longint;
fMaxValue: Longint;
//function GetMaxValue: Longint; virtual;
//function GetMinValue: Longint; virtual;
//function GetOrdType: TOrdType;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
protected
//function GetTypeSize: Integer; override;
public
procedure init(const aRttiOrdinalType: TRttiOrdinalType);
property OrdType: TOrdType read fOrdType;
property MinValue: Longint read fMinValue;
property MaxValue: Longint read fMaxValue;
end;
{*********************************}
TALRttiSetType = class(TALRttiType)
private
fElementType: TRttiType;
//function GetElementType: TRttiType;
//function GetTypeSize: Integer; override;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
public
procedure init(const aRttiSetType: TRttiSetType);
property ElementType: TRttiType read fElementType;
end;
{*******************************************************************************}
function ALGetEnumName(TypeInfo: PTypeInfo; Value: Integer): ansistring; overload
function ALGetEnumName(PropInfo: PPropInfo; Value: Integer): ansistring; overload
function ALTryGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring; Var EnumValue: Integer): boolean; overload;
function ALTryGetEnumValue(PropInfo: PPropInfo; const Name: ansistring; Var EnumValue: Integer): boolean; overload;
function ALGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring): Integer; overload;
function ALGetEnumValue(PropInfo: PPropInfo; const Name: ansistring): Integer; overload;
function ALSetToString(TypeInfo: PTypeInfo; Value: Integer; const Brackets: Boolean = False): ansistring; overload;
function ALSetToString(PropInfo: PPropInfo; Value: Integer; const Brackets: Boolean = False): ansistring; overload;
function ALTryStringToSet(TypeInfo: PTypeInfo; const Value: ansistring; Var SetInt: Integer): Boolean; overload;
function ALTryStringToSet(PropInfo: PPropInfo; const Value: ansistring; Var SetInt: Integer): Boolean; overload;
function ALStringToSet(TypeInfo: PTypeInfo; const Value: ansistring): Integer; overload;
function ALStringToSet(PropInfo: PPropInfo; const Value: ansistring): Integer; overload;
function ALGetRttiType(const aQualifiedName: AnsiString): TALRttiType;
procedure ALRttiInitialization(const aQualifiedNameToSkip: array of AnsiString); overload;
procedure ALRttiInitialization; overload;
procedure ALRttiFinalization;
{*******************************}
var vALRTTIContext: TRttiContext;
vALRttiTypeCache: TObjectDictionary<ansiString,TALRttiType>;
implementation
uses System.sysutils,
System.Generics.Defaults,
AlString;
{****************************************}
// P points a length field of ShortString.
function _AfterString(const P: PByte): Pointer; inline;
begin
Result := P + P^ + 1;
end;
{**********************************************************************}
function ALGetEnumName(TypeInfo: PTypeInfo; Value: Integer): ansistring;
const
_BooleanIdents: array [Boolean] of ansistring = ('False', 'True');
var
P: Pointer;
T: PTypeData;
Len: Byte;
begin
if TypeInfo^.Kind = tkInteger then
begin
Result := ALIntToStr(Value);
Exit;
end;
T := GetTypeData(GetTypeData(TypeInfo)^.BaseType^);
if (TypeInfo = System.TypeInfo(Boolean)) or (T^.MinValue < 0) then
begin
{ LongBool/WordBool/ByteBool have MinValue < 0 and arbitrary
content in Value; Boolean has Value in [0, 1] }
Result := _BooleanIdents[Value <> 0];
if SameText(HexDisplayPrefix, '0x') then
Result := ALLowerCase(Result);
end
else
begin
P := @T^.NameList;
while Value <> 0 do
begin
P := _AfterString(P);
Dec(Value);
end;
//Result := _UTF8ToString(P);
Len := PByte(P)^; // length is store in First array
if Len <> 0 then
begin
P := PByte(P) + 1;
SetLength(Result, Len);
Move(PByte(P)^, pointer(Result)^, Len);
end
else result := '';
end;
end;
{**********************************************************************}
function ALGetEnumName(PropInfo: PPropInfo; Value: Integer): ansistring;
begin
result := ALGetEnumName(PropInfo^.PropType^, Value);
end;
{************************************************************************}
function _UTF8SameText(const Str1: ShortString; Str2: PAnsiChar): Boolean;
begin
Result := ALSametext(Str1, Str2);
end;
{*******************************************************************************}
function _GetEnumNameValue(TypeInfo: PTypeInfo; const Name: AnsiString): Integer;
{$IFDEF PUREPASCAL}
var
TypeData: PTypeData;
LName: PByte;
LStr: ansiString;
I: Integer;
begin
TypeData := GetTypeData(GetTypeData(TypeInfo)^.BaseType^);
LName := PByte(@TypeData.NameList);
for i := 0 to TypeData.MaxValue do begin
setlength(LStr, LName^);
ALMove(pointer(LName+1)^, pointer(LStr)^, LName^);
if ALSameText(LStr, Name) then Exit(I);
inc(LName, LName^ + 1);
end;
Result := -1;
end;
{$ELSE PUREPASCAL}
asm //StackAligned
{ -> EAX Pointer to type info }
{ EDX Pointer to string }
{ <- EAX Value }
PUSH EBX
PUSH ESI
PUSH EDI
PUSH 0
TEST EDX,EDX
JE @notFound
{ point ESI to first name of the base type }
XOR ECX,ECX
MOV CL,[EAX].TTypeInfo.Name.Byte[0]
MOV EAX,[EAX].TTypeInfo.Name[ECX+1].TTypeData.BaseType
MOV EAX,[EAX]
MOV CL,[EAX].TTypeInfo.Name.Byte[0]
LEA ESI,[EAX].TTypeInfo.Name[ECX+1].TTypeData.NameList
{ make EDI the high bound of the enum type }
MOV EDI,[EAX].TTypeInfo.Name[ECX+1].TTypeData.MaxValue
{ EAX is our running index }
XOR EAX,EAX
{ make ECX the length of the current string }
@outerLoop:
MOVZX ECX,[ESI].Byte[0]
CMP ECX,[EDX-4]
JNE @lengthMisMatch
{ we know for sure the names won't be zero length }
@cmpLoop:
TEST [ESP],1
JNZ @utf8compare
MOV BL,[EDX+ECX-1]
TEST BL,$80
JNZ @utf8compareParam
XOR BL,[ESI+ECX]
TEST BL,$80
JNZ @utf8compare
TEST BL,0DFH
JNE @misMatch
DEC ECX
JNE @cmpLoop
{ as we didn't have a mismatch, we must have found the name }
JMP @exit
@utf8compareParam:
MOV [ESP],1
@utf8compare:
PUSH EAX
PUSH EDX
MOV EAX,ESI
{$IFDEF ALIGN_STACK}
SUB ESP,4
{$ENDIF ALIGN_STACK}
CALL _UTF8SameText
{$IFDEF ALIGN_STACK}
ADD ESP,4
{$ENDIF ALIGN_STACK}
TEST AL,AL
POP EDX
POP EAX
JNZ @exit
@misMatch:
MOVZX ECX,[ESI].Byte[0]
@lengthMisMatch:
INC EAX
LEA ESI,[ESI+ECX+1]
CMP EAX,EDI
JLE @outerLoop
{ we haven't found the thing - return -1 }
@notFound:
OR EAX,-1
@exit:
POP EDI
POP EDI
POP ESI
POP EBX
end;
{$ENDIF}
{*******************************************************************************************************}
function ALTryGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring; Var EnumValue: Integer): boolean;
begin
//
// original code
//
//if TypeInfo^.Kind = tkInteger then
// Result := ALStrToInt(Name)
//else
//begin
// Assert(TypeInfo^.Kind = tkEnumeration);
// if GetTypeData(TypeInfo)^.MinValue < 0 then // Longbool/wordbool/bytebool
// begin
// if ALSameText(Name, 'False') then
// Result := 0
// else if ALSameText(Name, 'True') then
// Result := -1
// else
// Result := ALStrToInt(Name);
// end
// else
// Result := _GetEnumNameValue(TypeInfo, Name);
//end;
if TypeInfo^.Kind <> tkEnumeration then exit(false);
EnumValue := _GetEnumNameValue(TypeInfo, Name);
result := EnumValue > -1;
end;
{*******************************************************************************************************}
function ALTryGetEnumValue(PropInfo: PPropInfo; const Name: ansistring; Var EnumValue: Integer): boolean;
begin
result := ALTryGetEnumValue(PropInfo^.PropType^, Name, EnumValue);
end;
{****************************************************************************}
function ALGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring): Integer;
begin
if not ALTryGetEnumValue(TypeInfo, Name, result) then raise EALException.CreateFmt('Invalid enumeration name: %s', [Name]);
end;
{****************************************************************************}
function ALGetEnumValue(PropInfo: PPropInfo; const Name: ansistring): Integer;
begin
result := ALGetEnumValue(PropInfo^.PropType^, Name);
end;
{*******************************************************************************************************}
function ALSetToString(TypeInfo: PTypeInfo; Value: Integer; const Brackets: Boolean = False): ansistring;
var
S: TIntegerSet;
I: Integer;
begin
Result := '';
Integer(S) := Value;
TypeInfo := GetTypeData(TypeInfo)^.CompType^;
for I := 0 to SizeOf(Integer) * 8 - 1 do
if I in S then
begin
if Result <> '' then
Result := Result + ',';
Result := Result + ALGetEnumName(TypeInfo, I);
end;
if Brackets then
Result := '[' + Result + ']';
end;
{*******************************************************************************************************}
function ALSetToString(PropInfo: PPropInfo; Value: Integer; const Brackets: Boolean = False): ansistring;
begin
Result := ALSetToString(PropInfo^.PropType^, Value, Brackets);
end;
{****************************************************************************************************}
function ALTryStringToSet(TypeInfo: PTypeInfo; const Value: ansistring; Var SetInt: Integer): Boolean;
var
P: PansiChar;
EnumName: ansistring;
EnumValue: NativeInt;
EnumInfo: PTypeInfo;
// grab the next enum name
function NextWord(var P: PansiChar): ansistring;
var
i: Integer;
begin
i := 0;
// scan til whitespace
while not (P[i] in [',', ' ', #0,']']) do
Inc(i);
SetString(Result, P, i);
// skip whitespace
while (P[i] in [',', ' ',']']) do
Inc(i);
Inc(P, i);
end;
begin
Result := True;
SetInt := 0;
if Value = '' then Exit;
P := PansiChar(Value);
// skip leading bracket and whitespace
while (P^ in ['[',' ']) do
Inc(P);
EnumInfo := GetTypeData(TypeInfo)^.CompType^;
EnumName := NextWord(P);
while EnumName <> '' do
begin
EnumValue := ALGetEnumValue(EnumInfo, EnumName);
if EnumValue < 0 then exit(False);
Include(TIntegerSet(SetInt), EnumValue);
EnumName := NextWord(P);
end;
end;
{****************************************************************************************************}
function ALTryStringToSet(PropInfo: PPropInfo; const Value: ansistring; Var SetInt: Integer): Boolean;
begin
result := ALTryStringToSet(PropInfo^.PropType^, Value, SetInt);
end;
{****************************************************************************}
function ALStringToSet(TypeInfo: PTypeInfo; const Value: ansistring): Integer;
begin
if not ALTryStringToSet(TypeInfo, Value, result) then raise EALException.CreateFmt('Invalid set string: %s', [Value]);
end;
{****************************************************************************}
function ALStringToSet(PropInfo: PPropInfo; const Value: ansistring): Integer;
begin
Result := ALStringToSet(PropInfo^.PropType^, Value);
end;
{*************************************}
function ALInsufficientRtti: Exception;
begin
Result := EInsufficientRtti.CreateRes(@SInsufficientRtti);
end;
{******************************************}
procedure ALCheckCodeAddress(code: Pointer);
begin
if (code = nil) or (PPointer(code)^ = nil) then
raise ALInsufficientRtti;
end;
{***************************************************************}
constructor TALRttiObject.Create(const aRttiObject: TRttiObject);
begin
inherited create;
fAttributes := aRttiObject.GetAttributes;
fHandle := aRttiObject.Handle;
end;
{*************************************************************}
function TALRttiObject.GetAttributes: TArray<TCustomAttribute>;
begin
result := fAttributes;
end;
{******************************************************************************}
constructor TALRttiNamedObject.Create(Const aRttiNamedObject: TRttiNamedObject);
begin
inherited create(aRttiNamedObject);
fName := ansiString(aRttiNamedObject.Name);
end;
{***************************************************************}
constructor TALRttiMember.Create(Const aRttiMember: TRttiMember);
begin
inherited create(aRttiMember);
fVisibility := aRttiMember.Visibility;
fOrder := -1;
end;
{************************************************************}
constructor TALRttiField.Create(const aRttiField: TRttiField);
begin
inherited create(aRttiField);
fRttiField := aRttiField;
//ENonPublicType look like :TALFormatSettings.:1
//This will raise an exception in QualifiedName
//function TRttiType.GetQualifiedName: string;
//begin
// Result := Package.GetNameFromType(Self);
// if Result = '' then
// raise ENonPublicType.CreateResFmt(@SNonPublicType, [Name]);
//end;
if assigned(aRttiField.FieldType) and
(alpos(':',aRttiField.FieldType.Handle.Name) <> 1) then fFieldType := ALGetRttiType(ansiString(aRttiField.FieldType.QualifiedName))
else fFieldType := nil;
fOffset := aRttiField.Offset;
end;
{********************************************************}
function TALRttiField.GetValue(Instance: Pointer): TValue;
begin
result := FRttiField.GetValue(Instance);
end;
{***********************************************************************}
procedure TALRttiField.SetValue(Instance: Pointer; const AValue: TValue);
begin
FRttiField.SetValue(Instance, aValue);
end;
{*********************************************************************}
constructor TALRttiProperty.Create(const aRttiProperty: TRttiProperty);
begin
inherited create(aRttiProperty);
fRttiProperty := aRttiProperty;
if assigned(aRttiProperty.PropertyType) then fPropertyType := ALGetRttiType(ansiString(aRttiProperty.PropertyType.QualifiedName))
else fPropertyType := nil;
fIsReadable := aRttiProperty.IsReadable;
fIsWritable := aRttiProperty.IsWritable;
end;
{***********************************************************}
function TALRttiProperty.GetValue(Instance: Pointer): TValue;
begin
result := fRttiProperty.GetValue(Instance);
end;
{**************************************************************************}
procedure TALRttiProperty.SetValue(Instance: Pointer; const AValue: TValue);
begin
fRttiProperty.SetValue(Instance, AValue);
end;
{*********************************************************************************************}
constructor TALRttiInstanceProperty.Create(const aRttiInstanceProperty: TRttiInstanceProperty);
begin
inherited create(aRttiInstanceProperty);
fIndex := aRttiInstanceProperty.Index;
fDefault := aRttiInstanceProperty.Default;
fNameIndex := aRttiInstanceProperty.NameIndex;
fPropInfo := aRttiInstanceProperty.PropInfo;
end;
{************************************************************************}
constructor TALRttiParameter.Create(const aRttiParameter: TRttiParameter);
begin
inherited create(aRttiParameter);
FFlags := aRttiParameter.Flags;
if assigned(aRttiParameter.ParamType) then fParamType := ALGetRttiType(ansiString(aRttiParameter.ParamType.QualifiedName))
else fParamType := nil;
end;
{**}
type
PVtable = ^TVtable;
TVtable = array[0..MaxInt div SizeOf(Pointer) - 1] of Pointer;
{***************************************************************}
constructor TALRttiMethod.Create(const aRttiMethod: TRttiMethod);
var aRttiParameters: TArray<TRttiParameter>;
i: integer;
begin
inherited create(aRttiMethod);
fRttiMethod := aRttiMethod;
aRttiParameters := aRttiMethod.GetParameters;
setlength(fRttiParameters, length(aRttiParameters));
for I := Low(aRttiParameters) to High(aRttiParameters) do
fRttiParameters[i] := TALRttiParameter.Create(aRttiParameters[i]);
if aRttiMethod.HasExtendedInfo and
assigned(aRttiMethod.ReturnType) then fReturnType := ALGetRttiType(ansiString(aRttiMethod.ReturnType.QualifiedName))
else fReturnType := nil;
fCodeAddress := aRttiMethod.CodeAddress;
fIsConstructor := aRttiMethod.IsConstructor;
fIsDestructor := aRttiMethod.IsDestructor;
fHasExtendedInfo := aRttiMethod.HasExtendedInfo;
if aRttiMethod.HasExtendedInfo then
fMethodKind := aRttiMethod.MethodKind;
fDispatchKind := aRttiMethod.DispatchKind;
fIsClassMethod := aRttiMethod.IsClassMethod;
fIsStatic := aRttiMethod.IsStatic;
fVirtualIndex := aRttiMethod.VirtualIndex;
if aRttiMethod.HasExtendedInfo then
fCallingConvention := aRttiMethod.CallingConvention;
end;
{*******************************}
destructor TALRttiMethod.Destroy;
var i: integer;
begin
for I := Low(fRttiParameters) to High(fRttiParameters) do fRttiParameters[i].free;
inherited;
end;
{************************************************************}
function TALRttiMethod.FinalCodeAddress(Cls: TClass): Pointer; // CodeAddress calculated => no virtuals etc.
begin
if IsStatic then result := fCodeAddress // ex: TMarshal => class function OutString(const S: string): MarshaledString; overload; inline; static;
else begin
case DispatchKind of
dkVtable: result := PVtable(Cls)^[fVirtualIndex];
dkDynamic: result := GetDynaMethod(Cls, fVirtualIndex);
else result := fCodeAddress; //dkStatic => not virtual nor dynamic method
//dkMessage => never possible here
//dkInterface => never possible here
end;
end;
end;
{************************************************************************************}
function TALRttiMethod.Invoke(Instance: TObject; const Args: array of TValue): TValue;
begin
Result := fRttiMethod.Invoke(Instance, Args);
end;
{***********************************************************************************}
function TALRttiMethod.Invoke(Instance: TClass; const Args: array of TValue): TValue;
begin
Result := fRttiMethod.Invoke(Instance, Args);
end;
{***********************************************************************************}
function TALRttiMethod.Invoke(Instance: TValue; const Args: array of TValue): TValue;
begin
Result := fRttiMethod.Invoke(Instance, Args);
end;
{*************************************************************}
function TALRttiMethod.GetParameters: TArray<TALRttiParameter>;
begin
result := fRttiParameters;
end;
{******************************************************************************************}
constructor TALRttiIndexedProperty.Create(const aRTTIIndexedProperty: TRttiIndexedProperty);
begin
inherited create(aRTTIIndexedProperty);
fRTTIIndexedProperty := aRTTIIndexedProperty;
if assigned(aRTTIIndexedProperty.PropertyType) then fPropertyType := ALGetRttiType(ansiString(aRTTIIndexedProperty.PropertyType.QualifiedName))
else fPropertyType := nil;
if assigned(aRTTIIndexedProperty.ReadMethod) then fReadMethod := TALRttiMethod.Create(aRTTIIndexedProperty.ReadMethod)
else fReadMethod := nil;
if assigned(aRTTIIndexedProperty.WriteMethod) then fWriteMethod := TALRttiMethod.Create(aRTTIIndexedProperty.WriteMethod)
else fWriteMethod := nil;
fHandle := aRTTIIndexedProperty.Handle;
fIsReadable := aRTTIIndexedProperty.IsReadable;
fIsWritable := aRTTIIndexedProperty.IsWritable;
fIsDefault := aRTTIIndexedProperty.IsDefault;
end;
{****************************************}
destructor TALRttiIndexedProperty.Destroy;
begin
if assigned(fReadMethod) then fReadMethod.Free;
if assigned(fWriteMethod) then fWriteMethod.Free;
inherited;
end;
{***********************************************************************************************}
function TALRttiIndexedProperty.GetValue(Instance: Pointer; const Args: array of TValue): TValue;
begin
result := FRttiIndexedProperty.GetValue(Instance, Args);
end;
{*************************************************************************************************************}
procedure TALRttiIndexedProperty.SetValue(Instance: Pointer; const Args: array of TValue; const Value: TValue);
begin
FRttiIndexedProperty.SetValue(Instance, Args, Value);
end;
{**************************************************************************}