Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
novadaq::EnvVarCache Class Reference

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N21-05-05/NovaDAQUtilities/cxx/include/EnvVarCache.h"

Inheritance diagram for novadaq::EnvVarCache:
novadaq::Cache

Public Member Functions

std::string getEnvVar (std::string name)
 
int getEnvVarAsLong (std::string name, long defaultValue)
 
double getEnvVarAsDouble (std::string name, double defaultValue)
 
void flush ()
 

Static Public Member Functions

static EnvVarCachegetInstance ()
 

Protected Member Functions

std::string _doGetEnvVar (std::string name)
 
void _loadDataIntoCache ()
 
void _clearCachedData ()
 
void _reloadIfNeeded ()
 

Protected Attributes

CachePolicy _policy
 
bool _cacheIsEmpty
 
boost::mutex _cacheMutex
 

Private Member Functions

 EnvVarCache (novadaq::CachePolicy policy)
 
 EnvVarCache (EnvVarCache const &)
 
void operator= (EnvVarCache const &)
 

Private Attributes

std::map< std::string, std::string_envVarMap
 

Detailed Description

A singleton class that serves as a cache of environmental variable values.

Author
Kurt Biery
Version
Revision
1.1.34.1
Date
2019/09/27 00:07:20

Definition at line 18 of file EnvVarCache.h.

Constructor & Destructor Documentation

novadaq::EnvVarCache::EnvVarCache ( novadaq::CachePolicy  policy)
explicitprivate

Default constructor (private)

Definition at line 21 of file EnvVarCache.cpp.

References novadaq::CacheManager::getInstance(), and novadaq::CacheManager::registerCache().

21  :
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 }
static CacheManager & getInstance()
void registerCache(Cache *cache)
novadaq::EnvVarCache::EnvVarCache ( EnvVarCache const &  )
private

Member Function Documentation

void novadaq::EnvVarCache::_clearCachedData ( )
protectedvirtual

Clears the data in the cache.

Implements novadaq::Cache.

Definition at line 137 of file EnvVarCache.cpp.

References _envVarMap.

137  {
138 
139  // the higher-level methods that call this one take care
140  // of the thread locking...
141 
142  _envVarMap.clear();
143 }
std::map< std::string, std::string > _envVarMap
Definition: EnvVarCache.h:48
std::string novadaq::EnvVarCache::_doGetEnvVar ( std::string  name)
protected

Returns the value of the environmental variable with the specified name, for internal use.

Definition at line 92 of file EnvVarCache.cpp.

References _envVarMap, cet::getenv(), and string.

Referenced by getEnvVar(), getEnvVarAsDouble(), and getEnvVarAsLong().

92  {
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 }
const XML_Char * name
Definition: expat.h:151
std::map< std::string, std::string > _envVarMap
Definition: EnvVarCache.h:48
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::string getenv(std::string const &name)
enum BeamMode string
void novadaq::EnvVarCache::_loadDataIntoCache ( )
protectedvirtual

Loads a new set of values into the cache.

Implements novadaq::Cache.

Definition at line 125 of file EnvVarCache.cpp.

125  {
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 }
void novadaq::Cache::_reloadIfNeeded ( )
protectedinherited

Reloads the cache if needed.

Definition at line 25 of file Cache.cpp.

References novadaq::Cache::_cacheIsEmpty, novadaq::Cache::_clearCachedData(), novadaq::Cache::_loadDataIntoCache(), novadaq::Cache::_policy, novadaq::CachePolicy::cacheUpdated(), and novadaq::CachePolicy::isExpired().

Referenced by novadaq::Cache::~Cache().

25  {
26  if (_cacheIsEmpty || _policy.isExpired()) {
29  _cacheIsEmpty = false;
31  }
32 }
bool _cacheIsEmpty
Definition: Cache.h:40
virtual void _clearCachedData()=0
CachePolicy _policy
Definition: Cache.h:39
virtual void _loadDataIntoCache()=0
void novadaq::Cache::flush ( )
inherited

Flushes the cache. (Removes any saved data.)

Definition at line 8 of file Cache.cpp.

References novadaq::Cache::_cacheIsEmpty, novadaq::Cache::_cacheMutex, and novadaq::Cache::_clearCachedData().

Referenced by CacheTests::testEnvVarCacheBasic(), and CacheTests::testEnvVarCacheNumbers().

8  {
9  boost::mutex::scoped_lock lock(_cacheMutex);
11  _cacheIsEmpty = true;
12 }
bool _cacheIsEmpty
Definition: Cache.h:40
virtual void _clearCachedData()=0
boost::mutex _cacheMutex
Definition: Cache.h:41
std::string novadaq::EnvVarCache::getEnvVar ( std::string  name)

Returns the value of the environmental variable with the specified name. If the env var is not found, an empty string is returned.

Definition at line 33 of file EnvVarCache.cpp.

References novadaq::Cache::_cacheMutex, and _doGetEnvVar().

Referenced by dbutils::gui::BasicEditorDisplay::_confirmSave(), dbutils::daqappmgr::ConfiguredProcessList::_fetchProcessData(), dbutils::daqconfig::DLRunConfig::_fetchStreamData(), dbutils::daqconfig::BNEVBRunConfig::_fetchSystemData(), dbutils::daqconfig::DLRunConfig::_fetchSystemData(), dbutils::daqconfig::GTRunConfig::_fetchTriggerData(), dbutils::gui::ConfigDataTableModel::ConfigDataTableModel(), dbutils::daqconfig::ConfigDataTree::ConfigDataTree(), dbutils::daqconfig::NamedConfigUtils::copyGlobalConfig(), dbutils::daqconfig::NamedConfigUtils::copySubsystemConfig(), dbutils::gui::DAQConfigEditorDisplay::DAQConfigEditorDisplay(), dbutils::gui::DBParamsGroupBox::DBParamsGroupBox(), dbutils::gui::DCSConfigEditorDisplay::DCSConfigEditorDisplay(), dbutils::gui::DCSConfigEditorDisplay::generateRecipeScripts(), dbutils::daqconfig::NamedConfigUtils::getAvailableGlobalConfigs(), dbutils::daqconfig::NamedConfigUtils::getAvailableSubsystemConfigs(), dbutils::daqconfig::NamedConfigUtils::getConfigDetailAncestry(), dbutils::daqconfig::NamedConfigUtils::getConfigIdAncestry(), dbutils::daqconfig::NamedConfigUtils::getGlobalConfigDetailAncestry(), dbutils::daqconfig::NamedConfigUtils::getGlobalConfigId(), dbutils::daqconfig::NamedConfigUtils::getGlobalConfigIdAncestry(), dbutils::daqconfig::NamedConfigUtils::getNewestGlobalConfigFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getNewestGlobalConfigFromSubsystemId(), dbutils::daqconfig::NamedConfigUtils::getNewestGlobalConfigFromSubsystemName(), dbutils::daqconfig::NamedConfigUtils::getNewestInstanceOfLatestUsedDAQGlobalConfig(), dbutils::daqconfig::NamedConfigUtils::getRecentGlobalConfigs(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdAncestry(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdAncestryFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdFromGlobalName(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdsFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigsFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::incrementUsageCount(), dbutils::daqconfig::NamedConfigUtils::markConfigDone(), dbutils::gui::BasicEditorDisplay::reactToDataChange(), dbutils::daqconfig::ConfigDataTree::saveValues(), CacheTests::testEnvVarCacheBasic(), and CacheTests::testEnvVarCacheNumbers().

33  {
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 }
const XML_Char * name
Definition: expat.h:151
std::string _doGetEnvVar(std::string name)
Definition: EnvVarCache.cpp:92
boost::mutex _cacheMutex
Definition: Cache.h:41
double novadaq::EnvVarCache::getEnvVarAsDouble ( std::string  name,
double  defaultValue 
)

Returns the value of the environmental variable with the specified name with the value interpreted as a double-precision number.

Definition at line 70 of file EnvVarCache.cpp.

References novadaq::Cache::_cacheMutex, _doGetEnvVar(), and string.

Referenced by CacheTests::testEnvVarCacheNumbers().

70  {
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 }
const XML_Char * name
Definition: expat.h:151
std::string _doGetEnvVar(std::string name)
Definition: EnvVarCache.cpp:92
boost::mutex _cacheMutex
Definition: Cache.h:41
enum BeamMode string
int novadaq::EnvVarCache::getEnvVarAsLong ( std::string  name,
long  defaultValue 
)

Returns the value of the environmental variable with the specified name with the value interpreted as a long integer.

Definition at line 48 of file EnvVarCache.cpp.

References novadaq::Cache::_cacheMutex, _doGetEnvVar(), and string.

Referenced by CacheTests::testEnvVarCacheNumbers().

48  {
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 }
const XML_Char * name
Definition: expat.h:151
std::string _doGetEnvVar(std::string name)
Definition: EnvVarCache.cpp:92
boost::mutex _cacheMutex
Definition: Cache.h:41
enum BeamMode string
EnvVarCache & novadaq::EnvVarCache::getInstance ( )
static

Returns the single instance of the EnvVarCache.

Returns
EnvVarCache instance.

Definition at line 12 of file EnvVarCache.cpp.

Referenced by dbutils::gui::BasicEditorDisplay::_confirmSave(), dbutils::daqappmgr::ConfiguredProcessList::_fetchProcessData(), dbutils::daqconfig::DLRunConfig::_fetchStreamData(), dbutils::daqconfig::BNEVBRunConfig::_fetchSystemData(), dbutils::daqconfig::DLRunConfig::_fetchSystemData(), dbutils::daqconfig::GTRunConfig::_fetchTriggerData(), dbutils::gui::ConfigDataTableModel::ConfigDataTableModel(), dbutils::daqconfig::ConfigDataTree::ConfigDataTree(), gov::fnal::cd::rms::provider::DPSingleton::connect_participant(), dbutils::daqconfig::NamedConfigUtils::copyGlobalConfig(), dbutils::daqconfig::NamedConfigUtils::copySubsystemConfig(), dbutils::gui::DAQConfigEditorDisplay::DAQConfigEditorDisplay(), dbutils::gui::DBParamsGroupBox::DBParamsGroupBox(), dbutils::gui::DCSConfigEditorDisplay::DCSConfigEditorDisplay(), dbutils::gui::DCSConfigEditorDisplay::generateRecipeScripts(), dbutils::daqconfig::NamedConfigUtils::getAvailableGlobalConfigs(), dbutils::daqconfig::NamedConfigUtils::getAvailableSubsystemConfigs(), dbutils::daqconfig::NamedConfigUtils::getConfigDetailAncestry(), dbutils::daqconfig::NamedConfigUtils::getConfigIdAncestry(), dbutils::daqconfig::NamedConfigUtils::getGlobalConfigDetailAncestry(), dbutils::daqconfig::NamedConfigUtils::getGlobalConfigId(), dbutils::daqconfig::NamedConfigUtils::getGlobalConfigIdAncestry(), dbutils::daqconfig::NamedConfigUtils::getNewestGlobalConfigFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getNewestGlobalConfigFromSubsystemId(), dbutils::daqconfig::NamedConfigUtils::getNewestGlobalConfigFromSubsystemName(), dbutils::daqconfig::NamedConfigUtils::getNewestInstanceOfLatestUsedDAQGlobalConfig(), dbutils::daqconfig::NamedConfigUtils::getRecentGlobalConfigs(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdAncestry(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdAncestryFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdFromGlobalName(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigIdsFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::getSubsystemConfigsFromGlobalId(), dbutils::daqconfig::NamedConfigUtils::incrementUsageCount(), gov::fnal::cd::rms::provider::DDSTopicWriterClass< TT_ >::init(), gov::fnal::cd::rms::provider::DDSTopicReaderClass< TT_ >::init(), dbutils::daqconfig::NamedConfigUtils::markConfigDone(), dbutils::gui::BasicEditorDisplay::reactToDataChange(), dbutils::daqconfig::ConfigDataTree::saveValues(), CacheTests::testEnvVarCacheBasic(), CacheTests::testEnvVarCacheNumbers(), and gov::fnal::cd::rms::provider::DDSTopicWriterClass< TT_ >::waitForNetworkServices().

12  {
13  static TimedCachePolicy policy(100000);
14  static EnvVarCache singletonInstance(policy);
15  return singletonInstance;
16 }
EnvVarCache(novadaq::CachePolicy policy)
Definition: EnvVarCache.cpp:21
void novadaq::EnvVarCache::operator= ( EnvVarCache const &  )
private

Member Data Documentation

bool novadaq::Cache::_cacheIsEmpty
protectedinherited

Definition at line 40 of file Cache.h.

Referenced by novadaq::Cache::_reloadIfNeeded(), and novadaq::Cache::flush().

boost::mutex novadaq::Cache::_cacheMutex
protectedinherited

Definition at line 41 of file Cache.h.

Referenced by novadaq::Cache::flush(), getEnvVar(), getEnvVarAsDouble(), and getEnvVarAsLong().

std::map<std::string, std::string> novadaq::EnvVarCache::_envVarMap
private

Definition at line 48 of file EnvVarCache.h.

Referenced by _clearCachedData(), and _doGetEnvVar().

CachePolicy novadaq::Cache::_policy
protectedinherited

Definition at line 39 of file Cache.h.

Referenced by novadaq::Cache::_reloadIfNeeded().


The documentation for this class was generated from the following files: