Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_StringToIntMap.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_StringToIntMap.hpp"
11 
12 namespace Teuchos {
13 
14 StringToIntMap::StringToIntMap(
15  const std::string& defaultGroupName_in, int n, const char* strings[]
16  ) : defaultGroupName_(defaultGroupName_in)
17 {
18  typedef map_t::value_type val_t;
19  for( int i = 0; i < n; ++i ) {
20  const bool unique = map_.insert( val_t( strings[i], i ) ).second;
22  !unique, AlreadyExists
23  ,"Teuchos::StringToIntMap::StringToIntMap(...): "
24  << "Error, the std::string \"" << strings[i] << "\" is a duplicate for "
25  << defaultGroupName_ );
26  }
27 }
28 
29 int StringToIntMap::get( const std::string& option, const std::string& groupName ) const
30 {
31  map_t::const_iterator itr = map_.find( option );
33  itr == map_.end(), DoesNotExist
34  ,"Teuchos::StringToIntMap:::get(\""<<option<<"\",...): "
35  << "Error, the std::string \"" << option << "\" is not recongnised for "
36  << ( groupName.length() ? groupName : defaultGroupName_ )
37  << "; valid selections include " << this->validSelections() << "."
38  );
39  return (*itr).second;
40 }
41 
42 // private
43 
44 std::string StringToIntMap::validSelections() const
45 {
46  std::ostringstream oss;
47  oss << "{";
48  map_t::const_iterator itr = map_.begin();
49  for( int i = 0; itr != map_.end(); ++itr, ++i ) {
50  if(i > 0)
51  oss << ",";
52  oss << "\""<<itr->first<<"\":"<<itr->second;
53  }
54  oss << "}";
55  return oss.str();
56 }
57 
58 } // end namespace Teuchos
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
int get(const std::string &option, const std::string &groupName="") const