31 #include <boost/format.hpp>
64 checkCompatibility(other);
73 checkCompatibility(other);
75 m_env = move(other.m_env);
90 m_env.get().
set(m_index, arg_value);
97 m_env.get().
unSet(m_index);
104 m_env.get().
append(m_index, arg_value);
117 m_env.get().
prepend(m_index, arg_value);
143 return m_env.get().get(m_index,
"");
152 return value().empty();
156 return m_env.get().hasKey(m_index);
161 if (m_index != other.
m_index) {
163 error_buffer <<
"The \"" << other.
m_index <<
"\" environment variable"
164 <<
" cannot be copied to the \"" << m_index <<
"\" environment variable."
186 setEnv(v.first, v.second);
250 const string new_value =
get(index) + value;
252 set(index, new_value);
259 const string new_value = value +
get(index);
261 set(index, new_value);
267 string value {default_value};
296 map<ShellType, string> set_cmd {{ShellType::sh,
"export %s=%s"},
297 {ShellType::csh,
"setenv %s %s"}};
298 map<ShellType, string> unset_cmd {{ShellType::sh,
"unset %s"},
299 {ShellType::csh,
"unsetenv %s"}};
303 script_text << format(set_cmd[type]) % v.first %
get(v.first) << endl;
305 script_text << format(unset_cmd[type]) % v.first << endl;
310 script_text << format(set_cmd[type]) % v %
get(v) << endl;
313 return script_text.str();
322 error_buffer <<
"The environment doesn't contain the " << index <<
" variable." << endl;
Defines a class to handle the Environment.
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
proxy class to overload the assignment
Variable & operator=(const Variable &other)
Variable & set(const std::string &)
std::string value() const
Variable & operator+=(const std::string &)
std::reference_wrapper< Environment > m_env
a copiable and movable reference
Variable & prepend(const std::string &)
std::string m_index
The Name of the variable.
Variable & append(const std::string &)
void checkCompatibility(const Variable &)
Variable operator+(const std::string &)
const std::string & index() const
Environment & env() const
std::vector< std::string > m_added_variables
variable added to the environment
std::string get(const std::string &index, const std::string &default_value="") const
Variable operator[](const std::string &)
Environment(bool keep_same=true)
std::map< std::string, std::string > m_old_values
old value for changed variables
static bool hasKey(const std::string &)
Environment & set(const std::string &, const std::string &)
Environment & append(const std::string &, const std::string &)
Environment & unSet(const std::string &)
Environment & prepend(const std::string &, const std::string &)
std::string generateScript(ShellType) const
T emplace_back(T... args)
static void checkOutOfRange(const std::string &)
check that the variable is in the environment
ELEMENTS_API bool isEnvSet(const std::string &var)
Check if an environment variable is set or not.
ELEMENTS_API int setEnv(const std::string &name, const std::string &value, bool overwrite=true)
set an environment variables.
ELEMENTS_API int unSetEnv(const std::string &name)
Simple wrap around unsetenv for strings.
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
ELEMENTS_API Environment::Variable operator+(const std::string &, const Environment::Variable &)
ELEMENTS_API std::ostream & operator<<(std::ostream &, const Environment::Variable &)