From 7083a1dc7d395df78d8e208c26ad996c5a6394f5 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 30 Mar 2021 13:29:46 +0200 Subject: Add notion of thread environment --- libbutl/utility.ixx | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'libbutl/utility.ixx') 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 // toupper(), tolower(), is*() #include // isw*() -#include // getenv() #include // for_each() #include // invalid_argument #endif @@ -333,13 +332,47 @@ namespace butl return utf8_length_impl (s, nullptr, ts, wl).has_value (); } - inline optional - 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 -- cgit v1.1