|
From: guuwwe on 27 Apr 2008 15:06 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.
From: Doug Harrison [MVP] on 27 Apr 2008 15:14 On Sun, 27 Apr 2008 12:06:18 -0700 (PDT), guuwwe(a)hotmail.com 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
From: Joseph M. Newcomer on 27 Apr 2008 21:31 If you are doing this a lot, you can come up with some macros to do it For example #define MYLIST_VALUES() VALUE_(Mem1), VALUE_(Mem2) #define VALUE_(x) x enum MyList {MYLIST_VALUES} #undef VALUE_ #define VALUE_(x) _T(#x) LPCTSTR MyListNames[] = { MYLIST_VALUES }; #undef VALUE_ which works if you use the default assignments but not if you want the enums to have specific values. joe On Sun, 27 Apr 2008 12:06:18 -0700 (PDT), guuwwe(a)hotmail.com 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. Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: guuwwe on 28 Apr 2008 11:16 On Apr 27, 3:14 pm, "Doug Harrison [MVP]" <d...(a)mvps.org> wrote: > On Sun, 27 Apr 2008 12:06:18 -0700 (PDT), guu...(a)hotmail.com 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.
From: Joseph M. Newcomer on 28 Apr 2008 13:01 See below... On Mon, 28 Apr 2008 08:16:15 -0700 (PDT), guuwwe(a)hotmail.com wrote: >On Apr 27, 3:14�pm, "Doug Harrison [MVP]" <d...(a)mvps.org> wrote: >> On Sun, 27 Apr 2008 12:06:18 -0700 (PDT), guu...(a)hotmail.com 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(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
|
Pages: 1 Prev: Large text files and searching text Next: Exporting functions from my application |