diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-03-30 13:29:46 +0200 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2021-03-31 16:33:01 +0300 |
commit | 7083a1dc7d395df78d8e208c26ad996c5a6394f5 (patch) | |
tree | 439e894cdd30b968efbbba852f03c131a5c846e7 /libbutl/utility.ixx | |
parent | 7190ae233819cd68f5fce2564f7221ffe23a83bf (diff) |
Add notion of thread environment
Diffstat (limited to 'libbutl/utility.ixx')
-rw-r--r-- | libbutl/utility.ixx | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/libbutl/utility.ixx b/libbutl/utility.ixx index fa37a14..40b7a84 100644 --- a/libbutl/utility.ixx +++ b/libbutl/utility.ixx @@ -4,7 +4,6 @@ #ifndef __cpp_lib_modules_ts #include <cctype> // toupper(), tolower(), is*() #include <cwctype> // isw*() -#include <cstdlib> // getenv() #include <algorithm> // for_each() #include <stdexcept> // invalid_argument #endif @@ -333,13 +332,47 @@ namespace butl return utf8_length_impl (s, nullptr, ts, wl).has_value (); } - inline optional<std::string> - getenv (const std::string& name) + // auto_thread_env + // + inline auto_thread_env:: + auto_thread_env (const char* const* new_env) { - if (const char* r = std::getenv (name.c_str ())) - return std::string (r); + // Note that this backwards logic (first initializing prev_env and then + // resetting it) is here to work around bogus "maybe used uninitialized" + // warning in GCC. + // + prev_env = thread_env (); + + if (*prev_env != new_env) + thread_env (new_env); + else + prev_env = nullopt; + } - return nullopt; + inline auto_thread_env:: + auto_thread_env (auto_thread_env&& x) + : prev_env (std::move (x.prev_env)) + { + x.prev_env = nullopt; + } + + inline auto_thread_env& auto_thread_env:: + operator= (auto_thread_env&& x) + { + if (this != &x) + { + prev_env = std::move (x.prev_env); + x.prev_env = nullopt; + } + + return *this; + } + + inline auto_thread_env:: + ~auto_thread_env () + { + if (prev_env) + thread_env (*prev_env); } template <typename F, typename P> |