Recent

Author Topic: TObject with sets  (Read 5143 times)

tomek

  • Jr. Member
  • **
  • Posts: 85
TObject with sets
« on: June 27, 2011, 12:14:25 pm »
In my example Objects is array of Objects (from StringGrid)
I can do this:
Code: [Select]
Objects[0,0] := TObject(5);
...
if Objects[0,0] = TObject(5) then ...
but, how to compare TObject as set of ?
I can set Objects as "set":
Code: [Select]
Objects[0,0] := TObject([5,4,1]);
but I don't know how to compare:
Code: [Select]
if [2] in Objects[0,0] then ... ???
is there any way to do this ?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TObject with sets
« Reply #1 on: June 27, 2011, 01:38:03 pm »
Your code makes no sense whatsoever to me...

TObject(5) ???

TObject([5,4,1])  ????

What are those? How can you possibly typecast a constant value into TObject? What would you gain from it?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12715
  • FPC developer.
Re: TObject with sets
« Reply #2 on: June 27, 2011, 01:41:22 pm »
No. There is no support for sets that are not enums or subranges of integer.

Mostly because they are very expensive to compute and maintain, mitigating the use of the set type.

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: TObject with sets
« Reply #3 on: June 27, 2011, 03:49:13 pm »
Ok, I found some workaround for this (with pointers help), that is ok for me.
Code: [Select]
type
  TSet = set of byte;
  PSet = ^TSet;
var
 pvarSet: PSet;

 pvarSet  := AllocMem(SizeOf(TSet));
 pvarSet^ := [5,4,2];

 Objects[0,0]:=TObject(pvarSet);

 ...

 if (5 in PSet(Objects[0,0])^ ) then ...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TObject with sets
« Reply #4 on: June 27, 2011, 04:56:19 pm »
Quote
Your code makes no sense whatsoever to me...

TObject(5) ???

TObject([5,4,1])  ????

What are those? How can you possibly typecast a constant value into TObject? What would you gain from it?
I think the OP wants to use Objects property of TStringGrid, since the object expects TObject descendant but he only wants to store integers, then the integers got casted to TObject. I'm not sure whether it's safe and what code the compiler will generate for the cast.

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: TObject with sets
« Reply #5 on: June 28, 2011, 11:20:23 am »
... I'm not sure whether it's safe and what code the compiler will generate for the cast.

You're right. This is not the right solution.
I've big problems with properly freeing memory and get AV with -Cr (range checks).
I will use another way.

 

TinyPortal © 2005-2018