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 VariableMapCont::VariableMapCont() {}
21 
22 VariableMapCont::OptionType VariableMapCont::hasVariable(string key) {
23  lowercaseString(key);
24  // Look through our maps to see if we've got it
25  if (m_integerMap.end() != m_integerMap.find(key))
26  return kInteger;
27  if (m_doubleMap.end() != m_doubleMap.find(key))
28  return kDouble;
29  if (m_stringMap.end() != m_stringMap.find(key))
30  return kString;
31  if (m_boolMap.end() != m_boolMap.find(key))
32  return kBool;
33  if (m_integerVecMap.end() != m_integerVecMap.find(key))
34  return kIntegerVector;
35  if (m_doubleVecMap.end() != m_doubleVecMap.find(key))
36  return kDoubleVector;
37  if (m_stringVecMap.end() != m_stringVecMap.find(key))
38  return kStringVector;
39  // if we're here, the answer's no.
40  return kNone;
41 }
42 
43 void VariableMapCont::lowercaseString(string &arg) {
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 VariableMapCont::toLower(char &ch) {
52  ch = tolower(ch);
53  return ch;
54 }
55 
56 void VariableMapCont::_checkKey(string &key, const string &description) {
57  // Let's make sure we don't already have this key
58  lowercaseString(key);
59  if (m_variableModifiedMap.end() != m_variableModifiedMap.find(key)) {
60  cerr << "VariableMapCont::addVariable() Error: Key '" << key << "' has already been defined. Aborting." << endl;
61  assert(0);
62  } // found a duplicate
63  m_variableModifiedMap[key] = false;
64  m_variableDescriptionMap[key] = description;
65 }
66 
67 void VariableMapCont::addOption(string key, OptionType type, const string &description) {
68  _checkKey(key, description);
69  if (kInteger == type) {
70  m_integerMap[key] = kDefaultInteger;
71  return;
72  }
73  if (kDouble == type) {
74  m_doubleMap[key] = kDefaultDouble;
75  return;
76  }
77  if (kString == type) {
78  m_stringMap[key] = kDefaultString;
79  return;
80  }
81  if (kBool == type) {
82  m_boolMap[key] = kDefaultBool;
83  return;
84  }
85  if (kIntegerVector == type) {
86  m_integerVecMap[key] = kEmptyIVec;
87  return;
88  }
89  if (kDoubleVector == type) {
90  m_doubleVecMap[key] = kEmptyDVec;
91  return;
92  }
93  if (kStringVector == type) {
94  m_stringVecMap[key] = kEmptySVec;
95  return;
96  }
97 }
98 
99 void VariableMapCont::addOption(string key, OptionType type, const string &description, int defaultValue) {
100  _checkKey(key, description);
101  if (kInteger != type) {
102  cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an integer but has an integer "
103  << "default value. Aborting." << endl;
104  assert(0);
105  }
106  m_integerMap[key] = defaultValue;
107 }
108 
109 void VariableMapCont::addOption(string key, OptionType type, const string &description, double defaultValue) {
110  _checkKey(key, description);
111  if (kDouble != type) {
112  cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an double but has an double "
113  << "default value. Aborting." << endl;
114  assert(0);
115  }
116  m_doubleMap[key] = defaultValue;
117 }
118 
119 void VariableMapCont::addOption(string key, OptionType type, const string &description, const string &defaultValue) {
120  _checkKey(key, description);
121  if (kString != type) {
122  cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an string but has an string "
123  << "default value. Aborting." << endl;
124  assert(0);
125  }
126  m_stringMap[key] = defaultValue;
127 }
128 
129 void VariableMapCont::addOption(string key, OptionType type, const string &description, const char *defaultValue) {
130  addOption(key, type, description, (string)defaultValue);
131 }
132 
133 void VariableMapCont::addOption(string key, OptionType type, const string &description, bool defaultValue) {
134  _checkKey(key, description);
135  if (kBool != type) {
136  cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an bool but has an bool "
137  << "default value. Aborting." << endl;
138  assert(0);
139  }
140  m_boolMap[key] = defaultValue;
141 }
142 
143 int &VariableMapCont::integerValue(std::string key) {
144  lowercaseString(key);
145  SIMapIter iter = m_integerMap.find(key);
146  if (m_integerMap.end() == iter) {
147  cerr << "VariableMapCont::integerValue() Error: key '" << key << "' not found. Aborting." << endl;
148  assert(0);
149  }
150  return iter->second;
151 }
152 
153 double &VariableMapCont::doubleValue(std::string key) {
154  lowercaseString(key);
155  SDMapIter iter = m_doubleMap.find(key);
156  if (m_doubleMap.end() == iter) {
157  cerr << "VariableMapCont::doubleValue() Error: key '" << key << "' not found. Aborting." << endl;
158  assert(0);
159  }
160  return iter->second;
161 }
162 
164  lowercaseString(key);
165  SSMapIter iter = m_stringMap.find(key);
166  if (m_stringMap.end() == iter) {
167  cerr << "VariableMapCont::stringValue() Error: key '" << key << "' not found. Aborting." << endl;
168  assert(0);
169  }
170  return iter->second;
171 }
172 
173 bool &VariableMapCont::boolValue(std::string key) {
174  lowercaseString(key);
175  SBMapIter iter = m_boolMap.find(key);
176  if (m_boolMap.end() == iter) {
177  cerr << "VariableMapCont::boolValue() Error: key '" << key << "' not found. Aborting." << endl;
178  assert(0);
179  }
180  return iter->second;
181 }
182 
183 VariableMapCont::IVec &VariableMapCont::integerVector(std::string key) {
184  lowercaseString(key);
185  SIVecMapIter iter = m_integerVecMap.find(key);
186  if (m_integerVecMap.end() == iter) {
187  cerr << "VariableMapCont::integerVector() Error: key '" << key << "' not found. Aborting." << endl;
188  assert(0);
189  }
190  return iter->second;
191 }
192 
193 VariableMapCont::DVec &VariableMapCont::doubleVector(std::string key) {
194  lowercaseString(key);
195  SDVecMapIter iter = m_doubleVecMap.find(key);
196  if (m_doubleVecMap.end() == iter) {
197  cerr << "VariableMapCont::doubleVector() Error: key '" << key << "' not found. Aborting." << endl;
198  assert(0);
199  }
200  return iter->second;
201 }
202 
203 VariableMapCont::SVec &VariableMapCont::stringVector(std::string key) {
204  lowercaseString(key);
205  SSVecMapIter iter = m_stringVecMap.find(key);
206  if (m_stringVecMap.end() == iter) {
207  cerr << "VariableMapCont::stringVector() Error: key '" << key << "' not found. Aborting." << endl;
208  assert(0);
209  }
210  return iter->second;
211 }
212 
213 bool VariableMapCont::_valueHasBeenModified(const string &key) {
214  SBMapConstIter iter = m_variableModifiedMap.find(key);
215  if (m_variableModifiedMap.end() == iter) {
216  // Not found. Not a valid option
217  cerr << "VariableMapCont::valueHasBeenModfied () Error: '" << key << "' is not a valid key." << endl;
218  return false;
219  }
220  return iter->second;
221 }
222 
223 // friends
224 ostream &operator<<(ostream &o_stream, const VariableMapCont &rhs) { return o_stream; }
SSMap::iterator SSMapIter
SBMap::iterator SBMapIter
SIMap::iterator SIMapIter
SBMap::const_iterator SBMapConstIter
SIVecMap::iterator SIVecMapIter
std::vector< double > DVec
std::vector< std::string > SVec
assert(be >=bs)
A arg
Definition: Factorize.h:31
key
prepare the HTCondor submission files and eventually submit them
SSVecMap::iterator SSVecMapIter
SDVecMap::iterator SDVecMapIter
std::ostream & operator<<(std::ostream &out, const std::tuple< Types... > &value)
Definition: Utilities.h:32
UTF-8 string value.
Definition: value.h:28
std::vector< int > IVec
SDMap::iterator SDMapIter