Enum usage.

Hello,

My algorithm has a few "modes", currently defined by an integer 'id'. A lot of my code features "breaker functions", which accept generic input and then return a mode-specific result, e.g.:

1
2
3
4
5
6
int breakerSomething(int A) {
  switch(mode) {
    case 0: return A+5;
    case 1: return A-3;
  }
}

That kind of stuff. Now, the number assigned to each mode does have an impact, because the algorithm cycles through modes in a pre-determined order. I switch the order by changing the id numbers of each mode (mode 0 is always first). That also means I have to adjust my cases each time.

I'm pretty sure I could solve this by defining an enum and naming each mode, then adjust the order of the names in the enum. Sadly, I've never used them and I don't know how they work. Can someone provide me an example of how to use them?

Secondly, I once read the specific case id's can have an effect on performance. The example in the article was comparing cases (0, 1, 2, 3, ..) versus (1, 18, 105, 106, ..) or so, as in "random" numbers rather than the structured 0->3. I think enums use the first (sequential numbers), but would the order in which cases appear cause performance issues, e.g. having case 1 appear before case 0, or so?

Lastly, one of the breaker functions can be replaced with a lookup table, because it just returns a constant for each "mode". Is there a way to do this safely with the enum-types? (i.e. safe from changes in order of the enum-types)

Thanks in advance!

Example of an enum:
1
2
3
4
5
6
enum GameState {
    STATE_MENU,
    STATE_PLAYING,
    STATE_PAUSED,
    STATE_GAMEOVER 
};


Each element of an enum corresponds to an integer, and each element has the value of the previous element + 1. So, in this case, STATE_MENU has value 0, STATE_PLAYING 1, STATE_PAUSED 2 and so on.
You can also set the value of the element yourself:
1
2
3
4
5
6
enum GameState {
    STATE_MENU = 6,
    STATE_PLAYING,
    STATE_PAUSED = 4,
    STATE_GAMEOVER 
};


In this case, STATE_MENU corresponds to 6, and STATE_PLAYING to STATE_MENU + 1, so 7. STATE_PAUSED is logically 4, and so STATE_GAMEOVER is 5. It is possible that more elements of the enum have the same integer value. Have a look here for a detailed explanation: http://www.learncpp.com/cpp-tutorial/45-enumerated-types/
Thanks, working so far!

Additional question: is there any kind of issue that can happen when I use ints to access the enum?

For example, let's say I have these states:
enum States { SLOW, NORMAL, FAST};
but I loop through states simply using an int:
1
2
3
for (int STATE = 0; STATE < STATE_COUNT; ++STATE) {
        doSomething(STATE);    // Calls one of the breaker functions mentioned above.
 }

is there anything I should watch out for?
The breaker functions check for enum values, of course:
1
2
3
4
5
void doSomething(int STATE) {
    switch(CASE) {
    case States::SLOW: return doSomethingSlow();
// etc
}


I intend to swap the order of the types (enum values) around. My primary concern is no sneaky bugs enter the algorithm. I take it the order of the enum definition is guaranteed and that comparing an int to enum values won't ever mess up?

Last edited on
Topic archived. No new replies allowed.