Re: How to get enum identifier name as text during runtime?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



See below...
On Mon, 28 Apr 2008 08:16:15 -0700 (PDT), guuwwe@xxxxxxxxxxx wrote:

On Apr 27, 3:14 pm, "Doug Harrison [MVP]" <d...@xxxxxxxx> wrote:
On Sun, 27 Apr 2008 12:06:18 -0700 (PDT), guu...@xxxxxxxxxxx wrote:
Is there any preprocessor directive to get the text string of the enum
identifier name
at runtime?
For example, assume:
enum MyList {Mem1, Mem2};

during runtime my code wants to reference Mem1 and print its source
code
text name "Mem1" by referencing Mem1, and not its numeric value 0.

You will have to do that yourself. One way is to maintain an array of
strings in parallel to the enum containing the names as text.

--
Doug Harrison
Visual C++ MVP

You need an array in either case. The enum identifiers have to be
statically bound to their text names by the preprocessor. It is a
simple
thing to have a directive that takes any identifier and assignes its
text name to another identifier or an entry in a constant array,
and a constant array is the only way if you want to keep many
identifiers.
****
Not sure that the preprocessor has to enter the picture; the binding is actually done by
the compiler. I posed a solution in which the preprocessor makes it easier to write, but
you can also (if the enums have specific assignments) write a table of the form

struct {
MyEnumType id, LPCTSTR str} MyEnumNames[] = {
{ Mem1, _T("Mem1") },
{ Mem2, _T("Mem2") },
{ 0, NULL }
};

and write

CString MapEnumToName(MyEnumType id)
{
for(int i = 0; MyEnumNames[i].str != NULL; i++)
{
if(MyEnumNames[i].id == id)
return MyEnumNames[i].str;
}
CString s;
s.Format(_T("%d"), id);
return s;
}

There are many obvious generalizations of this technique. It is most useful in debugging.

Another solution is

class EnumMapper {
public:
CString MapEnumToName(UINT id)
{
EnumMap map = GetEnumMap();
for(int i = 0; map[i].str != NULL; i++)
if(map[i].id == id)
return map[i].str;
CString s;
s.Format(_T("%d"), id);
return s;
}
virtual EnumMap GetEnumMap() PURE;
}
class MyEnums : public EnumMapper {
public:
typedef enum {Mem1, Mem2} MyEnumType;
DECLARE_ENUM_MAP()
virtual GetEnumMap() { return map; }
CString ToString(MyEnumType type) {
return MapEnumToName((UINT)type);
}
};

BEGIN_ENUM_MAP(MyEnums)
ENUMTYPE(Mem1)
ENUMTYPE(Mem2)
END_ENUM_MAP

where you have done something like

#define DECLARE_ENUM_MAP() static EnumMap map;

#define BEGIN_ENUM_MAP(classname) \
static EnumMap classname::map[] = {

#define END_ENUM_MAP() \
{ 0, NULL } \
};

#define ENUMTYPE(x) { (UINT)(x), _T(#x) },

typedef struct { UINT id; LPCSTR str; } EnumMap;

This may not be complete or necessarily syntactically correct, but it is probably a
reasonable sketch for a starting point.

joe
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: How to get enum identifier name as text during runtime?
    ... text name "Mem1" by referencing Mem1, and not its numeric value 0. ... strings in parallel to the enum containing the names as text. ... thing to have a directive that takes any identifier and assignes its ... text name to another identifier or an entry in a constant array, ...
    (microsoft.public.vc.mfc)
  • Re: Jython inherit from Java class
    ... Compiling .java to .class... ... 'assert' is a keyword, and may not be used as an identifier ... release 1.5, 'enum' is a keyword, and may not be used as an identifier ...
    (comp.lang.python)
  • Re: bison c-parse.y:1115.19-20: $$ for the midrule at $4 of `structsp has no declared type
    ... STRUCT identifier '{' ... | ENUM identifier '{' ... UNION identifier '' ...
    (comp.compilers)
  • Re: error: as of release 1.5, enum is a keyword
    ... Hall's book Core Servlets and JSPs. ... and may not be used as an identifier ... Enumeration enum = path.elements; ...
    (comp.lang.java.programmer)
  • Re: Why [Enum]?
    ... using a word as an identifier that happens to be the same name as a ... reserved word, as in: ... why must I access the shared GetNamesmethod of an Enum with ... Otherwise it would be identified as the keyword that is there to declare a new Enum type. ...
    (microsoft.public.dotnet.languages.vb)