General method for displaying enumeration value names

Is there a way to display the enum value name? let's say we have:

enum fuits{ APPLE, MANGO, ORANGE, }; main(){ enum fruits xFruit = MANGO; ... printf("%s",_PRINT_ENUM_STRING(xFruit)); ... } 

using preprocessor

 #define _PRINT_ENUM_STRING(x) #x 

will not work, since we need to get the value of the variable "x" and then convert it to a string. Is this possible in c / C ++?

+7
c ++ c
source share
4 answers

You can use a preprocessor for this, I believe this method is called X-Macros :

 /* fruits.def */ X(APPLE) X(MANGO) X(ORANGE) /* file.c */ enum fruits { #define X(a) a, #include "fruits.def" #undef X }; const char *fruit_name[] = { #define X(a) #a, #include "fruits.def" #undef X }; 

Note that the last entry contains the trailing comma, which is allowed in C99 (but not in C89). If this is a problem, you can add valuable values. It is also possible to make a macro more complex by providing a few arguments for user names or enumeration values, etc.:

 X(APPLE, Apple, 2) #define X(a,b,c) a = c, /* in enum */ #define X(a,b,c) [c] = #b, /* in name array */ 

Limitations: you cannot have negative constants, and your array is sizeof (char *) * largest_constant . But you can get around both with an optional lookup table:

 int map[] = { #define X(a,b,c) c, #include "fruits.def" #undef X }; 

Strike>

This does not work, of course. What makes the job of generating an additional set of enum constants as keys for names:

 enum fruits { #define X(a,b,c) a ## _KEY, #include "fruits.def" #undef X #define X(a,b,c) a = c, #include "fruits.def" #undef X }; 

Now you can find the name X(PINEAPPLE, Pineapple, -40) using fruit_name[PINEAPPLE_KEY] .

People noted that they did not like the optional include file. You do not need this additional file, you also use #define . This may be more suitable for small lists:

 #define FRUIT_LIST X(APPLE) X(ORANGE) 

And replace #include "fruits.def with FRUIT_LIST in the previous examples.

+7
source share

In this case, you can use matching.

 char *a[10] = { "APPLE","MANGO","ORANGE"}; printf("%s",a[xFruit]); 

Yes, the preprocessor will not work unless you provide the exact enum value.

Also check out this question for more information.

+1
source share

I have successfully used preprocessing to get this type of macro:

 DEFINE_ENUM(Fruits, (Apple)(Mango)(Orange)); 

This is a little more than just printing names, but if necessary, it can be simplified to 2 switches.

It is based on the capabilities of Boost.Preprocessor (especially BOOST_PP_SEQ_FOREACH ) that a preprocessor should have for programming, and I find it much more elegant than X and its file re-entry system.

0
source share

  public enum LDGoalProgressUpdateState
     {

  [Description("Yet To Start")] YetToStart = 1, [Description("In Progress")] InProgress = 2, [Description("Completed")] Completed = 3 } var values = (ENUMList[])Enum.GetValues(typeof(ENUMList)); var query = from name in values select new EnumData//EnumData is a Modal or Entity { ID = (short)name, Name = GetEnumDescription(name)//Description of Particular Enum Name }; return query.ToList(); 

HelperMethods region

public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return value.ToString(); } #endregion
-one
source share

All Articles