forked from MagicFoundation/Alcinoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlcinoe.RTTI.pas
3114 lines (2814 loc) · 135 KB
/
Alcinoe.RTTI.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
(*******************************************************************************
In the constant evolution of software development, we often find ourselves
seeking ways to reduce boilerplate code and enhance the maintainability of our
projects. One such instance where boilerplate can become cumbersome is in the
initialization of class fields. The traditional method involves explicitly
setting each field's value in the constructor, which can be tedious, especially
for classes with numerous fields. Enter TALInit—a feature that allows
automatic initialization of object fields based on their attributes.
#### The Traditional Way ####
In the typical approach, developers manually initialize object fields in the
constructor. Take the following class as an example:
```
TAutoInitObject = class(TObject)
public
CharValue: Char;
ChildObject: TChildObject;
public
constructor Create; virtual;
destructor Destroy; override;
End;
```
Here, each field is initialized in the Create constructor:
```
constructor TAutoInitObject.create(const aOwner: Tform1; const AAutoInit: Boolean);
begin
CharValue := 'A';
ChildObject := TChildObject.create;
ChildObject.Name := 'AnObject';
ChildObject.Value := 12.2;
end;
destructor TAutoInitObject.Destroy;
begin
ALFreeandNil(ChildObject);
inherited;
end;
```
While this method offers precise control, it can become tedious for large
classes with numerous fields.
#### The TALInit Way ####
Imagine having a mechanism that not only automates this but is also as fast as
the traditional way - yes, you read that right. TALInit achieves this
remarkable feat.
```
TAutoInitObject = class(TObject)
public
[TALInit('A')]
CharValue: Char;
[TALInit('Name:AnObject;Value:12.2')]
ChildObject: TChildObject;
End;
```
By using custom attributes, every field within the object can be automatically
initialized based on its corresponding attribute. This eliminates the need for
manually setting each field within the constructor. The above snippet showcases
just how concise and readable object field initialization can become with
TALInit.
#### Performance - A Game Changer: ####
One of the strongest advantages of using TALInit is its performance. When
introducing automation, a natural concern is the overhead that might come with
it. However, TALInit is designed to be as efficient as the traditional way
of initializing fields. This means developers can enjoy the convenience
without having to worry about any hidden costs in execution time.
Learn more at [{alcinoe}/Alcinoe/tree/master/Demos/ALRTTI](https://github.com/MagicFoundation/Alcinoe/tree/master/Demos/ALRTTI)
*******************************************************************************)
unit Alcinoe.RTTI;
interface
{$I Alcinoe.inc}
uses
System.Rtti,
System.RTLConsts,
System.TypInfo,
System.Generics.Collections;
Type
{.$define ALRTTIAnsiString}
{$IF defined(ALRTTIAnsiString)}
TALRttiString = AnsiString;
{$ELSE}
TALRttiString = String;
{$ENDIF}
{******************}
TALRttiType = Class;
TALRttiOrdinalType = class;
TALRttiSetType = class;
TALRttiInstanceType = class;
TALRttiRecordType = 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: TALRttiString;
//function GetName: string; virtual; abstract;
public
constructor Create(Const aRttiNamedObject: TRttiNamedObject);
property Name: TALRttiString 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;
//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
property Order: integer read fOrder write fOrder;
end;
{*********************************}
TALRttiField = class(TALRttiMember)
private
fRttiField: TRttiField;
FFieldType: TALRttiType;
FOffset: Integer;
//function GetFieldType: TRttiType; virtual;
//function GetOffset: Integer; virtual;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
public
constructor Create(const aRttiField: TRttiField);
property FieldType: TALRttiType read FFieldType;
property Offset: Integer read FOffset;
function GetValue(Instance: Pointer): TValue;
procedure SetValue(Instance: Pointer; const AValue: TValue);
//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;
//procedure GetCommonInvokeParams(var isCons, isDest, isStat, isClas: Boolean;
// var callConv: TCallConv);
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;
// CodeAddress calculated => no virtuals etc.
function FinalCodeAddress(Cls: TClass): Pointer;
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; inline;
//function GetIsWritable: Boolean; inline;
//function GetIsDefault: Boolean;
//function GetReadMethod: TRttiMethod;
//function GetWriteMethod: TRttiMethod;
//function GetHandle: PArrayPropInfo; inline;
//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)
//protected type
// TGetListFunc<T: TRttiNamedObject> = function (AType: TRttiType): TArray<T>;
private
fQualifiedName: TALRttiString;
fHandle: PTypeInfo;
fTypeKind: TTypeKind;
fIsOrdinal: Boolean;
fIsPublicType: Boolean;
fTypeSize: Integer;
fIsManaged: Boolean;
fIsRecord: Boolean;
fIsSet: Boolean;
fIsInstance: Boolean;
fAsOrdinal: TALRttiOrdinalType;
fAsSet: TALRttiSetType;
fBaseType: TALRttiType;
fAsInstance: TALRttiInstanceType;
fAsRecord: TALRttiRecordType;
//-----
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; inline;
//function GetAsSet: TRttiSetType;
//function GetIsSet: Boolean;
//function GetIsHFA: Boolean; virtual;
//function GetHFAElementType: TRttiFloatType; virtual;
//function GetHFAElementCount: Integer; virtual;
//function GetTypeSize: Integer; virtual;
//function GetQualifiedName: string;
//function GetBaseType: TRttiType; virtual;
//function GetIsPublicType: Boolean;
//property TypeData: PTypeData read GetTypeData;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
protected
//function GetNamedObject<T: TRttiNamedObject>(const AName: string; const AGetListFunc: TGetListFunc<T>): T;
//function GetObjectList<T: TRttiNamedObject>(const AGetListFunc: TGetListFunc<T>): TArray<T>;
function Find(const FromArray: TArray<TALRttiNamedObject>; const S: TALRttiString; var Index: Integer): Boolean;
procedure init(const aRttiType: TRttiType); virtual;
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: TALRttiString 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: TALRttiString; const aVisibility: TMemberVisibility): TArray<TALRttiMethod>; overload;
function GetFields(const aVisibility: TMemberVisibility): TArray<TALRttiField>;
function GetField(const AName: TALRttiString; const aVisibility: TMemberVisibility): TALRttiField;
function GetProperties(const aVisibility: TMemberVisibility): TArray<TALRttiProperty>;
function GetProperty(const AName: TALRttiString; 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: TALRttiString; 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: TALRttiType read FBaseType;
property AsInstance: TALRttiInstanceType read fAsInstance;
property IsInstance: Boolean read fIsInstance;
property AsOrdinal: TALRttiOrdinalType read fAsOrdinal;
property IsOrdinal: Boolean read fIsOrdinal;
property AsRecord: TALRttiRecordType read fAsRecord;
property IsRecord: Boolean read fIsRecord;
property IsSet: Boolean read fIsSet;
property AsSet: TALRttiSetType read fAsSet;
/// <summary>Returns true if the reflected type is HFA.</summary>
//property IsHFA: Boolean read GetIsHFA;
/// <summary>Return the element type of HFA, if IsHFA is true. Otherwise, Return NIL. </summary>
//property HFAElementType: TRttiFloatType read GetHFAElementType;
/// <summary>Return the number of element type of HFA, if IsHFA is true. Otherwise, Return 0. </summary>
//property HFAElementCount: Integer read GetHFAElementCount;
end;
{*************************************}
TALRttiOrdinalType = class(TALRttiType)
private
fOrdType: TOrdType;
fMinValue: Integer;
fMaxValue: Integer;
//function GetMaxValue: Integer; virtual;
//function GetMinValue: Integer; virtual;
//function GetOrdType: TOrdType;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
protected
//function GetTypeSize: Integer; override;
procedure init(const aRttiType: TRttiType); override;
public
property OrdType: TOrdType read fOrdType;
property MinValue: Integer read fMinValue;
property MaxValue: Integer read fMaxValue;
end;
{*********************************}
TALRttiSetType = class(TALRttiType)
private
fElementType: TRttiType;
//function GetElementType: TRttiType;
//function GetTypeSize: Integer; override;
//function GetByteOffset: Integer;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
protected
procedure init(const aRttiType: TRttiType); override;
public
property ElementType: TRttiType read fElementType;
end;
{*************************************************}
TALRttiStructuredType = class abstract(TALRttiType)
end;
{**********************************************}
TALRttiRecordType = class(TALRttiStructuredType)
private
//FMethOfs: PByte;
{$IF Defined(CPUARM64) or Defined(LINUX64) or Defined(OSX64)}
//FHFAElementType: TRttiFloatType;
//FHFAElementCount: Integer;
//FHFAHasInsufficientTypeInformation: Boolean;
{$ENDIF CPUARM64 or LINUX64 or OSX64}
//function GetManagedFields: TArray<TRttiManagedField>;
{$IF Defined(CPUARM64) or Defined(LINUX64) or Defined(OSX64)}
//procedure GetHFAElement;
//function GetIsHFA: Boolean; override;
//function GetHFAElementType: TRttiFloatType; override;
//function GetHFAElementCount: Integer; override;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
{$ENDIF CPUARM64 or LINUX64 or OSX64}
protected
//function GetTypeSize: Integer; override;
{$IF Defined(CPUARM64) or Defined(LINUX64) or Defined(OSX64)}
//property HFAHasInsufficientTypeInformation: Boolean read FHFAHasInsufficientTypeInformation;
{$ENDIF CPUARM64 or LINUX64 or OSX64}
procedure init(const aRttiType: TRttiType); override;
public
//function GetDeclaredFields: TArray<TRttiField>; override;
//function GetDeclaredMethods: TArray<TRttiMethod>; override;
//function GetAttributes: TArray<TCustomAttribute>; override;
//property ManagedFields: TArray<TRttiManagedField> read GetManagedFields;
end;
{************************************************}
TALRttiInstanceType = class(TALRttiStructuredType)
private
fMetaclassType: TClass;
//FProps: TArray<TRttiProperty>;
//FMeths: TArray<TRttiMethod>;
//FVirtCount: Word;
//FIndexedProps: TArray<TRttiIndexedProperty>;
//FClassTab: PVmtFieldClassTab;
//FReadPropData: Boolean;
//FReadMethData: Boolean;
//procedure ReadPropData;
//procedure ReadMethData;
//function GetBaseType: TRttiType; override;
//function GetBaseTyped: TRttiInstanceType;
//function GetMetaclassType: TClass;
//function GetDeclaringUnitName: string;
//function GetVmtSize: Integer;
//constructor Create(APackage: TRttiPackage; AParent: TRttiObject; var P: PByte); override;
protected
procedure init(const aRttiType: TRttiType); override;
public
//property BaseType: TRttiInstanceType read GetBaseTyped;
//property DeclaringUnitName: string read GetDeclaringUnitName;
property MetaclassType: TClass read fMetaclassType;
//function GetDeclaredProperties: TArray<TRttiProperty>; override;
//function GetDeclaredMethods: TArray<TRttiMethod>; override;
//function GetDeclaredFields: TArray<TRttiField>; override;
//function GetDeclaredIndexedProperties: TArray<TRttiIndexedProperty>; override;
//function GetDeclaredImplementedInterfaces: TArray<TRttiInterfaceType>;
//function GetImplementedInterfaces: TArray<TRttiInterfaceType>;
// Members declared in this type only.
//function GetAttributes: TArray<TCustomAttribute>; override;
//property VmtSize: Integer read GetVmtSize;
end;
{****************************************}
TALInitAttribute = class(TCustomAttribute)
private
FParams: TALRttiString;
public
constructor Create(const AParams: TALRttiString);
property Params: TALRttiString read FParams;
end;
//This function is used solely to ensure RTTI generation
//for specific classes. This becomes necessary when the given
//classes aren't explicitly referenced anywhere in the code.
//Including them as a parameter here acts as a workaround
//to ensure that RTTI information is generated for them.
procedure ALEnsureRTTIgeneration(AClass: TClass);
{*********************************************************************************}
function ALGetEnumNameA(TypeInfo: PTypeInfo; Value: Integer): ansistring; overload;
function ALGetEnumNameA(PropInfo: PPropInfo; Value: Integer): ansistring; inline; overload;
function ALGetEnumNameW(TypeInfo: PTypeInfo; Value: Integer): string; inline; overload;
function ALGetEnumNameW(PropInfo: PPropInfo; Value: Integer): string; inline; overload;
function ALTryGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring; Var EnumValue: Integer): boolean; overload;
function ALTryGetEnumValue(PropInfo: PPropInfo; const Name: ansistring; Var EnumValue: Integer): boolean; inline; overload;
function ALTryGetEnumValue(TypeInfo: PTypeInfo; const Name: string; Var EnumValue: Integer): boolean; overload;
function ALTryGetEnumValue(PropInfo: PPropInfo; const Name: string; Var EnumValue: Integer): boolean; inline; overload;
function ALGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring): Integer; overload;
function ALGetEnumValue(PropInfo: PPropInfo; const Name: ansistring): Integer; inline; overload;
function ALGetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer; overload;
function ALGetEnumValue(PropInfo: PPropInfo; const Name: string): Integer; inline; overload;
function ALSetToStringA(TypeInfo: PTypeInfo; Value: Integer; const Brackets: Boolean = False): ansistring; overload;
function ALSetToStringA(PropInfo: PPropInfo; Value: Integer; const Brackets: Boolean = False): ansistring; inline; overload;
function ALSetToStringW(TypeInfo: PTypeInfo; Value: Integer; const Brackets: Boolean = False): string; inline; overload;
function ALSetToStringW(PropInfo: PPropInfo; Value: Integer; const Brackets: Boolean = False): string; inline; overload;
function ALTryStringToSet(TypeInfo: PTypeInfo; const Value: ansistring; Var SetInt: Integer): Boolean; overload;
function ALTryStringToSet(PropInfo: PPropInfo; const Value: ansistring; Var SetInt: Integer): Boolean; inline; overload;
function ALTryStringToSet(TypeInfo: PTypeInfo; const Value: string; Var SetInt: Integer): Boolean; overload;
function ALTryStringToSet(PropInfo: PPropInfo; const Value: string; Var SetInt: Integer): Boolean; inline; overload;
function ALStringToSet(TypeInfo: PTypeInfo; const Value: ansistring): Integer; overload;
function ALStringToSet(PropInfo: PPropInfo; const Value: ansistring): Integer; inline; overload;
function ALStringToSet(TypeInfo: PTypeInfo; const Value: string): Integer; overload;
function ALStringToSet(PropInfo: PPropInfo; const Value: string): Integer; inline; overload;
function ALGetRttiType(const aQualifiedName: TALRttiString; const aRaiseExceptionIfNotFound: Boolean = True): TALRttiType;
procedure ALRttiInitializeInstance(const AInstance: TObject);
procedure ALRttiFinalizeInstance(const AInstance: TObject);
procedure ALRttiInitialization(
const aQualifiedNameToInclude: array of TALRttiString; // ['*'] to include everything
const AQualifiedNameToExclude: array of TALRttiString); overload; // [] to Exclude nothing
procedure ALRttiInitialization; overload;
procedure ALRttiFinalization;
implementation
uses
System.Masks,
System.sysutils,
System.Generics.Defaults,
System.AnsiStrings,
System.Diagnostics,
Alcinoe.Common,
Alcinoe.StringList,
Alcinoe.StringUtils;
{*}
var
ALRTTIContext: TRttiContext;
ALRttiTypeCache: TObjectDictionary<TALRttiString,TALRttiType>;
{***}
const
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.BooleanIdents is still the same and adjust the IFDEF'}
{$IFEND}
ALBooleanIdentsA: array [Boolean] of AnsiString = ('False', 'True');
ALBooleanIdentsW: array [Boolean] of String = ('False', 'True');
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.AfterString is still the same and adjust the IFDEF'}
{$IFEND}
// P points a length field of ShortString.
function ALAfterString(const P: PByte): Pointer; inline;
begin
Result := P + P^ + 1;
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.GetEnumName(TypeInfo: PTypeInfo; Value: Integer... is still the same and adjust the IFDEF'}
{$IFEND}
function ALGetEnumNameA(TypeInfo: PTypeInfo; Value: Integer): ansistring;
var
P: Pointer;
T: PTypeData;
Len: Byte;
begin
if TypeInfo^.Kind = tkInteger then
begin
Result := ALIntToStrA(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 := ALBooleanIdentsA[Value <> 0];
if SameText(HexDisplayPrefix, '0x') then
Result := ALLowerCase(Result);
end
else
begin
P := @T^.NameList;
while Value <> 0 do
begin
P := ALAfterString(P);
Dec(Value);
end;
//Result := UTF8IdentToString(PShortString(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 ALGetEnumNameA(PropInfo: PPropInfo; Value: Integer): ansistring;
begin
result := ALGetEnumNameA(PropInfo^.PropType^, Value);
end;
{*******************************************************************}
function ALGetEnumNameW(TypeInfo: PTypeInfo; Value: Integer): string;
begin
result := System.TypInfo.GetEnumName(TypeInfo, Value);
end;
{*******************************************************************}
function ALGetEnumNameW(PropInfo: PPropInfo; Value: Integer): string;
begin
result := System.TypInfo.GetEnumName(PropInfo^.PropType^, Value);
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.GetEnumNameValue(TypeInfo: PTypeInfo... is still the same and adjust the IFDEF'}
{$IFEND}
function ALGetEnumNameValue(TypeInfo: PTypeInfo; const Name: AnsiString): Integer; overload;
var
TypeData: PTypeData;
LName: PByte;
I: Integer;
LLen1: Integer;
LLen2: Integer;
begin
TypeData := GetTypeData(GetTypeData(TypeInfo)^.BaseType^);
LName := PByte(@TypeData^.NameList);
//useless optimization with ansistring as we do LLen2 := Length(Name)
//instead of LLen2 := UTF8IdentLength(Name)
//if TypeData^.MaxValue >= 4 then
//begin
LLen2 := Length(Name);
for I := 0 to TypeData^.MaxValue do
begin
LLen1 := PByte(LName)^; // length is store in First array
if (LLen1 = LLen2) and
ALSameTextA(PShortString(LName)^, Name) then
Exit(I);
LName := ALAfterString(LName);
end;
//end
//else
//begin
// for I := 0 to TypeData^.MaxValue do
// begin
// if ALSameTextA(PShortString(LName)^, Name) then
// Exit(I);
// LName := ALAfterString(LName);
// end;
//end;
//We can not support Alias (IE: alLeft = TAlignLayout.Left) because the global
//var EnumAliases is private to System.TypInfo
//Result := GetAliasEnumValue(TypeInfo, Name);
Result := -1;
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.GetEnumValue(TypeInfo: PTypeInfo... is still the same and adjust the IFDEF'}
{$IFEND}
function ALTryGetEnumValue(TypeInfo: PTypeInfo; const Name: ansistring; Var EnumValue: Integer): boolean;
begin
if (TypeInfo = nil) or (Name = '') or (Length(Name) > 255) then Exit(false);
if TypeInfo^.Kind = tkInteger then
Result := ALTryStrToInt(Name, EnumValue)
else
begin
if (TypeInfo^.Kind <> tkEnumeration) then exit(false);
if GetTypeData(TypeInfo)^.MinValue < 0 then // Longbool/wordbool/bytebool
begin
if ALSameTextA(Name, ALBooleanIdentsA[False]) then begin
EnumValue := 0;
Result := true;
end
else if ALSameTextA(Name, ALBooleanIdentsA[True]) then begin
EnumValue := -1;
Result := true;
end
else
Result := ALTryStrToInt(Name, EnumValue);
end
else begin
EnumValue := ALGetEnumNameValue(TypeInfo, Name);
result := EnumValue > -1;
end;
end;
end;
{*******************************************************************************************************}
function ALTryGetEnumValue(PropInfo: PPropInfo; const Name: ansistring; Var EnumValue: Integer): boolean;
begin
result := ALTryGetEnumValue(PropInfo^.PropType^, Name, EnumValue);
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.GetEnumNameValue(TypeInfo: PTypeInfo... is still the same and adjust the IFDEF'}
{$IFEND}
function ALGetEnumNameValue(TypeInfo: PTypeInfo; const Name: String): Integer; overload;
var
TypeData: PTypeData;
LName: PByte;
I: Integer;
LLen1: Integer;
LLen2: Integer;
begin
TypeData := GetTypeData(GetTypeData(TypeInfo)^.BaseType^);
LName := PByte(@TypeData^.NameList);
if TypeData^.MaxValue >= 4 then
begin
LLen2 := UTF8IdentLength(Name);
for I := 0 to TypeData^.MaxValue do
begin
LLen1 := PByte(LName)^;
if (LLen1 = LLen2) and
UTF8IdentStringCompare(PShortString(LName), Name) then
Exit(I);
LName := ALAfterString(LName);
end;
end
else
begin
for I := 0 to TypeData^.MaxValue do
begin
if UTF8IdentStringCompare(PShortString(LName), Name) then
Exit(I);
LName := ALAfterString(LName);
end;
end;
//We can not support Alias (IE: alLeft = TAlignLayout.Left) because the global
//var EnumAliases is private to System.TypInfo
//Result := GetAliasEnumValue(TypeInfo, Name);
Result := -1;
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.GetEnumValue(TypeInfo: PTypeInfo... is still the same and adjust the IFDEF'}
{$IFEND}
function ALTryGetEnumValue(TypeInfo: PTypeInfo; const Name: string; Var EnumValue: Integer): boolean;
begin
if (TypeInfo = nil) or (Name = '') or (Length(Name) > 255) then Exit(false);
if TypeInfo^.Kind = tkInteger then
Result := ALTryStrToInt(Name, EnumValue)
else
begin
if (TypeInfo^.Kind <> tkEnumeration) then exit(false);
if GetTypeData(TypeInfo)^.MinValue < 0 then // Longbool/wordbool/bytebool
begin
if ALSameTextW(Name, ALBooleanIdentsW[False]) then begin
EnumValue := 0;
Result := true;
end
else if ALSameTextW(Name, ALBooleanIdentsW[True]) then begin
EnumValue := -1;
Result := true;
end
else
Result := ALTryStrToInt(Name, EnumValue);
end
else begin
EnumValue := ALGetEnumNameValue(TypeInfo, Name);
result := EnumValue > -1;
end;
end;
end;
{***************************************************************************************************}
function ALTryGetEnumValue(PropInfo: PPropInfo; const Name: string; 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 ALGetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;
begin
if not ALTryGetEnumValue(TypeInfo, Name, result) then
raise EALException.CreateFmt('Invalid enumeration name: %s', [Name]);
end;
{************************************************************************}
function ALGetEnumValue(PropInfo: PPropInfo; const Name: string): Integer;
begin
result := ALGetEnumValue(PropInfo^.PropType^, Name);
end;
{**}
type
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.TLargestSet/PLargestSet is still the same and adjust the IFDEF'}
{$IFEND}
TLargestSet = set of byte;
PLargestSet = ^TLargestSet;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.SetToString(TypeInfo: PTypeInfo; Value: Integer... is still the same and adjust the IFDEF'}
{$IFEND}
function ALSetToStringA(TypeInfo: PTypeInfo; Value: Integer; const Brackets: Boolean = False): ansistring;
var
ElementType: PPTypeInfo;
S: TIntegerSet;
I: Integer;
begin
Result := '';
Integer(S) := Value;
ElementType := GetTypeData(TypeInfo)^.CompType;
if ElementType <> nil then
begin
for I := 0 to SizeOf(Integer) * 8 - 1 do
if I in S then
begin
if Result <> '' then
Result := Result + ',';
Result := Result + ALGetEnumNameA(ElementType^, I);
end;
end
else
begin
for I := 0 to SizeOf(Integer) * 8 - 1 do
if I in S then
begin
if Result <> '' then
Result := Result + ',';
Result := Result + ALIntToStrA(I);
end;
end;
if Brackets then
Result := '[' + Result + ']';
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.SetToString(PropInfo: PPropInfo; Value: Integer... is still the same and adjust the IFDEF'}
{$IFEND}
function ALSetToStringA(PropInfo: PPropInfo; Value: Integer; const Brackets: Boolean = False): ansistring;
begin
Result := ALSetToStringA(PropInfo^.PropType^, Value, Brackets);
end;
{****************************************************************************************************}
function ALSetToStringW(TypeInfo: PTypeInfo; Value: Integer; const Brackets: Boolean = False): string;
begin
System.TypInfo.SetToString(TypeInfo, Value, Brackets);
end;
{****************************************************************************************************}
function ALSetToStringW(PropInfo: PPropInfo; Value: Integer; const Brackets: Boolean = False): string;
begin
System.TypInfo.SetToString(PropInfo, Value, Brackets);
end;
{*************************************}
{$IFNDEF ALCompilerVersionSupported122}
{$MESSAGE WARN 'Check if System.TypInfo.StringToSet(TypeInfo: PTypeInfo; const Value: string)... is still the same and adjust the IFDEF'}
{$IFEND}
function ALTryStringToSet(TypeInfo: PTypeInfo; const Value: ansistring; Var SetInt: Integer): Boolean;
var
P: PAnsiChar;
EnumName: AnsiString;
EnumValue: Integer;
PEnumInfo: PPTypeInfo;