Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-11

Enum RTTI

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Wimpie Nortje

Hi

This follows from stuff I read in "[fpc-devel] Re: Testing for..in feature". I read, and know from trying, that Low(enum) and High(enum) does not work.

Is there any way to achieve the following easily:
1) for enum_first to enum_last do....
2) NumberOfItems(enum)

I very often have enums for which I want string descriptions. I couldn't find a way to use RTTI to obtain the string descriptions so I resorted to having an array of strings. The problem is that the enums change frequently in item count and item order, which means I spend a lot of time fixing the string<->enum functions .

Jonas Maebe
to Wimpie Nortje
Wimpie Nortje wrote
Is there any way to achieve the following easily: 1) for enum_first to enum_last do....

No, because succ/pred are not defined for sparse enums.

2) NumberOfItems(enum)

No. Even typeinfo() doesn't work for sparse enums.

I very often have enums for which I want string descriptions.

As of FPC 2.4.0, you will be able to use

var
   s: string; { any kind of string will work
begin
   writestr(s,enumvar);
end;

And afterwards 's' will contain the string representation of enumvar. In general, writestr is identical to the regular "write" procedure, except that it stores its output in a string (which you have to pass as first parameter). A similar "readstr" procedure will also be available.

Jonas

Wolfram Kläger
Show quoted text 5 lines
-----Ursprüngliche Nachricht----- Von: "Wimpie Nortje" Gesendet: 05.11.09 09:33:36 An: FPC-Pascal users discussions Betreff: [fpc-pascal] Enum RTTI
Show quoted text 5 lines
I very often have enums for which I want string descriptions. I couldn't find a way to use RTTI to obtain the string descriptions so I resorted to having an array of strings. The problem is that the enums change frequently in item count and item order, which means I spend a lot of time fixing the string<->enum functions .

Wimpie,

maybe you want to try:

uses
  typinfo;
// implementation
function Str(const AEnum : TEnum) : String; overload;
begin
  Result := GetEnumName(TypeInfo(TEnum), ord(AEnum));
end;

I usually declare one such overload per enum type. If you are using sysutils.Str, you have to avoid conflicts by chosing a different function name, e.g. EnumStr.

Wolfram

Wimpie Nortje
to Jonas Maebe: No, because succ/pred are not defined for sparse enums. No. Even typeinfo() doesn't work…

Thanks.
I don't think I use sparse enums. Mine are declared like: type aEnum = (Val1, Val2);

So I think the Succ/Pred should work. I have never used typeinfo before...will have a look at it.

Show quoted text 9 lines
> 1) for enum_first to enum_last do.... Is there any way to achieve the following easily: No, because succ/pred are not defined for sparse enums. > 2) NumberOfItems(enum) No. Even typeinfo() doesn't work for sparse enums.
Wimpie Nortje
to Wolfram Kläger: maybe you want to try: uses typinfo; // implementation function Str(const AEnum : TEnum)…

Thanks, I will try this

Wolfram Kläger wrote 18 lines
Wimpie, maybe you want to try: uses typinfo; // implementation function Str(const AEnum : TEnum) : String; overload; begin Result := GetEnumName(TypeInfo(TEnum), ord(AEnum)); end; I usually declare one such overload per enum type. If you are using sysutils.Str, you have to avoid conflicts by chosing a different function name, e.g. EnumStr.
Jonas Maebe
to Wimpie Nortje: Thanks. I don't think I use sparse enums. Mine are declared like: type aEnum = (Val1,…
Wimpie Nortje wrote 14 lines
>> 1) for enum_first to enum_last do.... > Is there any way to achieve the following easily: > > No, because succ/pred are not defined for sparse enums. > >> 2) NumberOfItems(enum) > > No. Even typeinfo() doesn't work for sparse enums. > Thanks. I don't think I use sparse enums. Mine are declared like: type aEnum = (Val1, Val2); So I think the Succ/Pred should work.

They do, and so do low/high:

$ cat tt2.pp
type aEnum = (Val1, Val2);
var
   e: aEnum;
begin
   for e := low(aEnum) to high(aEnum) do
     writeln(ord(e));
end.
$ /usr/local/lib/fpc/2.2.4/ppcppc tt2
Free Pascal Compiler version 2.2.4 [2009/04/05] for powerpc
Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Darwin for PowerPC
Compiling tt2.pp
Assembling program
Linking tt2
8 lines compiled, 0.4 sec
$ ./tt
ea

I thought you were talking about sparse enums because you said that low/high didn't work.

Jonas

Jonas Maebe
to Wolfram Kläger: maybe you want to try: uses typinfo; // implementation function Str(const AEnum : TEnum)…
Wolfram Kläger wrote 15 lines
maybe you want to try: uses typinfo; // implementation function Str(const AEnum : TEnum) : String; overload; begin Result := GetEnumName(TypeInfo(TEnum), ord(AEnum)); end; I usually declare one such overload per enum type. If you are using sysutils.Str, you have to avoid conflicts by chosing a different function name, e.g. EnumStr.

Actually, as of FPC 2.4.0 str() is also supported for enum types (so in this case you don't need the writestr I mentioned earlier). It crashes if you pass an invalid enum value though, and there are next to no tests for this functionality so it may also contain other bugs.

Jonas