Recent

Author Topic: Embedding a boolean attribute in the items of a StringList.  (Read 1034 times)

simone

  • Sr. Member
  • ****
  • Posts: 356
Embedding a boolean attribute in the items of a StringList.
« on: January 16, 2020, 02:01:24 pm »
I need to embed a boolean 'attribute' in the items of a TStringList in an existing code. This can be done in different way, for example by using a generic container or by derivation of a subclass from a TStringList. However I have devised a very simple method: to use the 'objects' property, that is not used for other purposes, as showed in the following code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   Classes;
  4. var
  5.   Lst : TStringList;
  6.   ind : integer;
  7.  
  8. begin
  9.   Lst:=TStringList.Create;
  10.   Lst.Add('aaa');
  11.   Lst.Add('bbb');
  12.  
  13.   Lst.Objects[0]:=TObject(False);
  14.   Lst.Objects[1]:=TObject(True);
  15.  
  16.   for ind:=0 to Lst.Count-1 do
  17.     writeln(Lst[ind],'-',Assigned(Lst.Objects[ind]));
  18.  
  19.   Lst.Free;
  20.   readln;
  21. end.

The trick of casting a TObject in a boolean seems to work, resulting in a nil pointer wiht a 'false' value and in an assigned pointer with 'true' value. Do you think this approach is reliable? Thanks in advance.
Microsoft Windows 10 64 bit - Lazarus 2.0.10

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8732
  • FPC developer.
Re: Embedding a boolean attribute in the items of a StringList.
« Reply #1 on: January 16, 2020, 02:40:22 pm »
Yes, as long as you never use it in a tstringlist with ownsobjects:=true

simone

  • Sr. Member
  • ****
  • Posts: 356
Re: Embedding a boolean attribute in the items of a StringList.
« Reply #2 on: January 16, 2020, 03:34:51 pm »
Certainly. With ownsobjects:=true, a SIGSEGV error at runtime would occur.
Microsoft Windows 10 64 bit - Lazarus 2.0.10

Bart

  • Hero Member
  • *****
  • Posts: 3914
    • Bart en Mariska's Webstek
Re: Embedding a boolean attribute in the items of a StringList.
« Reply #3 on: January 16, 2020, 06:19:45 pm »
And Assigned(Objects[Index]) will return true if you set it to TObject(True).

Bart
« Last Edit: January 16, 2020, 06:21:24 pm by Bart »

simone

  • Sr. Member
  • ****
  • Posts: 356
Re: Embedding a boolean attribute in the items of a StringList.
« Reply #4 on: January 16, 2020, 08:12:56 pm »
I confirm. This was just what I wanted to check with the example code I published. I admit that casting a base type value (like a boolean) to an object is a bit unusual, but in my specific context it is convenient.
« Last Edit: January 16, 2020, 08:17:25 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 2.0.10

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 815
    • Lebeau Software
Re: Embedding a boolean attribute in the items of a StringList.
« Reply #5 on: January 17, 2020, 07:30:38 pm »
I have devised a very simple method: to use the 'objects' property, that is not used for other purposes

That is hardly a new trick. People have been using this trick for decades.  Also note that TStringList has an AddObject() method:

Code: Pascal  [Select][+][-]
  1. Lst.AddObject('aaa', TObject(False));
  2. Lst.AddObject('bbb', TObject(True));

Though, a TFPGList<record with String and Boolean> or a TFPGMap<String, Boolean> might make more sense to use, unless you really need the TStrings interface.

Do you think this approach is reliable?

Yes, it is.  At least in FreePascal, and in Delphi on desktop platforms, where ARC (automatic reference counting) is not used on TObject.  In Delphi on mobile platforms (iOs and Android), this approach would NOT work (at least until Delphi 10.4 comes out, as ARC on TObject will be removed in that version).  FreePascal does not support ARC on TObject.  But you can run into a similar problem if you set TStringList.OwnsObjects=True, like simone mentioned.  Just be aware of how your fake TObject pointers are being used, don't use them anywhere that real TObject pointers are expected.
« Last Edit: January 17, 2020, 07:34:25 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

simone

  • Sr. Member
  • ****
  • Posts: 356
Re: Embedding a boolean attribute in the items of a StringList.
« Reply #6 on: January 18, 2020, 12:00:48 am »
Thanks Remy for the interesting additional information about use of this trick in the ARC context.

However, I did not claim to have invented something original. In my first post I also stated that a generic container can be used.
« Last Edit: January 18, 2020, 01:58:14 am by simone »
Microsoft Windows 10 64 bit - Lazarus 2.0.10

 

TinyPortal © 2005-2018