Mateusz Guzdek
Mateusz Guzdek

Reputation: 48

Execute all cases in switch statement - specific issue

How to execute all cases in switch statement without duplicating existing in big "all-in-one" case ?

For e.g.

switch(obj.nObjType)
{
case eElephant:
...
break;
case eFoo:
...
break;
case eSeptember:
...
break;
default:
return;
}

I want force executing code for all 3 cases (eElephant, eFoo, eSeptember) like there is no break;, for example in case of nObjType = eAllTypes .

Upvotes: 1

Views: 4081

Answers (4)

Baldrickk
Baldrickk

Reputation: 4409

This answer is how it should be done. However:

I can see, it's hard to find as right solution without generating lots of code lines. Thanks @TobiMcNamobi, but in my switch there is about 200 of cases, so creating a function for each case is not a good idea. I think, the best can be (unfortunately) duplicating all cases in a big one. Because, I suppose, if else is much less efficient then switch statement ?

With this many cases (what on earth are you working on?) even extracting methods leaves you with 200 extra function calls in the body of your switch statement. This could get harder to maintain quickly.

In this case, I would do the following:

switch(enum_variable)
{
  case e_all:

  case e_1:
    e_1_function();
    if(enum_variable != e_all){break;}   //**

  case e_2:
    e_2_function();
    if(enum_variable != e_all){break;}   //**

  //...

  default:
    break;
}

The lines marked //** will break when not doing all cases. It is quick to implement (can be done with a search and replace of break; which speeds things up for you) and does what you want.
With that said, it's still a pretty terrible way of doing it, but exceptions do sometimes have to be made.

I do however recommend that you do not turn to this as a style to use in the future. You may find that you can do better by re-thinking the workflow of your project, as this will probably allow you to simplify and improve. Of course, we don't know what you are working on, so I can't offer more advice.

Upvotes: 2

Tomas Aschan
Tomas Aschan

Reputation: 60634

If you put your switch statement in a function, and make your obj have a list of flags rather than a flag, you can call the function with the switch statement for each flag in the list. Pseudo:

void dostuff(flag)
    switch(flag) {
        // ...
    }
}

for (auto flag in obj.flags) {
    dostuff(flag)
}

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409266

Then use if statements:

if (obj.nObjType == eElephant || obj.nObjType == eAllTypes)
{
    // Elephant code
}

if (obj.nObjType == eFoo || obj.nObjType == eAllTypes)
{
    // Foo code
}

// etc.

Upvotes: 3

Amadan
Amadan

Reputation: 198418

You can't just ignore a break. Rather, restructure your code.

switch(obj.nObjType) {
  case eElephant:
    pokeElephant();
    break;
  case eFoo:
    barTheFoo();
    break;
  case eSeptember:
    rememberSeptember();
    break;
  case eAllTypes:
    pokeElephant();
    barTheFoo();
    rememberSeptember();
    break;
  default:
    return;
}

Upvotes: 6

Related Questions