29 enum EnvironmentVariableState {
30 EnvironmentVariableIsSet_ON,
31 EnvironmentVariableIsSet_OFF,
32 EnvironmentVariableIsSet,
33 EnvironmentVariableIsNotSet
36 EnvironmentVariableState
37 environmentVariableState(
const std::string &environmentVariableValue) {
39 if (v ==
"1" || v ==
"YES" || v ==
"TRUE" || v ==
"ON")
41 return EnvironmentVariableIsSet_ON;
42 else if (v ==
"0" || v ==
"NO" || v ==
"FALSE" || v ==
"OFF")
44 return EnvironmentVariableIsSet_OFF;
46 return EnvironmentVariableIsSet;
49 void setEnvironmentVariableMap(
50 const char environmentVariableName[],
51 std::map<std::string, std::map<std::string, bool>> &valsMap,
52 const bool defaultValue) {
59 valsMap[environmentVariableName] =
60 map<string, bool>{{
"DEFAULT", defaultValue}};
62 const char *varVal = getenv(environmentVariableName);
63 if (varVal ==
nullptr) {
70 const string varStr(varVal);
74 for (
auto const &name : names) {
75 auto state = environmentVariableState(name);
76 if (state == EnvironmentVariableIsSet_ON) {
79 valsMap[environmentVariableName][
"DEFAULT"] =
true;
80 }
else if (state == EnvironmentVariableIsSet_OFF) {
83 valsMap[environmentVariableName][
"DEFAULT"] =
false;
87 valsMap[environmentVariableName][name] =
true;
96 const T defaultValue) {
97 const char *varVal = std::getenv(environmentVariableName.data());
98 if (varVal ==
nullptr) {
101 std::stringstream ss(varVal);
106 !ss, std::out_of_range,
107 "Environment variable \""
108 << environmentVariableName <<
"\" has a value " << varVal
109 <<
" that cannot be parsed as a " <<
typeid(T).name() <<
".");
118 const std::string_view environmentVariableName,
const bool defaultValue) {
119 const char *varVal = std::getenv(environmentVariableName.data());
120 bool retVal = defaultValue;
121 if (varVal !=
nullptr) {
122 auto state = environmentVariableState(std::string(varVal));
123 if (state == EnvironmentVariableIsSet_ON)
125 else if (state == EnvironmentVariableIsSet_OFF)
139 const size_t defaultValue) {
140 const char *varVal = std::getenv(environmentVariableName.data());
141 if (varVal ==
nullptr) {
145 if (val < static_cast<long long>(0)) {
147 return std::numeric_limits<size_t>::max();
149 if (
sizeof(
long long) >
sizeof(
size_t)) {
153 constexpr
long long maxSizeT =
154 static_cast<long long>(std::numeric_limits<size_t>::max());
156 val > maxSizeT, std::out_of_range,
157 "Environment variable \""
158 << environmentVariableName <<
"\" has a value " << val
159 <<
" larger than the largest size_t value " << maxSizeT <<
".");
161 return static_cast<size_t>(val);
166 const char name[],
bool &initialized,
const char environmentVariableName[],
167 const bool defaultValue) {
168 static std::map<std::string, std::map<std::string, bool>> namedVariableMap_;
170 setEnvironmentVariableMap(environmentVariableName, namedVariableMap_,
174 auto thisEnvironmentVariableMap = namedVariableMap_[environmentVariableName];
175 auto thisEnvironmentVariable = thisEnvironmentVariableMap.find(name);
176 if (thisEnvironmentVariable != thisEnvironmentVariableMap.end())
177 return thisEnvironmentVariable->second;
178 return thisEnvironmentVariableMap[
"DEFAULT"];
181 template <
typename T>
183 T &value,
bool &initialized,
const std::string_view environmentVariableName,
184 const T defaultValue) {
186 value = getEnvironmentVariable<T>(environmentVariableName, defaultValue);
193 template std::string idempotentlyGetEnvironmentVariable<std::string>(std::string&,
bool&,
const std::string_view,
const std::string);
static std::string allCaps(const std::string &str)
Converts a std::string to all upper case.
template unsigned long long idempotentlyGetEnvironmentVariable< unsigned long long >(unsigned long long &, bool &, const std::string_view, const unsigned long long)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool idempotentlyGetNamedEnvironmentVariableAsBool(const char name[], bool &initialized, const char environmentVariableName[], const bool defaultValue)
template unsigned long idempotentlyGetEnvironmentVariable< unsigned long >(unsigned long &, bool &, const std::string_view, const unsigned long)
A std::string utilities class for Teuchos.
T idempotentlyGetEnvironmentVariable(T &value, bool &initialized, const std::string_view environmentVariableName, const T defaultValue)
Read a variable from the environment. Example usage:
static Array< std::string > splitString(const std::string_view s, const char sep= ',')
Split an input std::string using a seperator char sep.
Standard test and throw macros.
template bool idempotentlyGetEnvironmentVariable< bool >(bool &, bool &, const std::string_view, const bool)
template int idempotentlyGetEnvironmentVariable< int >(int &, bool &, const std::string_view, const int)
size_t getEnvironmentVariable< size_t >(const std::string_view environmentVariableName, const size_t defaultValue)
T getEnvironmentVariable(const std::string_view environmentVariableName, const T defaultValue)
bool getEnvironmentVariable< bool >(const std::string_view environmentVariableName, const bool defaultValue)