Question

I'm creating a component with many published properties for the IDE, and one of such properties is an enum set with 38 values...

type
  TBigEnum = (beOne, beTwo, beThree, beFour, beFive, beSix, beSeven, beEight,
    beNine, beTen, beEleven, beTwelve, beThirteen, beFourteen, beFifteen,
    beSixteen, beSeventeen, beEighteen, beNineteen, beTwenty, beTwentyOne,
    beTwentyTwo, beTwentyThree, beTwentyFour, beTwentyFive, beTwentySix,
    beTwentySeven, beTwentyEight, beTwentyNine, beThirty, beThirtyOne,
    beThirtyTwo, beThirtyThree, beThirtyFour, beThirtyFive, beThirtySix,
    beThirtySeven, beThirtyEight);
  TBigEnums = set of TBigEnum;

Now, I try to use this in a component as a published property...

type
  TMyComponent = class(TComponent)
  private
    FBigEnums: TBigEnums;
  published
    property BigEnums: TBigEnums read FBigEnums write FBigEnums;
  end;

But the compiler does not let me...

[DCC Error] MyUnit.pas(50): E2187 Size of published set 'BigEnums' is >4 bytes

I understand this limitation, but how can I get around this without splitting it into two different sets?

PS - Each of these values actually has a unique name and purpose, but for the sake of example I just used the number as their names.

Was it helpful?

Solution

I don't remember the exactly correct syntax but in principle:

1 If the "property" does not have to be easily editable in the property inspector then definining 38 different consts of type Long with their values set to 1 shl 0, 1 shl 1, 1 shl 2..

so that those consts can be combined like this PropOne or PropTwo or PropThree

2 if the property must be editable in the property inspector then the TMyPersistent class proposed in Jerry's answer seems ok to me

3 there might be a way built-into the language (or compiler directive) how to type-cast the set representation so that it uses 8 bytes for storage. Int32 and Int64 are both native data types well supported on new processors and both assembler, C++ and C# can deal with it. Some Pascal flavor (Free Pascal?) either has it implemented or it was in the road map

EDIT option 3 seems to be misleading. What can Free Pascal compiler do regarding enums is listed here http://www.freepascal.org/docs-html/prog/prog.html, especially in chapter $PACKENUM. As of today enums are always backed by 32bit ordinals. So the possibility to increase number of bits used for "enums" possible in assembler, C++, C# is not likely to be available in Delphi.

I'm not even sure if bitwise operators and, or, not, shl, shr used in other languages to implement enums and sets are available for 8-byte integers either in Delphi or in Free Pascal so the option 1 might also be misleading and the winner is option 2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top