Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over non-incremental Enum

Before you ask, I've looked and looked for this on SO, and cannot find a solid answer.

I need to be able to dynamically iterate over an enum that has non-incremental values, as an example:

typedef enum {     CAPI_SUBTYPE_NULL = 0,               /* Null subtype. */     CAPI_SUBTYPE_DIAG_DFD = 1,           /* Data Flow diag. */     CAPI_SUBTYPE_DIAG_ERD = 2,           /* Entity-Relationship diag. */     CAPI_SUBTYPE_DIAG_STD = 3,           /* State Transition diag. */     CAPI_SUBTYPE_DIAG_STC = 4,           /* Structure Chart diag. */     CAPI_SUBTYPE_DIAG_DSD = 5,           /* Data Structure diag. */     CAPI_SUBTYPE_SPEC_PROCESS = 6,       /* Process spec. */     CAPI_SUBTYPE_SPEC_MODULE = 7,        /* Module spec. */     CAPI_SUBTYPE_SPEC_TERMINATOR = 8,    /* Terminator spec. */      CAPI_SUBTYPE_DD_ALL = 13,            /* DD Entries (All). */     CAPI_SUBTYPE_DD_COUPLE = 14,         /* DD Entries (Couples). */     CAPI_SUBTYPE_DD_DATA_AREA = 15,      /* DD Entries (Data Areas). */     CAPI_SUBTYPE_DD_DATA_OBJECT = 16,    /* DD Entries (Data Objects). */     CAPI_SUBTYPE_DD_FLOW = 17,           /* DD Entries (Flows). */     CAPI_SUBTYPE_DD_RELATIONSHIP = 18,   /* DD Entries (Relationships). */     CAPI_SUBTYPE_DD_STORE = 19,          /* DD Entries (Stores). */      CAPI_SUBTYPE_DIAG_PAD = 35,          /* Physical architecture diagram. */     CAPI_SUBTYPE_DIAG_BD  = 36,          /* Behaviour diagram. */     CAPI_SUBTYPE_DIAG_UCD = 37,          /* UML Use case diagram. */     CAPI_SUBTYPE_DIAG_PD  = 38,          /* UML Package diagram. */     CAPI_SUBTYPE_DIAG_COD = 39,          /* UML Collaboration diagram. */     CAPI_SUBTYPE_DIAG_SQD = 40,          /* UML Sequence diagram. */     CAPI_SUBTYPE_DIAG_CD  = 41,          /* UML Class diagram. */     CAPI_SUBTYPE_DIAG_SCD = 42,          /* UML State chart. */     CAPI_SUBTYPE_DIAG_ACD = 43,          /* UML Activity chart. */     CAPI_SUBTYPE_DIAG_CPD = 44,          /* UML Component diagram. */     CAPI_SUBTYPE_DIAG_DPD = 45,          /* UML Deployment diagram. */     CAPI_SUBTYPE_DIAG_PFD = 47,          /* Process flow diagram. */     CAPI_SUBTYPE_DIAG_HIER = 48,         /* Hierarchy diagram. */     CAPI_SUBTYPE_DIAG_IDEF0 = 49,        /* IDEF0 diagram. */     CAPI_SUBTYPE_DIAG_AID = 50,          /* AID diagram. */     CAPI_SUBTYPE_DIAG_SAD = 51,          /* SAD diagram. */     CAPI_SUBTYPE_DIAG_ASG = 59           /* ASG diagram. */ } CAPI_SUBTYPE_E ; 

The reason I'd like to be able to do this is because the enum is given in an API (which I cannot change, obviously) and would prefer to be able to, regardless of the API version, be able to iterate over these values.

Any direction is appreciated.

like image 637
Christopher Bales Avatar asked May 16 '13 20:05

Christopher Bales


People also ask

Can you iterate over an enum?

Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.

Can I iterate through enum Java?

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc. You can iterate the contents of an enumeration using for loop, using forEach loop and, using java.

Can you iterate over an enum C?

you can iterate the elements like: for(int i=Bar; i<=Last; i++) { ... } Note that this exposes the really-just-an-int nature of a C enum. In particular, you can see that a C enum doesn't really provide type safety, as you can use an int in place of an enum value and vice versa.

What is enum CPP?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.


1 Answers

With C++, the only way to iterate through enums is store them in an array and iterate through the same. The main challenge is how to track the same order in the enum declaration and the array declaration?
You can automate the way you order them in the enum as well as array. I feel that this is a decent way:

// CAPI_SUBTYPE_E_list.h // This header file contains all the enum in the order // Whatever order is set will be followed everywhere NAME_VALUE(CAPI_SUBTYPE_NULL, 0),         /* Null subtype. */ NAME_VALUE(CAPI_SUBTYPE_DIAG_DFD, 1),     /* Data Flow diag. */ NAME_VALUE(CAPI_SUBTYPE_DIAG_ERD, 2),     /* Entity-Relationship diag. */ ... NAME_VALUE(CAPI_SUBTYPE_DD_ALL, 13),      /* DD Entries (All). */ NAME_VALUE(CAPI_SUBTYPE_DD_COUPLE, 14),   /* DD Entries (Couples). */ ... NAME_VALUE(CAPI_SUBTYPE_DIAG_ASG, 59)     /* ASG diagram. */ 

Now you #include this file in your enum declaration and array declaration both places with macro redefinition:

// Enum.h typedef enum { #define NAME_VALUE(NAME,VALUE) NAME = VALUE #include"CAPI_SUBTYPE_E_list.h" #undef NAME_VALUE }CAPI_SUBTYPE_E; 

And put the same file for array with other macro definition:

// array file // Either this array can be declared `static` or inside unnamed `namespace` to make  // ... it visible through a header file; Or it should be declared `extern` and keep ... // ...  the record of its size; declare a getter method for both array and the size unsigned int CAPI_SUBTYPE_E_Array [] = { #define NAME_VALUE(NAME,VALUE) NAME #include"CAPI_SUBTYPE_E_list.h" #undef NAME_VALUE }; 

Now iterate in C++03 as:

for(unsigned int i = 0, size = sizeof(CAPI_SUBTYPE_E_Array)/sizeof(CAPI_SUBTYPE_E_Array[0]);     i < size; ++i) 

or yet simple in C++11:

for(auto i : CAPI_SUBTYPE_E_Array) 
like image 87
iammilind Avatar answered Oct 06 '22 00:10

iammilind