Re: How to get enum identifier name as text during runtime?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 28 Apr 2008 13:01:58 -0400
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
.
- References:
- How to get enum identifier name as text during runtime?
- From: guuwwe
- Re: How to get enum identifier name as text during runtime?
- From: Doug Harrison [MVP]
- Re: How to get enum identifier name as text during runtime?
- From: guuwwe
- How to get enum identifier name as text during runtime?
- Prev by Date: Re: Message 1457 decimal
- Next by Date: Re: undefing an int?
- Previous by thread: Re: How to get enum identifier name as text during runtime?
- Next by thread: Re: How to get enum identifier name as text during runtime?
- Index(es):
Relevant Pages
|