7 #ifndef YARP_CONF_ENVIRONMENT_H
8 #define YARP_CONF_ENVIRONMENT_H
19 #include <type_traits>
24 namespace environment {
40 template <
typename ContainerT = std::vector<std::
string>>
53 template <
typename ContainerT = std::vector<std::
string>>
68 inline std::string
get_string(
const std::string& key,
bool* found =
nullptr)
71 if (found !=
nullptr) {
72 *found = (result !=
nullptr);
74 if (result ==
nullptr) {
77 return std::string(result);
89 inline std::string
get_string(
const std::string& key,
const std::string& defaultValue)
92 return result ? std::string{result} : defaultValue;
104 inline std::string
get_string(
const std::string& key,
const std::string& altKey,
const std::string& altDefaultValue,
const std::string& altAppend = {})
108 return (found ?
ret :
get_string(altKey, altDefaultValue) + altAppend);
120 inline std::vector<std::string>
get_path(
const std::string& key,
bool* found =
nullptr)
134 inline std::vector<std::string>
get_path(
const std::string& key,
const std::string& defaultValue)
150 inline std::vector<std::string>
get_path(
const std::string& key,
const std::string& altKey,
const std::string& altDefaultValue,
const std::string& altAppend = {})
153 std::vector<std::string> path =
get_path(key, &found);
155 path =
get_path(altKey, altDefaultValue);
156 if (!altAppend.empty()) {
157 for (
auto& dir : path) {
158 dir.append(altAppend);
174 inline bool get_bool(
const std::string& key,
bool defaultValue =
false)
180 return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
192 template <
typename T>
193 inline T
get_numeric(
const std::string& key, T defaultValue =
static_cast<T
>(0))
199 return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
211 inline bool set_string(
const std::string& key,
const std::string& value)
213 #if defined(_MSC_VER)
214 auto ret = _putenv_s(key.c_str(), value.c_str());
216 auto ret = ::setenv(key.c_str(), value.c_str(), 1);
230 inline bool set_path(
const std::string& key,
const std::vector<std::string>& value)
244 inline bool set_bool(
const std::string& key,
bool value)
260 template <
typename T>
274 inline bool unset(
const std::string& key)
276 #if defined(_MSC_VER)
277 auto ret = _putenv_s(key.c_str(),
"");
279 auto ret = ::unsetenv(key.c_str());
290 inline bool is_set(
const std::string& key)
296 #ifndef YARP_NO_DEPRECATED
307 YARP_DEPRECATED_MSG("Use yarp::conf::environment::get_string() instead")
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
T get_numeric(const std::string &key, T defaultValue=static_cast< T >(0))
Read an numeric value from an environment variable.
bool set_bool(const std::string &key, bool value)
Set a bool to an environment variable (as 1 or 0).
ContainerT split_path(const typename ContainerT::value_type &s)
Utility to split a string containing a path separated by the path_separator, which depends on the sys...
void setEnvironment(const std::string &key, const std::string &value)
Set or change an environment variable.
bool unset(const std::string &key)
Remove an environment variable.
bool is_set(const std::string &key)
Check if an environment variable is set.
bool get_bool(const std::string &key, bool defaultValue=false)
Read a bool value from an environment variable.
ContainerT::value_type join_path(const ContainerT &v)
Utility to join a vector of strings into a single string separated by the proper path_separator,...
static constexpr char path_separator
bool set_string(const std::string &key, const std::string &value)
Set a string to an environment variable.
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
bool set_path(const std::string &key, const std::vector< std::string > &value)
Set a path to an environment variable.
void unsetEnvironment(const std::string &key)
Remove an environment variable.
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
bool set_numeric(const std::string &key, bool value)
Set a numeric value to an environment variable.
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
std::string to_string(IntegerType x)
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
The main, catch-all namespace for YARP.