[fpc-pascal] flexible record design

Michael Van Canneyt michael at freepascal.org
Fri May 28 16:03:28 CEST 2010



On Fri, 28 May 2010, spir ☣ wrote:

> On Fri, 28 May 2010 12:25:59 +0200
> Felipe Monteiro de Carvalho <felipemonteiro.carvalho at gmail.com> wrote:
>
>>> I would like to know the underlying structure of TString (linked list, "flexible-ised" dynamic array, what else?).
>>
>> TStrings provides no storage.
>>
>> I think that TStringList should be what you are looking for. See it's
>> code instead.
>
> I know about TStringList. But it does not provide associated values & objects. The reason why I ask about what these things are (supposed to be) in the interface of TStrings.
> I have a better idea about values, now. Seem to be strings as well (the name "value" is a bit misleading imo), so not what I need at all.
> But I'm still digging to try and find out what object association is *intended* to be, its purpose and possible implementation. In other word, what's the actual aim of this virtual interface?
>
> The doc reads:
> "Indexed access to the objects associated with the strings in the list.
> [...]
> Objects provides indexed access to the objects associated to the strings in the list. Index is a zero-based index and must be in the range of 0 to Count-1.
> Setting the objects property will not free the previously associated object, if there was one. The caller is repsonsible for freeing the object that was previously associated to the string.
> TStrings does not implement any storage for objects. Reading the Objects property will always return Nil, Setting the property will have no effect. It is the responsability of the descendent classes to provide storage for the associated objects."
>
> This is not stupid enough for me. I don't know what to do with this.

TStrings provides an abstract interface. It allows you to associate an
object with each string in the list.

This means you can do a

   L.Strings[i]:=Key;
   L.Objects[i]:=MyObject;

Or, in 1 statement:

   L.AddObject(Key,MyObject);

And you can retrieve it with
   I:=L.IndexOf(MyKey);
   If (I=-1) then
    // no mykey in list
   else
    MyObject:=L.Objects[i];


However, it does not actually implement this; it just provides a common
API for a string list with associated objects.

TStringList actually implements - in a simplistic way - the functionality
to store the strings, and the associated objects.

The 'Values' property is for Name=Value pairs, where both are strings.
(used commonly in .ini files)

That means that if you do

L.Add(mykey+'='+myvalue);

or, alternatively

L.Values[mykey]=myvalue;

you can retrieve myvalue with

MyValue:=L.Values[mykey];

The objects are not used in this case, the list contains items of the form 
Name=Value.
This is not very performant, but works easily for small amounts of values.

For large amounts of data, you should use hash lists, as implemented in the 
contnrs unit.

Michael.


More information about the fpc-pascal mailing list