MinVR  0.9.0
A multi-platform virtual reality library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ConfigMap.H
Go to the documentation of this file.
1 //========================================================================
2 // MinVR
3 // Platform: Any
4 // API version: 1.0
5 //------------------------------------------------------------------------
6 // The MIT License (MIT)
7 //
8 // Copyright (c) 2013 Regents of the University of Minnesota
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy of
11 // this software and associated documentation files (the "Software"), to deal in
12 // the Software without restriction, including without limitation the rights to
13 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14 // the Software, and to permit persons to whom the Software is furnished to do so,
15 // subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in all
18 // copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //========================================================================
27 
28 #ifndef CONFIGMAP_H
29 #define CONFIGMAP_H
30 
31 #include <sstream>
32 #include "MVRCore/StringUtils.H"
33 #include "MVRCore/DataFileUtils.H"
34 #include <boost/shared_ptr.hpp>
35 #include <boost/log/trivial.hpp>
36 #include <boost/filesystem.hpp>
37 #include <boost/filesystem/fstream.hpp>
38 #include <unordered_map>
39 
40 #define BOOST_ASSERT_MSG_OSTREAM std::cout
41 #include <boost/assert.hpp>
42 
43 namespace MinVR {
44 
45 #if (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x500) || \
46  (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
47 #define QUOTED_STRING const char *
48 #else
49 #define QUOTED_STRING char *
50 #endif
51 
52 
116 typedef boost::shared_ptr<class ConfigMap> ConfigMapRef;
123 {
124 public:
127  }
128 
130  ConfigMap(const std::string &filename) {
131  readFile(filename);
132  }
133 
136  ConfigMap(int argc, char **argv, bool exitOnUnrecognizedArgument);
137 
138  virtual ~ConfigMap() {}
139 
140  void printArgumentHelpAndExit(const std::string &programName);
141  bool readFile(const std::string &filename);
142 
143  template <class T>
144  bool retypeString(const std::string &str, T &val) {
145  std::istringstream is(str.c_str());
146  is >> val;
147  if (!is) {
148  return false;
149  }
150  else {
151  return true;
152  }
153  }
154 
155  template <class VALTYPE>
156  VALTYPE get(std::string keyString, const VALTYPE &defaultVal) {
157  if (containsKey(keyString)) {
158  std::string valString = getValue(keyString);
159  VALTYPE val;
160  bool ok = retypeString(valString, val);
161  if (ok) {
162  return val;
163  }
164  else {
165  BOOST_LOG_TRIVIAL(error) << "ConfigMap Error: cannot remap " << valString;
166  return defaultVal;
167  }
168  }
169  else {
170  BOOST_LOG_TRIVIAL(error) << "ConfigMap Error: cannot find " << keyString;
171  return defaultVal;
172  }
173  }
174 
175  std::string get(QUOTED_STRING keyString, QUOTED_STRING defaultVal) {
176  if (containsKey(keyString))
177  return replaceEnvVars(getValue(keyString));
178  else {
179  BOOST_LOG_TRIVIAL(error) << "ConfigMap Warning: no mapping for '" << keyString <<"'";
180  return replaceEnvVars(defaultVal);
181  }
182  }
183 
184  std::string get(QUOTED_STRING keyString, std::string defaultVal) {
185  if (containsKey(keyString))
186  return replaceEnvVars(getValue(keyString));
187  else {
188  BOOST_LOG_TRIVIAL(error) << "ConfigMap Warning: no mapping for '" << keyString <<"'";
189  return replaceEnvVars(defaultVal);
190  }
191  }
192 
193  std::string get(std::string keyString, QUOTED_STRING defaultVal) {
194  if (containsKey(keyString))
195  return replaceEnvVars(getValue(keyString));
196  else {
197  BOOST_LOG_TRIVIAL(error) << "ConfigMap Warning: no mapping for '" << keyString <<"'";
198  return replaceEnvVars(defaultVal);
199  }
200  }
201 
202  std::string get(std::string keyString, std::string defaultVal) {
203  if (containsKey(keyString))
204  return replaceEnvVars(getValue(keyString));
205  else {
206  BOOST_LOG_TRIVIAL(error) << "ConfigMap Warning: no mapping for '" << keyString <<"'";
207  return replaceEnvVars(defaultVal);
208  }
209  }
210 
211  bool containsKey(const std::string &keyString);
212  std::string getValue(const std::string &keyString);
213  void set(const std::string &key, const std::string &value);
214  void debugPrint();
215 
216 private:
217  std::unordered_map<std::string, std::string> _map;
218 };
219 
220 
221 } // end namespace
222 
223 
224 #endif
225