carb::extras::EnvironmentVariable

Defined in carb/extras/EnvironmentVariable.h

class EnvironmentVariable

Defines an environment variable class that allows one to get, set, and restore value on destruction.

Public Functions

EnvironmentVariable() = delete
inline EnvironmentVariable(std::string name)

Create instance from environment variable called name.

Parameters

name – name of the environment variable

inline EnvironmentVariable(std::string name, const optional<const std::string> &value)

Create instance from environment variable called name, setting it’s value to value, to be restored to it’s original value on destruction.

Parameters
  • name – name of the environment variable

  • value – optional value to set variable, if not set (nullopt) then unset the variable

inline ~EnvironmentVariable()
inline EnvironmentVariable(EnvironmentVariable &&other)

move constructor

inline EnvironmentVariable &operator=(EnvironmentVariable &&other)

move operator

inline const std::string &getName() const noexcept

Get the environment variable name.

Returns

environment variable name

inline optional<const std::string> getValue() const noexcept

Get the environment variable current value.

Returns

environment variable current value

Public Static Functions

static inline bool setValue(const char *name, const char *value)

Sets new environment value for a variable.

Warning

(Linux) While calls like setenv() and unsetenv() are thread-safe with respect to all calls that modify the environment, there is no thread-safe way to read the environment with getenv() if any thread is modifying the environment. Unfortunately many GLIBC functions read the environment through getenv(). Therefore avoid modifying the enviornment!.

Parameters
  • name – Environment variable string that we want to get the value for.

  • value – The value of environment variable to get (MAX 256 characters). Can be nullptr - which means the variable should be unset.

Returns

true if the operation was successful.

static inline bool getValue(const char *name, std::string &value)

Static helper to get the value of the current environment variable.

Parameters
  • name – Environment variable string that we want to get the value for.

  • value – The value of environment variable to get.

Returns

true if the variable exists