Tpetra parallel linear algebra  Version of the Day
Tpetra_Details_Environment.hpp
1 #include <map>
2 #include <string>
3 #include <list>
4 
5 #ifndef _TPETRA_DETAILS_ENVIRONMENT_HPP_
6 #define _TPETRA_DETAILS_ENVIRONMENT_HPP_
7 
8 namespace Tpetra {
9 namespace Details {
10 
11 class DefaultEnvironmentVariables {
12  /*
13  * This class merely provides a list of variables that should be cached on
14  * instantiation of the singleton Environment(). This can be moved and made
15  * more general to include default variables for other packages.
16  *
17  */
18  public:
19  static const std::list<std::string> getDefaults() {
20  const std::list<std::string> defaultVariables {"TPETRA_DEBUG",
21  "TPETRA_USE_BLAS"};
22  return defaultVariables;
23  }
24 };
25 
26 
27 class Environment {
28  public:
29  // Get singleton instance
30  static Environment& getInstance() {
31  // The following construction guarantees that theInstance_ will be
32  // destroyed and is instantiated only on first use.
33  static Environment theInstance_;
34  return theInstance_;
35  }
36 
37  private:
38  Environment() {
39  // Initialize the instance
40  std::string variableValue;
41  std::list<std::string>::iterator variableName;
42  std::list<std::string> std_vars(DefaultEnvironmentVariables::getDefaults());
43  for (variableName = std_vars.begin();
44  variableName != std_vars.end();
45  ++variableName) {
46  // By getting the value, it will be cached
47  variableValue = getValue(*variableName);
48  }
49  }
50  std::map<std::string, const char *> environCache_;
51  void cacheVariable(const std::string& variableName,
52  const char* variableValue);
53 
54  public:
55  /* Don't allow any copies of the singleton to appear... (requires CXX11)
56  * Note: Scott Meyers mentions in his Effective Modern
57  * C++ book, that deleted functions should generally
58  * be public as it results in better error messages
59  * due to the compilers behavior to check accessibility
60  before deleted status
61  */
62  Environment(Environment const&) = delete;
63  void operator=(Environment const&) = delete;
64 
70  bool variableIsCached(const std::string& variableName);
71 
80  bool variableExists(const std::string& variableName);
81 
95  bool getBooleanValue(const std::string& variableName,
96  const std::string& defaultValue="");
97 
111  std::string getValue(const std::string& variableName,
112  const std::string& defaultValue="");
113 
114  // /* @brief Sets value of the given environment variable.
115  // *
116  // * @param variableName the name of the environment variable
117  // * @param variableValue the value of the environment variable
118  // * @param overwrite [optional, 1] overwrite the variable if it already exists
119  // *
120  // * @return void
121  // *
122  // */
123  // void setValue(const std::string& variableName,
124  // const std::string& variableValue,
125  // const int overwrite=1);
126 
127 
128  void clearCache();
129 };
130 } // namespace Details
131 } // namespace Tpetra
132 #endif // _TPETRA_DETAILS_ENVIRONMENT_HPP_
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Implementation details of Tpetra.