#1 2014-08-29 10:11:21

lele9
Member
Registered: 2011-10-28
Posts: 170

TDynArray and RTTI?

Hi,
im playing with TDynArray (until now i use TCollection, but i would like use array of record in next project).
Can i iterate the field of a generic TDynArray and edit the value without know the type of my array of record?
Now i do this with TCollection to fill in a grid and to edit value without know the type of tCollectionItem.
Thanks

Offline

#2 2014-08-29 10:38:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArray and RTTI?

AFAIK to use RTTI you need to know its type information.

For any TObject, you have got the type information from its class (which is stored as pointer at offset 0 of the instance).
But for a dynamic array, you just have a pointer to the data (with its length stored before the items), no type information.

Offline

#3 2014-08-29 11:01:58

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: TDynArray and RTTI?

excuse me ab,
i see the code of RecordEquals in synCommons and seems iterate in the field of a record...with a parameter TypeInfo.
but i can pass to my generic procedure the typeinfo of the specific tdynarray.
what do you think?

Offline

#4 2014-08-29 11:56:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArray and RTTI?

The dynamic array has indeed the type information for its managed nested record.
You can use TDynArray code as start.

Offline

#5 2014-08-29 15:31:00

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: TDynArray and RTTI?

thanks ab, im in the way, when i finish post the code for sharing.
another question about TDynArray:
there is a method to shift an element?
thanks

Offline

#6 2014-08-29 15:45:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArray and RTTI?

What do you call "shift"?
AFAIR there are both "insert" and "delete" methods, but no direct way of moving one item from one place to another, if this is what you meant.

Offline

#7 2014-08-29 16:06:35

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: TDynArray and RTTI?

i would like to exchange position at two element, but without know the type of element...so i can't use an helper variable...
for example when i know the record type, to shift up an element i do:
var myHelperVar: TMyRecordType;

myHelperVar := TmyRecordType(myDynArray.ElemPtr(Pos)^);
myDynArray.Delete(Pos);
myDynArray.Insert(Pos-1, myHelperVar);

there is a way to do without know the record type!?

Offline

#8 2014-08-29 19:49:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArray and RTTI?

You need at least the record size.
But to have the record size, you need the record type.
So in all cases, you need the record type.
The TDynArray already knows the record type.

Just work at binary level.

Offline

Board footer

Powered by FluxBB