-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDelphiUiLib.LiteReflection.pas
More file actions
888 lines (753 loc) · 23 KB
/
Copy pathDelphiUiLib.LiteReflection.pas
File metadata and controls
888 lines (753 loc) · 23 KB
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
unit DelphiUiLib.LiteReflection;
{
This module provides lightweight reflection support for represnting various
types.
}
interface
uses
DelphiApi.Reflection, DelphiUtils.LiteRTTI;
type
TRttixReflectionFormat = (rfText, rfHint);
TRttixReflectionFormats = set of TRttixReflectionFormat;
TRttixFieldReflectionOption = (
frDoNotAggregate, // Return only immediate fields; don't honor [Aggregate]
frIncludeUntyped, // Include fields without type information
frIncludeUnlisted, // Include fields marked as [Unlisted]
frIncludeInternal, // Include fields marked as [RecordSize] or [Offset]
frIncludeNewerVersions // Include fields for newer OS version than the current
);
TRttixFieldReflectionOptions = set of TRttixFieldReflectionOption;
TRttixFullReflection = record
ValidFormats: TRttixReflectionFormats;
Text: String;
Hint: String;
end;
IRttixTypeFormatter = interface
['{DDB726E6-F711-46CB-943A-1C1EA0BE4C66}']
function GetRttixType: IRttixType;
function GetHasCustomFormatting: Boolean;
property RttixType: IRttixType read GetRttixType;
property HasCustomFormatting: Boolean read GetHasCustomFormatting;
function FormatText(const [ref] Instance): String;
function FormatHint(const [ref] Instance): String;
function Format(const [ref] Instance;
Formats: TRttixReflectionFormats): TRttixFullReflection;
end;
IRttixFieldFormatter = interface
['{457B9A50-E58B-4A23-8910-CD2E0F09E82E}']
function GetScopingType: IRttixType;
function GetRecordType: IRttixRecordType;
function GetField: IRttixField;
function GetFormatter: IRttixTypeFormatter;
property ScopingType: IRttixType read GetScopingType;
property RecordType: IRttixRecordType read GetRecordType;
property Field: IRttixField read GetField;
property Formatter: IRttixTypeFormatter read GetFormatter;
function FormatText(const [ref] ScopingInstance): String;
function FormatHint(const [ref] ScopingInstance): String;
function Format(const [ref] ScopingInstance;
Formats: TRttixReflectionFormats): TRttixFullReflection;
end;
TRttixFullFieldReflection = record
Field: IRttixField;
Reflection: TRttixFullReflection;
end;
TRttixCustomTypeFormatter = function (
const RttixType: IRttixType;
const [ref] Instance;
RequestedFormats: TRttixReflectionFormats
): TRttixFullReflection;
// Add a callback for formatting a custom type
procedure RttixRegisterCustomTypeFormatter(
TypeInfo: PLiteRttiTypeInfo;
Formatter: TRttixCustomTypeFormatter
);
// Prepare a formatter for representing a specific type from its type info
function RttixMakeTypeFormatter(
[opt] TypeInfo: PLiteRttiTypeInfo;
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): IRttixTypeFormatter;
// Prepare formatter for representing a specific type from its RTTI info
function RttixMakeTypeFormatterForType(
[opt] const RttixType: IRttixType
): IRttixTypeFormatter;
// Prepare formatters for representing fields of a specific record
// or record pointer type from its type info
function RttixMakeFieldFormatters(
[opt] TypeInfo: PLiteRttiTypeInfo;
Options: TRttixFieldReflectionOptions = [];
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): TArray<IRttixFieldFormatter>;
// Prepare formatters for representing fields of a specific record
// or record pointer type from its RTTI info
function RttixMakeFieldFormattersForType(
[opt] const RttixType: IRttixType;
Options: TRttixFieldReflectionOptions = []
): TArray<IRttixFieldFormatter>;
// An attribute indicating that reflection should preserve constant names
function RttixPreserveEnumCase(
): TArray<PLiteRttiAttribute>;
// An attribute indicating that reflection should format numbers as hexadecimal
function RttixAsHex(
): TArray<PLiteRttiAttribute>;
// An attribute indicating that reflection should format numbers as byte sizes
function RttixAsBytes(
): TArray<PLiteRttiAttribute>;
// An attribute indicating that reflection should interpret numbers as ASCII
function RttixAsAsciiMagic(
): TArray<PLiteRttiAttribute>;
// An attribute indicating that reflection should not follow pointers
function RttixDontFollowPointers(
): TArray<PLiteRttiAttribute>;
// Represent a type as text from raw type info
function RttixFormat(
TypeInfo: PLiteRttiTypeInfo;
const [ref] Instance;
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): String;
// Represent a type as text and hint from raw type info
function RttixFormatFull(
TypeInfo: PLiteRttiTypeInfo;
const [ref] Instance;
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil;
RequestedFormats: TRttixReflectionFormats = [rfText, rfHint]
): TRttixFullReflection;
// Represent fields of a record type as text/hint from raw type info
function RttixFormatFields(
TypeInfo: PLiteRttiTypeInfo;
const [ref] Instance;
Formats: TRttixReflectionFormats;
Options: TRttixFieldReflectionOptions = [];
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): TArray<TRttixFullFieldReflection>;
type
Rttix = record
// Represent a known type as text from a generic parameter
class function Format<T>(
const Instance: T;
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): String; static;
// Represent a known type as text and hint from a generic parameter
class function FormatFull<T>(
const Instance: T;
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): TRttixFullReflection; static;
// Represent fields of a known record or record pointer type as text/hint
// from a generic parameter
class function FormatFields<T>(
const Instance: T;
Formats: TRttixReflectionFormats;
Options: TRttixFieldReflectionOptions = [];
const ExtraAttributes: TArray<PLiteRttiAttribute> = nil
): TArray<TRttixFullFieldReflection>; static;
end;
implementation
uses
DelphiApi.TypInfo, NtUtils.SysUtils, DelphiUtils.Arrays, DelphiUiLib.Strings,
DelphiUtils.AutoObjects, Ntapi.Versions;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Custom Formatters }
type
TRttixFormatterEntry = record
TypeInfo: PLiteRttiTypeInfo;
Formatter: TRttixCustomTypeFormatter;
end;
var
RttixKnownFormatters: TArray<TRttixFormatterEntry>;
function RttixFindTypeIndex(
TypeInfo: PLiteRttiTypeInfo
): Integer;
begin
Result := TArray.BinarySearchEx<TRttixFormatterEntry>(RttixKnownFormatters,
function (const Entry: TRttixFormatterEntry): Integer
var
Difference: IntPtr;
begin
{$Q-}{$R-}
Difference := IntPtr(Entry.TypeInfo) - IntPtr(TypeInfo);
{$IFDEF R+}{$R+}{$ENDIF}{$IFDEF Q+}{$Q+}{$ENDIF}
if Difference > 0 then
Result := 1
else if Difference < 0 then
Result := -1
else
Result := 0;
end
);
end;
procedure RttixRegisterCustomTypeFormatter;
var
Index: Integer;
Entry: TRttixFormatterEntry;
begin
Entry.TypeInfo := TypeInfo;
Entry.Formatter := Formatter;
Index := RttixFindTypeIndex(TypeInfo);
if Index < 0 then
Insert(Entry, RttixKnownFormatters, -(Index + 1))
else
RttixKnownFormatters[Index] := Entry;
end;
{ Type sub-kind formatters }
function RttixPrettifyName(
const Input: String;
NamingStyle: TNamingStyle;
const Prefix: String;
const Suffix: String
): String;
begin
Result := Input;
RtlxPrefixStripString(Prefix, Result, True);
RtlxSuffixStripString(Suffix, Result, True);
if NamingStyle <> nsPreserveCase then
Result := RtlxPrettifyIdentifier(Result);
end;
function RttixFormatEnum(
const EnumType: IRttixEnumType;
const [ref] Instance
): String;
var
Value: Cardinal;
TypeName: String;
Size: TIntegerSize;
begin
Value := EnumType.ReadInstance(Instance);
if EnumType.IsValidValue(Value) then
begin
Result := RttixPrettifyName(
EnumType.TypeInfo.EnumerationName(Integer(Value)),
EnumType.NamingStyle,
EnumType.Prefix,
EnumType.Suffix
);
end
else
begin
TypeName := EnumType.SDKName;
if TypeName = '' then
TypeName := EnumType.TypeInfo.Name;
case EnumType.Size of
SizeOf(Byte): Size := isByte;
SizeOf(Word): Size := isWord;
SizeOf(Cardinal): Size := isCardinal;
else
Error(reAssertionFailed);
end;
Result := '(' + TypeName + ' of ' + RtlxIntToDec(Value, Size, isSigned, 0,
npSpace) + ')';
end;
end;
function RttixFormatEnumHint(
const EnumType: IRttixEnumType;
const [ref] Instance
): String;
var
Value: Cardinal;
begin
Value := EnumType.ReadInstance(Instance);
Result := BuildHint([
THintSection.New('Value (decimal)', UiLibUIntToDec(Value)),
THintSection.New('Value (hex)', UiLibUIntToHex(Value))
]);
end;
function RttixFormatBool(
const BoolType: IRttixBoolType;
const [ref] Instance
): String;
begin
Result := BooleanToString(BoolType.ReadInstance(Instance),
BoolType.BooleanKind);
end;
function RttixFormatBitwise(
const BitwiseType: IRttixBitwiseType;
const [ref] Instance
): String;
var
Value, ExcludedMask: UInt64;
Matched: TArray<String>;
i, Count: Integer;
begin
Value := BitwiseType.ReadInstance(Instance);
// Record found bits
SetLength(Matched, Length(BitwiseType.Flags) + 1);
Count := 0;
ExcludedMask := 0;
for i := 0 to High(BitwiseType.Flags) do
if (BitwiseType.Flags[i].Mask and ExcludedMask = 0) and
(Value and BitwiseType.Flags[i].Mask = BitwiseType.Flags[i].Value) then
begin
Value := Value and not BitwiseType.Flags[i].Mask;
ExcludedMask := ExcludedMask or BitwiseType.Flags[i].Mask;
Matched[Count] := RttixPrettifyName(
BitwiseType.Flags[i].Name,
BitwiseType.NamingStyle,
BitwiseType.Prefix,
BitwiseType.Suffix
);
Inc(Count);
end;
// Record unknown bits
if Value <> 0 then
begin
Matched[Count] := UiLibUIntToHex(Value, BitwiseType.MinDigits or
NUMERIC_WIDTH_ROUND_TO_GROUP);
Inc(Count);
end;
// Trim
SetLength(Matched, Count);
// Combine
if Length(Matched) > 0 then
Result := RtlxJoinStrings(Matched, ', ')
else
Result := '(None)';
end;
function RttixFormatBitwiseHint(
const BitwiseType: IRttixBitwiseType;
const [ref] Instance
): String;
var
HasNonEnumFlags: Boolean;
Value: UInt64;
Checkboxes: TArray<String>;
i: Integer;
begin
// Only format hints if there are any flags, not just sub-enums
HasNonEnumFlags := False;
for i := 0 to High(BitwiseType.Flags) do
if BitwiseType.Flags[i].Kind = rbkFlag then
begin
HasNonEnumFlags := True;
Break;
end;
Value := BitwiseType.ReadInstance(Instance);
Result := BuildHint('Value', UiLibUIntToHex(Value, BitwiseType.MinDigits or
NUMERIC_WIDTH_ROUND_TO_GROUP));
if not HasNonEnumFlags then
Exit;
SetLength(Checkboxes, Length(BitwiseType.Flags));
for i := 0 to High(BitwiseType.Flags) do
Checkboxes[i] := ' ' + CheckboxToString(Value and BitwiseType.Flags[i].Mask
= BitwiseType.Flags[i].Value) + ' ' + RttixPrettifyName(
BitwiseType.Flags[i].Name, BitwiseType.NamingStyle, BitwiseType.Prefix,
BitwiseType.Suffix) + ' ';
Result := 'Flags: '#$D#$A + RtlxJoinStrings(Checkboxes, #$D#$A) +
#$D#$A + Result;
end;
function RttixFormatDigits(
const DigitsType: IRttixDigitsType;
const [ref] Instance
): String;
var
Size: TIntegerSize;
Sign: TIntegerSign;
Value: UInt64;
begin
Value := DigitsType.ReadInstance(Instance);
case DigitsType.Size of
SizeOf(Byte): Size := isByte;
SizeOf(Word): Size := isWord;
SizeOf(Cardinal): Size := isCardinal;
else
Size := isUInt64;
end;
if DigitsType.Signed then
Sign := isSigned
else
Sign := isUnsigned;
case DigitsType.DigitsKind of
rokDecimal:
Result := RtlxIntToDec(Value, Size, Sign, 0, npSpace);
rokHex:
Result := RtlxIntToStr(Value, nsHexadecimal, DigitsType.MinHexDigits or
NUMERIC_WIDTH_ROUND_TO_GROUP, Size, Sign, [nsHexadecimal], npSpace);
rokBytes:
Result := UiLibBytesToString(Value);
rokAscii:
Result := UiLibAsciiMagicToString(Value);
else
Error(reAssertionFailed);
end;
end;
function RttixFormatDigitsHint(
const DigitsType: IRttixDigitsType;
const [ref] Instance
): String;
var
Value: UInt64;
begin
Value := DigitsType.ReadInstance(Instance);
case DigitsType.DigitsKind of
rokDecimal, rokAscii:
Result := BuildHint('Value (hex)', UiLibUIntToHex(Value));
rokHex:
Result := BuildHint('Value (decimal)', UiLibUIntToDec(Value));
rokBytes:
Result := BuildHint([
THintSection.New('Value (decimal)', UiLibUIntToDec(Value)),
THintSection.New('Value (hex)', UiLibUIntToHex(Value))
]);
end;
end;
function RttixFormatString(
const StringType: IRttixStringType;
const [ref] Instance
): String;
begin
Result := StringType.ReadInstance(Instance);
end;
function RttixFormatPointer(
const PointerType: IRttixPointerType;
const [ref] Instance
): String;
var
Value: Pointer absolute Instance;
begin
if Assigned(Value) then
Result := '(' + UiLibUIntToHex(UIntPtr(Value)) + ' as ' +
PointerType.TypeInfo.Name + ')'
else
Result := '(nil)';
end;
{ Formatters }
type
TRttixTypeFormatter = class (TAutoInterfacedObject, IRttixTypeFormatter)
FRttixType: IRttixType;
FFormatter: TRttixCustomTypeFormatter;
FInner: IRttixTypeFormatter;
function GetRttixType: IRttixType;
function GetHasCustomFormatting: Boolean;
function FormatText(const [ref] Instance): String;
function FormatHint(const [ref] Instance): String;
function Format(const [ref] Instance;
Formats: TRttixReflectionFormats): TRttixFullReflection;
constructor Create(
[opt] const RttixType: IRttixType;
[opt] Formatter: TRttixCustomTypeFormatter
);
end;
constructor TRttixTypeFormatter.Create;
var
PointerType: IRttixPointerType;
begin
FRttixType := RttixType;
FFormatter := Formatter;
// Pointer types delegate formatting to the referenced type
if not Assigned(FFormatter) and Assigned(FRttixType) and
(FRttixType.SubKind = rtkPointer) then
begin
PointerType := FRttixType as IRttixPointerType;
if not PointerType.DontFollow and Assigned(PointerType.ReferncedType) then
FInner := RttixMakeTypeFormatterForType(PointerType.ReferncedType);
end;
end;
function TRttixTypeFormatter.Format;
begin
// Use the custom formatter first, then delegate pointer formatting
if Assigned(FFormatter) then
Result := FFormatter(FRttixType, Instance, Formats)
else if Assigned(FInner) and Assigned(Pointer(Instance)) then
Result := FInner.Format(Pointer(Instance)^, Formats)
else
Result.ValidFormats := [];
// Fall back to generic text formatting
if (rfText in Formats) and not (rfText in Result.ValidFormats) then
begin
if Assigned(FRttixType) then
case FRttixType.SubKind of
rtkEnumeration:
Result.Text := RttixFormatEnum(FRttixType as IRttixEnumType,
Instance);
rtkBoolean:
Result.Text := RttixFormatBool(FRttixType as IRttixBoolType,
Instance);
rtkBitwise:
Result.Text := RttixFormatBitwise(FRttixType as IRttixBitwiseType,
Instance);
rtkDigits:
Result.Text := RttixFormatDigits(FRttixType as IRttixDigitsType,
Instance);
rtkString:
Result.Text := RttixFormatString(FRttixType as IRttixStringType,
Instance);
rtkPointer:
Result.Text := RttixFormatPointer(FRttixType as IRttixPointerType,
Instance);
else
Result.Text := '(' + FRttixType.TypeInfo.Name + ')';
end
else
Result.Text := '(no type info)';
Include(Result.ValidFormats, rfText);
end;
// Fall back to generic hint formatting
if (rfHint in Formats) and not (rfHint in Result.ValidFormats) then
begin
if Assigned(FRttixType) then
case FRttixType.SubKind of
rtkEnumeration:
Result.Hint := RttixFormatEnumHint(FRttixType as IRttixEnumType,
Instance);
rtkBitwise:
Result.Hint := RttixFormatBitwiseHint(FRttixType as IRttixBitwiseType,
Instance);
rtkDigits:
Result.Hint := RttixFormatDigitsHint(FRttixType as IRttixDigitsType,
Instance);
else
Result.Hint := '';
end
else
Result.Hint := '';
Include(Result.ValidFormats, rfHint);
end;
end;
function TRttixTypeFormatter.FormatHint;
begin
Result := Format(Instance, [rfHint]).Hint;
end;
function TRttixTypeFormatter.FormatText;
begin
Result := Format(Instance, [rfText]).Text;
end;
function TRttixTypeFormatter.GetHasCustomFormatting;
begin
Result := Assigned(FFormatter);
end;
function TRttixTypeFormatter.GetRttixType;
begin
Result := FRttixType;
end;
function RttixMakeTypeFormatter;
var
Index: Integer;
RttixType: IRttixType;
Formatter: TRttixCustomTypeFormatter;
begin
Formatter := nil;
if Assigned(TypeInfo) then
begin
RttixType := RttixTypeInfo(TypeInfo, ExtraAttributes);
Index := RttixFindTypeIndex(TypeInfo);
if Index >= 0 then
Formatter := RttixKnownFormatters[Index].Formatter;
end
else
RttixType := nil;
Result := TRttixTypeFormatter.Create(RttixType, Formatter);
end;
function RttixMakeTypeFormatterForType;
var
Index: Integer;
Formatter: TRttixCustomTypeFormatter;
begin
Formatter := nil;
if Assigned(RttixType) then
begin
Index := RttixFindTypeIndex(RttixType.TypeInfo);
if Index >= 0 then
Formatter := RttixKnownFormatters[Index].Formatter;
end;
Result := TRttixTypeFormatter.Create(RttixType, Formatter);
end;
type
TRttixFieldFormatter = class (TAutoInterfacedObject, IRttixFieldFormatter)
FScopingType: IRttixType;
FRecordType: IRttixRecordType;
FPointerDepth: Integer;
FField: IRttixField;
FFormatter: IRttixTypeFormatter;
function GetScopingType: IRttixType;
function GetRecordType: IRttixRecordType;
function GetField: IRttixField;
function GetFormatter: IRttixTypeFormatter;
function FormatText(const [ref] ScopingInstance): String;
function FormatHint(const [ref] ScopingInstance): String;
function Format(const [ref] ScopingInstance;
Formats: TRttixReflectionFormats): TRttixFullReflection;
constructor Create(
const ScopingType: IRttixType;
const RecordType: IRttixRecordType;
PointerDepth: Integer;
const Field: IRttixField
);
end;
constructor TRttixFieldFormatter.Create;
begin
inherited Create;
FScopingType := ScopingType;
FRecordType := RecordType;
FPointerDepth := PointerDepth;
FField := Field;
FFormatter := RttixMakeTypeFormatterForType(FField.FieldType);
end;
function TRttixFieldFormatter.Format;
var
RecordStart: PByte;
i: Integer;
begin
// Follow pointers until we find the record start
RecordStart := @ScopingInstance;
for i := 0 to Pred(FPointerDepth) do
RecordStart := PPointer(RecordStart)^;
// Locate the field and use its formatter
Result := FFormatter.Format((RecordStart + FField.Offset)^, Formats);
end;
function TRttixFieldFormatter.FormatHint;
begin
Result := Format(ScopingInstance, [rfHint]).Hint;
end;
function TRttixFieldFormatter.FormatText;
begin
Result := Format(ScopingInstance, [rfText]).Text;
end;
function TRttixFieldFormatter.GetField;
begin
Result := FField;
end;
function TRttixFieldFormatter.GetFormatter;
begin
Result := FFormatter;
end;
function TRttixFieldFormatter.GetRecordType;
begin
Result := FRecordType;
end;
function TRttixFieldFormatter.GetScopingType;
begin
Result := FScopingType;
end;
function RttixFieldFilter(
Options: TRttixFieldReflectionOptions
): TCondition<IRttixField>;
var
CurrentVersion: TWindowsVersion;
begin
CurrentVersion := RtlOsVersion;
Result := function (const Field: IRttixField): Boolean
begin
Result := (Assigned(Field.FieldType) or (frIncludeUntyped in Options)) and
(not Field.Unlisted or (frIncludeUnlisted in Options)) and
(not (Field.IsRecordSize or Field.IsOffset) or (frIncludeInternal in Options)) and
((Field.MinOsVersion <= CurrentVersion) or (frIncludeNewerVersions in Options));
end;
end;
function RttixMakeFieldFormatters;
begin
if not Assigned(TypeInfo) then
Exit(nil);
Result := RttixMakeFieldFormattersForType(RttixTypeInfo(TypeInfo,
ExtraAttributes), Options);
end;
function RttixMakeFieldFormattersForType;
var
NestedType: IRttixType;
RecordType: IRttixRecordType;
PointerDepth: Integer;
Fields: TArray<IRttixField>;
i: Integer;
begin
if not Assigned(RttixType) then
Exit(nil);
PointerDepth := 0;
// Determine the pointer depth and extract the nested record type
NestedType := RttixType;
while Assigned(NestedType) and (NestedType.SubKind = rtkPointer) do
begin
NestedType := (NestedType as IRttixPointerType).ReferncedType;
Inc(PointerDepth);
end;
if not Assigned(NestedType) or (NestedType.SubKind <> rtkRecord) then
Exit(nil);
RecordType := NestedType as IRttixRecordType;
// Collect fields
if frDoNotAggregate in Options then
Fields := RecordType.ImmediateFields
else
Fields := RecordType.EffectiveFields;
// Remove uninteresting fields
TArray.FilterInline<IRttixField>(Fields, RttixFieldFilter(Options));
// Prepare formatters
SetLength(Result, Length(Fields));
for i := 0 to High(Fields) do
Result[i] := TRttixFieldFormatter.Create(RttixType, RecordType,
PointerDepth, Fields[i]);
end;
function RttixPreserveEnumCase;
type
[NamingStyle(nsPreserveCase)]
PreserveCase = type Pointer;
begin
Result := RttixTypeInfo(TypeInfo(PreserveCase)).Attributes;
end;
function RttixAsHex;
type
[Hex]
AsHex = type Pointer;
begin
Result := RttixTypeInfo(TypeInfo(AsHex)).Attributes;
end;
function RttixAsBytes;
type
[Bytes]
AsBytes = type Pointer;
begin
Result := RttixTypeInfo(TypeInfo(AsBytes)).Attributes;
end;
function RttixAsAsciiMagic;
type
[AsciiMagic]
AsAsciiMagic = type Pointer;
begin
Result := RttixTypeInfo(TypeInfo(AsAsciiMagic)).Attributes;
end;
function RttixDontFollowPointers;
type
[DontFollow]
DontFollowPointers = type Pointer;
begin
Result := RttixTypeInfo(TypeInfo(DontFollowPointers)).Attributes;
end;
{ Common }
function RttixFormat;
var
Formatter: IRttixTypeFormatter;
begin
Formatter := RttixMakeTypeFormatter(TypeInfo, ExtraAttributes);
Result := Formatter.FormatText(Instance);
end;
function RttixFormatFull;
var
Formatter: IRttixTypeFormatter;
begin
Formatter := RttixMakeTypeFormatter(TypeInfo, ExtraAttributes);
Result := Formatter.Format(Instance, RequestedFormats);
end;
function RttixFormatFields;
var
Formatters: TArray<IRttixFieldFormatter>;
i: Integer;
begin
Formatters := RttixMakeFieldFormatters(TypeInfo, Options, ExtraAttributes);
SetLength(Result, Length(Formatters));
for i := 0 to High(Result) do
begin
Result[i].Field := Formatters[i].Field;
Result[i].Reflection := Formatters[i].Format(Instance, Formats);
end;
end;
class function Rttix.Format<T>;
begin
Result := RttixFormat(TypeInfo(T), Instance, ExtraAttributes);
end;
class function Rttix.FormatFields<T>;
begin
Result := RttixFormatFields(TypeInfo(T), Instance, Formats, Options,
ExtraAttributes);
end;
class function Rttix.FormatFull<T>;
begin
Result := RttixFormatFull(TypeInfo(T), Instance, ExtraAttributes);
end;
end.