Advertisement

How to stringize enums...

Started by October 17, 2000 09:12 AM
2 comments, last by Danack 24 years, 2 months ago
Does anyone know how you can automatically generate a table strings that are taken from a list of enums, for debugging purposes? eg I have some enums:
    
enum	TransparencyType
{
	SOLID_TEXTURE,
	SEMI_TRANSPARENT,
	REFLECTION_SURFACE,
};
[/source]

I could build up the table

[source]
char    *TransparencyTypeString[] =
{
	"SOLID_TEXTURE",
	"SEMI_TRANSPARENT",
	"REFLECTION_SURFACE",
}

    
by hand, but I''d much rather find some macro that could build the string table automatically, so whenever I add an enum the string table get updated automatically. There should be some way of doing this, but I haven''t been able to find it yet.... cheers dan Game production: Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.


This code is untested but should do what you want.
Advertisement
Oops, sorry. I meant to back out of that post. After much thought, I don''t think there is way to avoid typing twice if you really want to do it as a strict enum. If it didn''t have to be an enum, you would have some flexibility.
An enum is really not much more than a quick way of doing multiple constant declarations (I would say sequential but you can explicitly declare values, so that would be inaccurate, though they are sequential by default). As such, the data type is retained in memory with the value, but I don''t believe that the name is retained there as well (so, if you were enumerating days with "enum Days{ sunday, monday,...} then the type "Days" would be visible to your debugger, but I don''t believe that the literal names are retrievable other than by using an array of strings which you have already apparently thought of.)
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

This topic is closed to new replies.

Advertisement