22 #include <boost/algorithm/string.hpp> 32 #if defined(_GNU_SOURCE) 42 namespace this_process
48 std::mutex& env_guard()
55 void for_each(
const std::function<
void(
const std::string&,
const std::string&)>& functor) noexcept(true)
57 std::lock_guard<std::mutex> lg(env_guard());
59 while (it !=
nullptr && *it !=
nullptr)
61 std::string line(*it);
62 functor(line.substr(0,line.find_first_of(
'=')),
63 line.substr(line.find_first_of(
'=')+1));
70 std::lock_guard<std::mutex> lg(env_guard());
72 auto result = ::getenv(key.c_str());
74 if (result ==
nullptr)
77 ss <<
"Variable with name " << key <<
" is not defined in the environment";
78 throw std::runtime_error(ss.str());
81 return std::string{result};
84 std::string
get(
const std::string& key,
85 const std::string& default_value) noexcept(
true)
87 std::lock_guard<std::mutex> lg(env_guard());
89 auto result = ::getenv(key.c_str());
90 return std::string{result ? result : default_value};
95 std::lock_guard<std::mutex> lg(env_guard());
97 auto rc = ::unsetenv(key.c_str());
100 throw std::system_error(errno, std::system_category());
104 std::error_code& se) noexcept(
true)
106 std::lock_guard<std::mutex> lg(env_guard());
108 auto rc = ::unsetenv(key.c_str());
112 se = std::error_code(errno, std::system_category());
120 const std::string& value)
122 std::lock_guard<std::mutex> lg(env_guard());
124 static const int overwrite = 0;
125 auto rc = ::setenv(key.c_str(), value.c_str(), overwrite);
128 throw std::system_error(errno, std::system_category());
131 bool set(
const std::string &key,
132 const std::string &value,
133 std::error_code& se) noexcept(
true)
135 std::lock_guard<std::mutex> lg(env_guard());
137 static const int overwrite = 0;
138 auto rc = ::setenv(key.c_str(), value.c_str(), overwrite);
142 se = std::error_code(errno, std::system_category());
152 static const Process self{getpid()};
161 std::istream&
cin() noexcept(true)
166 std::ostream&
cout() noexcept(true)
171 std::ostream&
cerr() noexcept(true)
The Process class models a process and possible operations on it.
CORE_POSIX_DLL_PUBLIC std::istream & cin() noexcept(true)
Access this process's stdin.
CORE_POSIX_DLL_PUBLIC void for_each(const std::function< void(const std::string &, const std::string &)> &functor) noexcept(true)
for_each invokes a functor for every key-value pair in the environment.
CORE_POSIX_DLL_PUBLIC std::ostream & cerr() noexcept(true)
Access this process's stderr.
CORE_POSIX_DLL_PUBLIC void set_or_throw(const std::string &key, const std::string &value)
set_or_throw will adjust the contents of the variable identified by key to the provided value...
CORE_POSIX_DLL_PUBLIC std::string get_or_throw(const std::string &key)
get queries the value of an environment variable.
CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true)
Returns a Process instance corresponding to this process.
CORE_POSIX_DLL_PUBLIC void unset_or_throw(const std::string &key)
unset_or_throw removes the variable with name key from the environment.
CORE_POSIX_DLL_PUBLIC std::ostream & cout() noexcept(true)
Access this process's stdout.
CORE_POSIX_DLL_PUBLIC bool unset(const std::string &key, std::error_code &se) noexcept(true)
unset removes the variable with name key from the environment.
CORE_POSIX_DLL_PUBLIC Process parent() noexcept(true)
Query the parent of the process.