EnvVarCache.cpp
Go to the documentation of this file.
1 #include <NovaDAQUtilities/EnvVarCache.h>
2 #include <NovaDAQUtilities/CacheManager.h>
3 #include <boost/lexical_cast.hpp>
4 
5 namespace novadaq {
6 
7 /**
8  * Returns the single instance of the EnvVarCache.
9  *
10  * @returns EnvVarCache instance.
11  */
13  static TimedCachePolicy policy(100000);
14  static EnvVarCache singletonInstance(policy);
15  return singletonInstance;
16 }
17 
18 /**
19  * Default constructor (private)
20  */
22  novadaq::Cache(policy) {
23 
24  // register this cache with the CacheManager so that it gets
25  // flushed when the CacheManager gets a flush request
27 }
28 
29 /**
30  * Returns the value of the environmental variable with the specified name.
31  * If the env var is not found, an empty string is returned.
32  */
34 
35  // protect against unsynchronized access to the cache
36  boost::mutex::scoped_lock lock(_cacheMutex);
37 
38  // (global reloads are not needed since we only load values as needed)
39  //_reloadIfNeeded();
40 
41  return this->_doGetEnvVar(name);
42 }
43 
44 /**
45  * Returns the value of the environmental variable with the specified name
46  * with the value interpreted as a long integer.
47  */
49 
50  // protect against unsynchronized access to the cache
51  boost::mutex::scoped_lock lock(_cacheMutex);
52 
53  // (global reloads are not needed since we only load values as needed)
54  //_reloadIfNeeded();
55 
56  std::string valueString = this->_doGetEnvVar(name);
57  long longValue = defaultValue;
58  try {
59  longValue = boost::lexical_cast<long>(valueString);
60  }
61  catch (boost::bad_lexical_cast &excpt) {}
62 
63  return longValue;
64 }
65 
66 /**
67  * Returns the value of the environmental variable with the specified name
68  * with the value interpreted as a double-precision number.
69  */
70 double EnvVarCache::getEnvVarAsDouble(std::string name, double defaultValue) {
71 
72  // protect against unsynchronized access to the cache
73  boost::mutex::scoped_lock lock(_cacheMutex);
74 
75  // (global reloads are not needed since we only load values as needed)
76  //_reloadIfNeeded();
77 
78  std::string valueString = this->_doGetEnvVar(name);
79  double doubleValue = defaultValue;
80  try {
81  doubleValue = boost::lexical_cast<double>(valueString);
82  }
83  catch (boost::bad_lexical_cast &excpt) {}
84 
85  return doubleValue;
86 }
87 
88 /**
89  * Returns the value of the environmental variable with the specified name,
90  * for internal use.
91  */
93 
94  // the higher-level methods that call this one take care
95  // of the thread locking...
96 
97  // look up the requested entry in the map
99  std::map<std::string, std::string>::iterator iter = _envVarMap.find(name);
100  if (iter == _envVarMap.end()) {
101 
102  // the getenv system call is not multi-threaded, but we don't need
103  // to add explicit protection here because access to the cache is
104  // protected in the public methods.
105  char *envPtr = getenv(name.c_str());
106  if (envPtr != 0) {
107  std::string tmpString(envPtr);
108  value = tmpString;
109  }
110  else {
111  value = "";
112  }
113  _envVarMap[name] = value;
114  }
115  else {
116  value = iter->second;
117  }
118 
119  return value;
120 }
121 
122 /**
123  * Loads a new set of values into the cache.
124  */
126 
127  // the higher-level methods that call this one take care
128  // of the thread locking...
129 
130  // values are loaded on an as-needed basis, so there is nothing
131  // to be done here
132 }
133 
134 /**
135  * Clears the data in the cache.
136  */
138 
139  // the higher-level methods that call this one take care
140  // of the thread locking...
141 
142  _envVarMap.clear();
143 }
144 
145 } // end of namespace novadaq
const XML_Char * name
Definition: expat.h:151
EnvVarCache(novadaq::CachePolicy policy)
Definition: EnvVarCache.cpp:21
static CacheManager & getInstance()
std::string _doGetEnvVar(std::string name)
Definition: EnvVarCache.cpp:92
static EnvVarCache & getInstance()
Definition: EnvVarCache.cpp:12
std::map< std::string, std::string > _envVarMap
Definition: EnvVarCache.h:48
int getEnvVarAsLong(std::string name, long defaultValue)
Definition: EnvVarCache.cpp:48
double getEnvVarAsDouble(std::string name, double defaultValue)
Definition: EnvVarCache.cpp:70
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::string getenv(std::string const &name)
void registerCache(Cache *cache)
std::string getEnvVar(std::string name)
Definition: EnvVarCache.cpp:33
boost::mutex _cacheMutex
Definition: Cache.h:41
enum BeamMode string