Teuchos::StringToIntMap Class Reference

Map a std::string to an enumeration. More...

#include <Teuchos_StringToIntMap.hpp>

List of all members.

Classes

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

Public Member Functions

 StringToIntMap (const std::string &defaultGroupName, int n, const char *strings[])
 
int get (const std::string &option, const std::string &groupName="") const
 
template<class EnumType >
EnumType get (const std::string &option, const std::string &groupName="") const
 
const std::string & defaultGroupName () const
 

Private Types

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

Private Member Functions

std::string validSelections () const
 StringToIntMap ()

Private Attributes

std::string defaultGroupName_
map_t map_

Related Functions

(Note that these are not member functions.)



template<class EnumType >
EnumType get (StringToIntMap const &theMap, std::string const &option, std::string const &groupName="")
 Nonmember get function.

Detailed Description

Map a std::string to an enumeration.

The purpose of this class is to simplify mapping a standard std::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
  };  // NOTE: Must be 0, 1,..., n_opt - 1
  const char* MyOptStrings[n_opt] = {
    "OPT_ONE
    ,"OPT_TWO"
    ,"OPT_THREE"
  }; // NOTE: parallels enums in MyOptEnum
  StringToIntMap my_enum_map( "opt_map", n_opt, NyOptStrings );
  ...
  switch( my_enum_map.get<MyOptEnum>("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 std::exception AlreadyExists is throw. If a std::string that was not passed in the constructor if given to operator()( const std::string& str ) then the std::exception DoesNotExist is thrown.

In the constructor, defaultGroupName 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 82 of file Teuchos_StringToIntMap.hpp.


Member Typedef Documentation

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

Definition at line 108 of file Teuchos_StringToIntMap.hpp.


Constructor & Destructor Documentation

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

Definition at line 33 of file Teuchos_StringToIntMap.cpp.

Teuchos::StringToIntMap::StringToIntMap (  )  [private]

Member Function Documentation

EnumType Teuchos::StringToIntMap::get ( const std::string &  option,
const std::string &  groupName = "" 
) const [inline]

Definition at line 48 of file Teuchos_StringToIntMap.cpp.

template<class EnumType >
EnumType Teuchos::StringToIntMap::get ( const std::string &  option,
const std::string &  groupName = "" 
) const [inline]

const std::string & Teuchos::StringToIntMap::defaultGroupName (  )  const [inline]

Definition at line 144 of file Teuchos_StringToIntMap.hpp.

std::string Teuchos::StringToIntMap::validSelections (  )  const [private]

Definition at line 63 of file Teuchos_StringToIntMap.cpp.


Friends And Related Function Documentation

template<class EnumType >
EnumType get ( StringToIntMap const &  theMap,
std::string const &  option,
std::string const &  groupName = "" 
) [related]

Nonmember get function.

Definition at line 124 of file Teuchos_StringToIntMap.hpp.


Member Data Documentation

Definition at line 109 of file Teuchos_StringToIntMap.hpp.

Definition at line 110 of file Teuchos_StringToIntMap.hpp.


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

Generated on Tue Oct 20 10:14:05 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1