CMS 3D CMS Logo

VariableMapCont.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 #include <iostream>
3 #include <fstream>
4 #include <iomanip>
5 #include <cassert>
6 #include <algorithm>
8 
9 using namespace std;
10 using namespace optutl;
11 
12 const int VariableMapCont::kDefaultInteger = 0;
13 const double VariableMapCont::kDefaultDouble = 0.;
14 const std::string VariableMapCont::kDefaultString = "";
15 const bool VariableMapCont::kDefaultBool = false;
16 const VariableMapCont::IVec VariableMapCont::kEmptyIVec;
17 const VariableMapCont::DVec VariableMapCont::kEmptyDVec;
18 const VariableMapCont::SVec VariableMapCont::kEmptySVec;
19 
20 
21 VariableMapCont::VariableMapCont()
22 {
23 }
24 
26 VariableMapCont::hasVariable (string key)
27 {
28  lowercaseString (key);
29  // Look through our maps to see if we've got it
30  if (m_integerMap.end() != m_integerMap.find (key)) return kInteger;
31  if (m_doubleMap.end() != m_doubleMap.find (key)) return kDouble;
32  if (m_stringMap.end() != m_stringMap.find (key)) return kString;
33  if (m_boolMap.end() != m_boolMap.find (key)) return kBool;
34  if (m_integerVecMap.end() != m_integerVecMap.find (key)) return kIntegerVector;
35  if (m_doubleVecMap.end() != m_doubleVecMap.find (key)) return kDoubleVector;
36  if (m_stringVecMap.end() != m_stringVecMap.find (key)) return kStringVector;
37  // if we're here, the answer's no.
38  return kNone;
39 }
40 
41 void
42 VariableMapCont::lowercaseString(string& arg)
43 {
44  // assumes 'toLower(ch)' modifies ch
45  std::for_each (arg.begin(), arg.end(), VariableMapCont::toLower);
46  // // assumes 'toLower(ch)' returns the lower case char
47  // std::transform (arg.begin(), arg.end(), arg.begin(),
48  // VariableMapCont::toLower);
49 }
50 
51 char
52 VariableMapCont::toLower (char& ch)
53 {
54  ch = tolower (ch);
55  return ch;
56 }
57 
58 void
59 VariableMapCont::_checkKey (string &key, const string &description)
60 {
61  // Let's make sure we don't already have this key
62  lowercaseString (key);
63  if ( m_variableModifiedMap.end() != m_variableModifiedMap.find (key) )
64  {
65  cerr << "VariableMapCont::addVariable() Error: Key '" << key
66  << "' has already been defined. Aborting." << endl;
67  assert (0);
68  } // found a duplicate
69  m_variableModifiedMap[key] = false;
70  m_variableDescriptionMap[key] = description;
71 }
72 
73 void
74 VariableMapCont::addOption (string key, OptionType type,
75  const string &description)
76 {
77  _checkKey (key, description);
78  if (kInteger == type)
79  {
80  m_integerMap[key] = kDefaultInteger;
81  return;
82  }
83  if (kDouble == type)
84  {
85  m_doubleMap[key] = kDefaultDouble;
86  return;
87  }
88  if (kString == type)
89  {
90  m_stringMap[key] = kDefaultString;
91  return;
92  }
93  if (kBool == type)
94  {
95  m_boolMap[key] = kDefaultBool;
96  return;
97  }
98  if (kIntegerVector == type)
99  {
100  m_integerVecMap[key] = kEmptyIVec;
101  return;
102  }
103  if (kDoubleVector == type)
104  {
105  m_doubleVecMap[key] = kEmptyDVec;
106  return;
107  }
108  if (kStringVector == type)
109  {
110  m_stringVecMap[key] = kEmptySVec;
111  return;
112  }
113 }
114 
115 void
116 VariableMapCont::addOption (string key, OptionType type,
117  const string &description, int defaultValue)
118 {
119  _checkKey (key, description);
120  if (kInteger != type)
121  {
122  cerr << "VariableMapCont::addOption() Error: Key '" << key
123  << "' is not defined as an integer but has an integer "
124  << "default value. Aborting." << endl;
125  assert (0);
126  }
127  m_integerMap[key] = defaultValue;
128 }
129 
130 void
131 VariableMapCont::addOption (string key, OptionType type,
132  const string &description, double defaultValue)
133 {
134  _checkKey (key, description);
135  if (kDouble != type)
136  {
137  cerr << "VariableMapCont::addOption() Error: Key '" << key
138  << "' is not defined as an double but has an double "
139  << "default value. Aborting." << endl;
140  assert (0);
141  }
142  m_doubleMap[key] = defaultValue;
143 }
144 
145 void
146 VariableMapCont::addOption (string key, OptionType type,
147  const string &description,
148  const string &defaultValue)
149 {
150  _checkKey (key, description);
151  if (kString != type)
152  {
153  cerr << "VariableMapCont::addOption() Error: Key '" << key
154  << "' is not defined as an string but has an string "
155  << "default value. Aborting." << endl;
156  assert (0);
157  }
158  m_stringMap[key] = defaultValue;
159 }
160 
161 void
162 VariableMapCont::addOption (string key, OptionType type,
163  const string &description,
164  const char* defaultValue)
165 {
166  addOption (key, type, description, (string) defaultValue);
167 }
168 
169 void
170 VariableMapCont::addOption (string key, OptionType type,
171  const string &description, bool defaultValue)
172 {
173  _checkKey (key, description);
174  if (kBool != type)
175  {
176  cerr << "VariableMapCont::addOption() Error: Key '" << key
177  << "' is not defined as an bool but has an bool "
178  << "default value. Aborting." << endl;
179  assert (0);
180  }
181  m_boolMap[key] = defaultValue;
182 }
183 
184 
185 int &
186 VariableMapCont::integerValue (std::string key)
187 {
188  lowercaseString (key);
189  SIMapIter iter = m_integerMap.find (key);
190  if (m_integerMap.end() == iter)
191  {
192  cerr << "VariableMapCont::integerValue() Error: key '"
193  << key << "' not found. Aborting." << endl;
194  assert (0);
195  }
196  return iter->second;
197 }
198 
199 double &
200 VariableMapCont::doubleValue (std::string key)
201 {
202  lowercaseString (key);
203  SDMapIter iter = m_doubleMap.find (key);
204  if (m_doubleMap.end() == iter)
205  {
206  cerr << "VariableMapCont::doubleValue() Error: key '"
207  << key << "' not found. Aborting." << endl;
208  assert (0);
209  }
210  return iter->second;
211 }
212 
213 string &
215 {
216  lowercaseString (key);
217  SSMapIter iter = m_stringMap.find (key);
218  if (m_stringMap.end() == iter)
219  {
220  cerr << "VariableMapCont::stringValue() Error: key '"
221  << key << "' not found. Aborting." << endl;
222  assert (0);
223  }
224  return iter->second;
225 }
226 
227 bool &
228 VariableMapCont::boolValue (std::string key)
229 {
230  lowercaseString (key);
231  SBMapIter iter = m_boolMap.find (key);
232  if (m_boolMap.end() == iter)
233  {
234  cerr << "VariableMapCont::boolValue() Error: key '"
235  << key << "' not found. Aborting." << endl;
236  assert (0);
237  }
238  return iter->second;
239 }
240 
242 VariableMapCont::integerVector (std::string key)
243 {
244  lowercaseString (key);
245  SIVecMapIter iter = m_integerVecMap.find (key);
246  if (m_integerVecMap.end() == iter)
247  {
248  cerr << "VariableMapCont::integerVector() Error: key '"
249  << key << "' not found. Aborting." << endl;
250  assert (0);
251  }
252  return iter->second;
253 }
254 
256 VariableMapCont::doubleVector (std::string key)
257 {
258  lowercaseString (key);
259  SDVecMapIter iter = m_doubleVecMap.find (key);
260  if (m_doubleVecMap.end() == iter)
261  {
262  cerr << "VariableMapCont::doubleVector() Error: key '"
263  << key << "' not found. Aborting." << endl;
264  assert (0);
265  }
266  return iter->second;
267 }
268 
270 VariableMapCont::stringVector (std::string key)
271 {
272  lowercaseString (key);
273  SSVecMapIter iter = m_stringVecMap.find (key);
274  if (m_stringVecMap.end() == iter)
275  {
276  cerr << "VariableMapCont::stringVector() Error: key '"
277  << key << "' not found. Aborting." << endl;
278  assert (0);
279  }
280  return iter->second;
281 }
282 
283 bool
284 VariableMapCont::_valueHasBeenModified (const string &key)
285 {
286  SBMapConstIter iter = m_variableModifiedMap.find (key);
287  if (m_variableModifiedMap.end() == iter)
288  {
289  // Not found. Not a valid option
290  cerr << "VariableMapCont::valueHasBeenModfied () Error: '"
291  << key << "' is not a valid key." << endl;
292  return false;
293  }
294  return iter->second;
295 }
296 
297 // friends
298 ostream& operator<< (ostream& o_stream, const VariableMapCont &rhs)
299 {
300  return o_stream;
301 }
type
Definition: HCALResponse.h:21
SSMap::iterator SSMapIter
SBMap::iterator SBMapIter
std::vector< double > DVec
SIMap::iterator SIMapIter
std::vector< std::string > SVec
SBMap::const_iterator SBMapConstIter
SIVecMap::iterator SIVecMapIter
A arg
Definition: Factorize.h:36
std::vector< int > IVec
SSVecMap::iterator SSVecMapIter
SDVecMap::iterator SDVecMapIter
std::ostream & operator<<(std::ostream &out, const std::tuple< Types... > &value)
Definition: Utilities.h:38
UTF-8 string value.
Definition: value.h:29
SDMap::iterator SDMapIter