SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringBijection.h
Go to the documentation of this file.
1 /****************************************************************************/
7 // Bijective Container between string and something else
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 #ifndef StringBijection_h
21 #define StringBijection_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <map>
35 #include <vector>
36 #include <string>
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
46 template< class T >
48 
49 public:
50 
51 #ifdef _MSC_VER
52 #pragma warning(push)
53 #pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer
54 #endif
55  struct Entry {
56  const char* str;
57  const T key;
58  };
59 #ifdef _MSC_VER
60 #pragma warning(pop)
61 #endif
62 
63 
65 
66 
67  StringBijection(Entry entries[], T terminatorKey) {
68  int i = 0;
69  do {
70  insert(entries[i].str, entries[i].key);
71  } while (entries[i++].key != terminatorKey);
72  }
73 
74 
75  void insert(const std::string str, const T key) {
76  myString2T[str] = key;
77  myT2String[key] = str;
78  }
79 
80 
81  T get(const std::string& str) {
82  if (hasString(str)) {
83  return myString2T[str];
84  } else {
85  throw InvalidArgument("String '" + str + "' not found.");
86  }
87  }
88 
89 
90  const std::string& getString(const T key) {
91  if (has(key)) {
92  return myT2String[key];
93  } else {
94  // cannot use toString(key) because that might create an infinite loop
95  throw InvalidArgument("Key not found.");
96  }
97  }
98 
99 
100  bool hasString(const std::string& str) {
101  return myString2T.count(str) != 0;
102  }
103 
104 
105  bool has(const T key) {
106  return myT2String.count(key) != 0;
107  }
108 
109 
110  size_t size() const {
111  return myString2T.size();
112  }
113 
114 
115  std::vector<std::string> getStrings() const {
116  std::vector<std::string> result;
117  typename std::map<T, std::string>::const_iterator it; // learn something new every day
118  for (it = myT2String.begin(); it != myT2String.end(); it++) {
119  result.push_back(it->second);
120  }
121  return result;
122  }
123 
124 
125 private:
126  std::map<std::string, T> myString2T;
127  std::map<T, std::string> myT2String;
128 
129 };
130 
131 #endif
132 
133 /****************************************************************************/
134 
StringBijection(Entry entries[], T terminatorKey)
const std::string & getString(const T key)
std::vector< std::string > getStrings() const
size_t size() const
std::map< std::string, T > myString2T
std::map< T, std::string > myT2String
void insert(const std::string str, const T key)
const char * str
bool has(const T key)
const T key
bool hasString(const std::string &str)