glibmm 2.80.0
Public Member Functions | List of all members
Glib::Environ Class Reference

A convenience class for manipulating a copy of the environment variables. More...

#include <glibmm/environ.h>

Public Member Functions

 Environ ()
 Constructs a list of environment variables for the current process.
 
 Environ (const std::vector< std::string > &env_vec)
 Constructs a Glib::Environ instance from a vector.
 
std::optional< std::stringget (StdStringView variable) const
 Gets the value of the environment variable variable.
 
std::optional< std::stringoperator[] (StdStringView variable) const
 Same as get().
 
void set (StdStringView variable, StdStringView value, bool overwrite=true)
 Sets the environment variable variable in the provided list to value.
 
void unset (StdStringView variable)
 Removes the environment variable variable from the provided list.
 
std::vector< std::stringto_vector () const
 Get a vector with the environment variables.
 

Detailed Description

A convenience class for manipulating a copy of the environment variables.

Useful for generating the envp parameter in calls to Glib::spawn_async_with_pipes(), Glib::spawn_async() and Glib::spawn_sync().

If you want to change the environment itself (i.e. not a copy of it), see Glib::getenv(), Glib::setenv() and Glib::unsetenv().

Since glibmm 2.70:

Constructor & Destructor Documentation

◆ Environ() [1/2]

Glib::Environ::Environ ( )

Constructs a list of environment variables for the current process.

Each item in the list is of the form 'NAME=VALUE'.

◆ Environ() [2/2]

Glib::Environ::Environ ( const std::vector< std::string > &  env_vec)
explicit

Constructs a Glib::Environ instance from a vector.

Parameters
env_vecA vector with the environment variables. Each element in the vector must be of the form 'NAME=VALUE'.

Member Function Documentation

◆ get()

std::optional< std::string > Glib::Environ::get ( StdStringView  variable) const

Gets the value of the environment variable variable.

Parameters
variableThe environment variable to get, must not contain '='.
Returns
The value of the environment variable, or an empty std::optional if the environment variable is not set in this Environ.

◆ operator[]()

std::optional< std::string > Glib::Environ::operator[] ( StdStringView  variable) const
inline

Same as get().

◆ set()

void Glib::Environ::set ( StdStringView  variable,
StdStringView  value,
bool  overwrite = true 
)

Sets the environment variable variable in the provided list to value.

Parameters
variableThe environment variable to set, must not contain '='.
valueThe value to set the variable to.
overwriteWhether to change the variable if it already exists.

◆ to_vector()

std::vector< std::string > Glib::Environ::to_vector ( ) const

Get a vector with the environment variables.

Returns
A vector with the environment variables. Each element in the vector is of the form 'NAME=VALUE'.

◆ unset()

void Glib::Environ::unset ( StdStringView  variable)

Removes the environment variable variable from the provided list.

Parameters
variableThe environment variable to remove, must not contain '='.