Iterating over non-incremental Enum

前端 未结 15 1729
长发绾君心
长发绾君心 2021-01-31 15:42

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-incre

15条回答
  •  独厮守ぢ
    2021-01-31 16:02

    Based on the articles given at the begin of the question, I derived a solution that is based in the assumption that you know the invalids ranges.

    I really wanna knows if this is a good solution.

    First, end you enum with something like that: CAPI_END = 60. It will helps to interates. So my code is:

    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_END = 60                        /* just to mark the end of your enum */
    } CAPI_SUBTYPE_E ;
    
    CAPI_SUBTYPE_E& operator++(CAPI_SUBTYPE_E& capi)
    {
      const int ranges = 2;  // you have 2 invalid ranges in your example
      int invalid[ranges][2] = {{8, 12}, {19, 34}};  // {min, max} (inclusive, exclusive)
    
      CAPI_SUBTYPE_E next = CAPI_SUBTYPE_NULL;
    
      for (int i = 0; i < ranges; i++)
        if ( capi >= invalid[i][0] && capi < invalid[i][1] ) {
          next = static_cast(invalid[i][1] + 1);
          break;
        } else {
          next = static_cast(capi + 1);
        }
    
      //  if ( next > CAPI_END )
        // throw an exception
    
      return capi = next;
    }
    
    int main()
    {
      for(CAPI_SUBTYPE_E i = CAPI_SUBTYPE_NULL; i < CAPI_END; ++i)
        cout << i << endl;
    
      cout << endl;
    }
    

    I'm providing only a pre increment operator. A post increment operator is let to be implemanted later.

提交回复
热议问题