* * *

Author Topic: How to declare a variable absolute with respect to a dynamic array?  (Read 2315 times)

molly

  • Hero Member
  • *****
  • Posts: 1619
Re: How to declare a variable absolute with respect to a dynamic array?
« Reply #15 on: February 19, 2017, 07:30:45 am »
As there obviously not is access to an implementation independent view of array descriptors, not even concerning ElementCount / High and ElementSize, I might either design a class providing this in it's own way, or write procedures directly getting passed the needed information via dedicated parameters.
The first isn't true, see code below. (ok, ok.. i cheated by providing that info  ;D )

For the later you'd better as otherwise things becomes a mess. What will happen if someone would to pass a simple integer to your function, or a byte ? It is called an untyped parameter for a reason  :D

Code: Pascal  [Select]
  1. program something;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4. {$RANGECHECKS ON}
  5.  
  6. uses
  7.   typinfo;
  8.  
  9. Type
  10.   PByteArray = ^TByteArray;
  11.   TByteArray = array of byte;
  12.   TWordArray = array of Word;
  13.   TLongWordArray = array of LongWord;
  14.  
  15. procedure DoSomethingWithSomething(var v; ti: PTypeInfo);
  16. var
  17.   a   : PByte;
  18.   i   : integer;
  19. begin
  20.   a := Addr(v);
  21.  
  22.   if (ti^.Kind = tkDynArray) then
  23.   begin
  24.     if (DynArrayDim(ti) = 1) then
  25.     begin
  26.       for i := 0 to Pred(GetTypeData(ti)^.elSize * DynArraySize(pointer(v))) do
  27.       begin
  28.         Write('$', HexStr(a[i],2),' ');
  29.       end;
  30.       WriteLn;
  31.     end
  32.     else
  33.       WriteLn('ERROR: Multidimensional arrays are not supported');
  34.   end
  35.   else
  36.     WriteLn('ERROR: type ', ti^.kind, ' is unsupported');
  37. end;
  38.  
  39. var
  40.   x: TbyteArray;
  41.   y: TWordArray;
  42.   z: TLongWordArray;
  43. begin
  44.   x := TByteArray.Create(9,8,7,6,5,4,3,2,1);  
  45.   y := TWordArray.Create($1122,$3344,$5566,$7788,$9900);
  46.   z := TLongWordArray.Create($12345678,$23456789,$34567890);
  47.  
  48.   DoSomethingWithSomething(x,TypeInfo(x));
  49.   WriteLn;
  50.   DoSomethingWithSomething(y,TypeInfo(y));
  51.   WriteLn;
  52.   DoSomethingWithSomething(z,TypeInfo(z));
  53.   WriteLn;
  54.  
  55.   SetLength(x, 0);
  56.   SetLength(y, 0);
  57.   SetLength(z, 0);
  58. end.
  59.  
« Last Edit: February 19, 2017, 07:40:52 am by molly »

Lupp

  • New member
  • *
  • Posts: 30
Re: How to declare a variable absolute with respect to a dynamic array?
« Reply #16 on: February 19, 2017, 02:14:38 pm »
1) Thank you, @molly.
2) I did not yet know the unit 'typinfo'. (Do you know the reason for not naming it 'typeinfo'?)

3) It's not quite the style I would prefer as it seems to be against the grain somehow. (Implementing features the language doesn't want to support basically.)
3.a) Functionally I would see a language/compiler modification. However, I do not understand how registering packages works.
3.b) The 'TypeInfo' function returns a pointer that must be dereferenced explicitly.
3.c) I feel slightly reminded of the implemenattion of mouse-awareness by Turbo Pascal in old times.
3.d) What is returned by TypeInfo(ti^).Kind? (Or TypeInfo(ti^)^.Kind?)

4) Ok. There's a dilemma.  If I expect meta information to be availabel about variables, I will also expect to be able to save it to files.  (And to pass it to different applications?)
5) It's a mess. The world is too complicated. At least this fact is simple.
6) I would better stay silent.

7) I didn't understand the remark
Quote
(ok, ok.. i cheated by providing that info  ;D )
Suppose it's fun. A lesson in English -or whatever- available?
« Last Edit: February 19, 2017, 02:21:04 pm by Lupp »

howardpc

  • Hero Member
  • *****
  • Posts: 2160
Re: How to declare a variable absolute with respect to a dynamic array?
« Reply #17 on: February 22, 2017, 03:16:24 pm »
2) I did not yet know the unit 'typinfo'. (Do you know the reason for not naming it 'typeinfo'?)

Simply to avoid a name clash with the compiler-intrinsic TypeInfo() function that can be applied to any type at all, returning a PTypeInfo.

molly

  • Hero Member
  • *****
  • Posts: 1619
Re: How to declare a variable absolute with respect to a dynamic array?
« Reply #18 on: February 25, 2017, 02:12:40 pm »
1) Thank you, @molly.
You're welcome  :)

Quote
3) It's not quite the style I would prefer as it seems to be against the grain somehow. (Implementing features the language doesn't want to support basically.)
Pascal is a strongly typed language, you seem to want to 'overrule' the strong type. That causes issues that have to be dealt with.

Quote
3.a) Functionally I would see a language/compiler modification. However, I do not understand how registering packages works.
That will not happen. Because you made the parameter 'typeless' no rtti information is available ergo you are unable to determine its type using the usual constructs.

Quote
3.b) The 'TypeInfo' function returns a pointer that must be dereferenced explicitly.
yes.

Quote
3.c) I feel slightly reminded of the implemenattion of mouse-awareness by Turbo Pascal in old times.
You asked for 'trouble' so...  ;D

Quote
3.d) What is returned by TypeInfo(ti^).Kind? (Or TypeInfo(ti^)^.Kind?)
That depends on the type  :P

More information can be read inside documentation of unit typinfo (press the types section at the top).

Quote
4) Ok. There's a dilemma.  If I expect meta information to be availabel about variables, I will also expect to be able to save it to files.  (And to pass it to different applications?)
That should be no problem ?

Quote
5) It's a mess. The world is too complicated. At least this fact is simple.
That fact wasn't simple for me because this post slipped through my todo list....  :-[ sorry for the delay.

Quote
Suppose it's fun. A lesson in English -or whatever- available?
you originally wrote:

Quote
As there obviously not is access to an implementation independent view of array descriptors,

To which i replied:
Quote
The first isn't true, see code below.

But i later added, that i cheated. I cheated because _I_ am supplying the information that is needed for the routine in order to have a "implementation independent view of array descriptors""

You could just as well invent your own 'design' to solve the issue in a similar manner. Point being: you would have to provide the information about the array structure. A well designed record and passing that would also work but, is basically the same as what TTypeInfo does (and the RTTI does a better job than i ever could ;-)

Again, sorry for the delay.
« Last Edit: February 25, 2017, 02:20:34 pm by molly »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus