MOOCHO (Single Doxygen Collection) Version of the Day
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes
OptionsFromStreamPack::StringToIntMap Class Reference

Map a string to an enumeration. More...

#include <OptionsFromStreamPack_StringToIntMap.hpp>

List of all members.

Classes

class  AlreadyExists
  More...
class  DoesNotExist
  More...

Public Member Functions

 StringToIntMap (const std::string &name, int n, const char *strings[])
 
int operator() (const std::string &str) const
 
const std::string & name () const
 

Private Types

typedef std::map< std::string,
int > 
map_t

Private Member Functions

 StringToIntMap ()

Private Attributes

std::string name_
map_t map_

Detailed Description

Map a string to an enumeration.

The purpose of this class is to simplify mapping a standard string to an integer which can be interpreted as an enumeration.

Here is an example of its use.


  const int n_opt = 3;
  enum MyOptEnum {
    OPT_ONE
    ,OPT_TWO
    ,OPT_THREE
  };	// must be 0, 1,..., n_opt - 1
  const char* MyOptStrings[n_opt] = {
    "OPT_ONE
    ,"OPT_TWO"
    ,"OPT_THREE"
  }; // parallels MyOptEnum
  StringToIntMap my_enum_map( "opt_map", n_opt, NyOptStrings );
  ...
  switch( (MyEnum)my_enum_map( "OPT_ONE" ) ) {
    case OPT_ONE:
      // do stuff
    case OPT_TWO:
      // do stuff
    case OPT_THREE:
      // do stuff
    default:
      // ???
  }

  

The number of strings passed to the constructor must equal the number of options in the enumeration. If there are duplicate strings (capitalization concidered) then the exception #AlreadyExists# is throw. If a string that was not passed in the constructor if given to #operator()( const std::string& str )# then the exception #DoesNotExist# is thrown.

In the constructor, name# is used in error messages in the exceptions thrown to help make since out of the message.

The default constructor is not defined and not to be called.

Definition at line 83 of file OptionsFromStreamPack_StringToIntMap.hpp.


Member Typedef Documentation

typedef std::map< std::string, int > OptionsFromStreamPack::StringToIntMap::map_t [private]

Definition at line 104 of file OptionsFromStreamPack_StringToIntMap.hpp.


Constructor & Destructor Documentation

OptionsFromStreamPack::StringToIntMap::StringToIntMap ( const std::string &  name,
int  n,
const char *  strings[] 
)

Definition at line 33 of file OptionsFromStreamPack_StringToIntMap.cpp.

OptionsFromStreamPack::StringToIntMap::StringToIntMap ( ) [private]

Member Function Documentation

int OptionsFromStreamPack::StringToIntMap::operator() ( const std::string &  str) const

Definition at line 47 of file OptionsFromStreamPack_StringToIntMap.cpp.

const std::string & OptionsFromStreamPack::StringToIntMap::name ( ) const [inline]

Definition at line 117 of file OptionsFromStreamPack_StringToIntMap.hpp.


Member Data Documentation

Definition at line 105 of file OptionsFromStreamPack_StringToIntMap.hpp.

Definition at line 106 of file OptionsFromStreamPack_StringToIntMap.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines