MinVR  0.9.0
A multi-platform virtual reality library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ConfigVal.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 CONFIGVAL_H
29 #define CONFIGVAL_H
30 
31 #include <iostream>
32 #include <sstream>
33 #include "MVRCore/ConfigMap.H"
34 
35 #define BOOST_ASSERT_MSG_OSTREAM std::cout
36 #include <boost/assert.hpp>
37 
38 namespace MinVR {
39 
89 class ConfigValMap
92 {
93 public:
94  static ConfigMapRef map;
95 };
96 
97 
98 
100 template <class T>
101 static inline bool retypeVal(const std::string &str, T &val) {
102  std::istringstream is(str.c_str());
103  is >> val;
104  if (!is) return false;
105  else return true;
106 }
107 
108 
109 static inline void notFoundWarning(const std::string &keyString) {
110  std::cout << "No ConfigVal mapping found for \"" << keyString << "\"." << std::endl;
111 }
112 
113 
119 template <class KEYTYPE, class VALTYPE>
120 VALTYPE ConfigVal(KEYTYPE keyString, const VALTYPE &defaultVal, bool warn=true) {
121  BOOST_ASSERT_MSG(ConfigValMap::map.notNull(), "The global config map is NULL!");
122  if (!ConfigValMap::map->containsKey(keyString)) {
123  if (warn) {
124  notFoundWarning(keyString);
125  }
126  return defaultVal;
127  }
128  else {
129  std::string valString = ConfigValMap::map->getValue(keyString);
130  //std::cout << "valString = " << valString << std::endl;
131  VALTYPE val;
132  bool ok = retypeVal(valString, val);
133  if (!ok) {
134  std::string errString = string("ERROR: ConfigVal is unable to retype value.\n\tKey:\t'")
135  + keyString + string("'\n\tValue:\t'") + valString + string("'\n");
136  std::cout << errString;
137  }
138  return val;
139  }
140 }
141 
142 
143 // Override the template above for the case of a quoted string passed in as the
144 // default value for the ConfigVal function call. The quoted string takes on
145 // different types depending on the compiler (hence the #ifdef's below.)
146 
147 #if (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x500) || \
148 (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
149 #define QUOTED_STRING const char *
150 #else
151 #define QUOTED_STRING char *
152 #endif
153 
154 inline std::string ConfigVal(QUOTED_STRING keyString, QUOTED_STRING defaultVal, bool warn=true) {
155  BOOST_ASSERT_MSG(ConfigValMap::map != nullptr, "The global config map is NULL!");
156  if (ConfigValMap::map->containsKey(keyString))
157  return replaceEnvVars(ConfigValMap::map->getValue(keyString));
158  else {
159  if (warn) notFoundWarning(keyString);
160  return replaceEnvVars(std::string(defaultVal));
161  }
162 }
163 
164 inline std::string ConfigVal(QUOTED_STRING keyString, std::string defaultVal, bool warn=true) {
165  BOOST_ASSERT_MSG(ConfigValMap::map != nullptr , "The global config map is NULL!");
166  if (ConfigValMap::map->containsKey(keyString))
167  return replaceEnvVars(ConfigValMap::map->getValue(keyString));
168  else {
169  if (warn) notFoundWarning(keyString);
170  return replaceEnvVars(defaultVal);
171  }
172 }
173 
174 inline std::string ConfigVal(std::string keyString, QUOTED_STRING defaultVal, bool warn=true) {
175  BOOST_ASSERT_MSG(ConfigValMap::map != nullptr, "The global config map is NULL!");
176  if (ConfigValMap::map->containsKey(keyString))
177  return replaceEnvVars(ConfigValMap::map->getValue(keyString));
178  else {
179  if (warn) notFoundWarning(keyString);
180  return replaceEnvVars(std::string(defaultVal));
181  }
182 }
183 
184 inline std::string ConfigVal(std::string keyString, std::string defaultVal, bool warn=true) {
185  BOOST_ASSERT_MSG(ConfigValMap::map != nullptr, "The global config map is NULL!");
186  if (ConfigValMap::map->containsKey(keyString))
187  return replaceEnvVars(ConfigValMap::map->getValue(keyString));
188  else {
189  if (warn) notFoundWarning(keyString);
190  return replaceEnvVars(defaultVal);
191  }
192 }
193 
194 
195 } // end namespace
196 
197 
198 #endif
199