BuildCache
env_utils.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2020 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_ENV_UTILS_HPP_
21 #define BUILDCACHE_ENV_UTILS_HPP_
22 
23 #include <string>
24 
25 namespace bcache {
27 class env_var_t {
28 public:
31  env_var_t(const std::string& name);
32 
34  operator bool() const {
35  return m_defined;
36  }
37 
39  const std::string& as_string() const {
40  return m_value;
41  }
42 
44  int64_t as_int64() const;
45 
47  bool as_bool() const;
48 
49 private:
50  std::string m_value;
51  bool m_defined;
52 };
53 
56 public:
60  scoped_set_env_t(const std::string& name, const std::string& value);
61 
64 
65 private:
66  std::string m_name;
67  env_var_t m_old_env_var;
68 };
69 
72 public:
75  scoped_unset_env_t(const std::string& name);
76 
79 
80 private:
81  std::string m_name;
82  env_var_t m_old_env_var;
83 };
84 
88 bool env_defined(const std::string& env_var);
89 
94 std::string get_env(const std::string& env_var);
95 
99 void set_env(const std::string& env_var, const std::string& value);
100 
103 void unset_env(const std::string& env_var);
104 
105 } // namespace bcache
106 
107 #endif // BUILDCACHE_ENV_UTILS_HPP_
bcache::env_var_t::as_string
const std::string & as_string() const
Definition: env_utils.hpp:39
bcache::env_var_t::env_var_t
env_var_t(const std::string &name)
Read an environment variable.
Definition: env_utils.cpp:47
bcache::set_env
void set_env(const std::string &env_var, const std::string &value)
Set the named environment variable for this process.
Definition: env_utils.cpp:108
bcache::unset_env
void unset_env(const std::string &env_var)
Unset the named environment variable for this process.
Definition: env_utils.cpp:118
bcache::env_var_t::as_bool
bool as_bool() const
Definition: env_utils.cpp:58
bcache::env_defined
bool env_defined(const std::string &env_var)
Check if the named environment variable is defined.
Definition: env_utils.cpp:88
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::scoped_set_env_t::scoped_set_env_t
scoped_set_env_t(const std::string &name, const std::string &value)
Temporarily set an environment variable.
Definition: env_utils.cpp:64
bcache::scoped_unset_env_t::scoped_unset_env_t
scoped_unset_env_t(const std::string &name)
Temporarily clear an environment variable.
Definition: env_utils.cpp:77
bcache::get_env
std::string get_env(const std::string &env_var)
Get the named environment variable for this process.
Definition: env_utils.cpp:97
bcache::scoped_unset_env_t::~scoped_unset_env_t
~scoped_unset_env_t()
Restore the environment variable to its old value.
Definition: env_utils.cpp:82
bcache::scoped_set_env_t
A class for temporarily modifying an environment variable.
Definition: env_utils.hpp:55
bcache::scoped_unset_env_t
A class for temporarily clearing an environment variable.
Definition: env_utils.hpp:71
bcache::env_var_t
A helper class for reading and parsing environment variables.
Definition: env_utils.hpp:27
bcache::env_var_t::as_int64
int64_t as_int64() const
Definition: env_utils.cpp:54
bcache::scoped_set_env_t::~scoped_set_env_t
~scoped_set_env_t()
Restore the environment variable to its old value.
Definition: env_utils.cpp:69