Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove redundancies

Status
Not open for further replies.

fmaurer

Technical User
May 15, 2007
11
0
0
PL
Hi, I have the following problem
Code:
#define FIRST_OPTION 1
#define SECOND_OPTION 2
#define THIRD_OPTION 3
#define FOURTH_OPTION 4
…
Then I have some STL input string, let’s say inpStr and do sth like that
Code:
if (inpStr == "FIRST_OPTION")
   importantVariable = FIRST_OPTION;
else if (inpStr == "SECOND_OPTION")
   importantVariable = SECOND_OPTION;
...
else
   importantVariable = UNKNOWN_OPTION;
After such checking code becomes more and more redundant, however, I cannot find a way to do this smarter. I’m wondering if we can apply some casting to do this more less that simple (pseudo code)
Code:
checkIfDefineExists(inpStr)
if YES:
   importantVariable = (getIntegerValueFromDefinedHavingName)inpStr;
if NO:
   importantVariable = -1
Any help will be appreciated.
 
An alternative would be to put in in a map.

Code:
#include <map>

typedef std::map<std::string, int> MyMap;
// ...
{
  // Setup Map
  MyMap map;
  map["1ST_OPTION"] = 1;
  map["2ND_OPTION"] = 2;
  map["3RD_OPTION"] = 3;
  // etc.

  // ...

  // Query for existance/value
  MyMap::iterator it=map.find(inpStr);
  if (it ! map.end())
  {
    // Found it
    int value = (*it).second;
  }
  else
  {
    // inpStr not found
  }
}

Code usually get a lott simpler/less cluttered if you "mapify" choces like this", specially if it's a lot of options to choose from.

If it actually is a huge amount of options to choose from another benefit would kick in; that the map's find works in logarithmic time and which is a lot faster than the if statemens linear time.


/Per
[sub]
www.perfnurt.se[/sub]
 
That is really good idea, however, what to do if those defines comes from some include files that cannot be changed. Of course I can perform mapping of those defines
Code:
#include fileWithDefines.h
map["1ST_OPTION"] = 1ST_OPTION;
and I believe it would be much improve but maybe there is another method.
 
Since I'm a bit allergic to macros (I would even suggest replacing your current macros with const int) I probably shouldn't suggest this...but anyway...

You could define a macro that uses the stringify prepocessor operator (#), like this:
Code:
#define ASSIGN_TO_MAP(map, value) map[#value]=value

Then you can set up the map like this
Code:
MyMap map;
ASSIGN_TO_MAP(map, FIRST_OPTION);
ASSIGN_TO_MAP(map, SECOND_OPTION);
ASSIGN_TO_MAP(map, THIRD_OPTION);
//etc ...

/*
   ASSIGN_TO_MAP(map, FIRST_OPTION);
   expands to
   map["FIRST_OPTION"]=FIRST_OPTION;
*/




/Per
[sub]
www.perfnurt.se[/sub]
 
If all those #defines are related, I'd change them to an enum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top