Recent

Author Topic: [SOLVED] Static array RTTI  (Read 554 times)

avk

  • Hero Member
  • *****
  • Posts: 752
[SOLVED] Static array RTTI
« on: December 19, 2022, 08:02:22 am »
Suppose we have a static array type, such as:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyArray = array[1..3, 2..4, 0..1] of Integer;
  3.   ...
  4.  


Is it possible to recover the exact structure of some variable of this type using RTTI?

Thanks in advance.
« Last Edit: December 20, 2022, 06:45:22 am by avk »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Static array RTTI
« Reply #1 on: December 19, 2022, 12:38:21 pm »
Not sure about the number of DIMS but you can get the Lo(), HI() and length of each element at runtime. But you still need to know the Dim count.

 I have a class I wrote way back which does allow me to remap the array and of course determine the DIM layout at runtime. All memory layouts are contiguous and if changes are made during runtime they are remapped and that includes the number of DIM element.

 I Think in your case however, you could get away with using multiple arrays whereby each one can be determined and reconfigured that points to the next! etc.
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Static array RTTI
« Reply #2 on: December 19, 2022, 06:39:01 pm »
Is it possible to recover the exact structure of some variable of this type using RTTI?
Only if each dimension is defined by an explicit type.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses TypInfo;
  5.  
  6. procedure PrintArrayInfo(ArrTypeInfo: PTypeInfo);
  7. var
  8.   ArrTypeData: PTypeData;
  9.   ItemTypeInfo: PTypeInfo;
  10.   ItemTypeData: PTypeData;
  11.   i: SizeInt;
  12. begin
  13.   ArrTypeData := GetTypeData(ArrTypeInfo);
  14.   Writeln(ArrTypeInfo^.Name, ' is ', ArrTypeData^.ArrayData.DimCount,
  15.     ' dimensions array of ', ArrTypeData^.ArrayData.ElType^.Name);
  16.   for i := 0 to ArrTypeData^.ArrayData.DimCount - 1 do
  17.   begin
  18.     ItemTypeInfo := ArrTypeData^.ArrayData.Dims[i];
  19.     ItemTypeData := GetTypeData(ItemTypeInfo);
  20.     Writeln(ItemTypeInfo^.Name, ' ', ItemTypeData^.MinValue, '..', ItemTypeData^.MaxValue);
  21.   end;
  22. end;
  23.  
  24. type
  25.   TRange1 = 17..19;
  26.   TRange2 = 123..124;
  27.   TRange3 = 5..5;
  28.   TMyArray = array[TRange1, TRange2, TRange3, 7..8] of string;
  29.  
  30. begin
  31.   PrintArrayInfo(TypeInfo(TMyArray));
  32.   Readln;
  33. end.
Result:
Code: Text  [Select][+][-]
  1. TMyArray is 4 dimensions array of ShortString
  2. TRange1 17..19
  3. TRange2 123..124
  4. TRange3 5..5
  5. ShortInt -128..127

As far as I know, it's the same in Delphi - if the array range is not declared, it does not fall into RTTI.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: [SOLVED] Static array RTTI
« Reply #3 on: December 20, 2022, 06:45:36 am »
Thank you all very much for your help.

 

TinyPortal © 2005-2018