E-MailRelay
Public Member Functions | Static Public Member Functions | List of all members
G::Environment Class Reference

Holds a set of environment variables and also provides static methods to wrap getenv() and putenv(). More...

#include <genvironment.h>

Public Member Functions

 Environment (const std::map< std::string, std::string > &)
 Constructor from a map. More...
 
bool contains (const std::string &name) const
 Returns true if the given variable is in this set. More...
 
std::string value (const std::string &name, const std::string &default_={}) const
 Returns the value of the given variable in this set. More...
 
bool add (std::string_view key, std::string_view value)
 Adds an environment variable. Returns false if invalid. More...
 
std::string block () const
 Returns a contiguous block of memory containing the null-terminated strings with an extra zero byte at the end. More...
 
std::wstring block (std::wstring(*)(std::string_view)) const
 Returns a contiguous block of memory containing the null-terminated strings with an extra zero character at the end. More...
 
bool empty () const noexcept
 Returns true if empty. More...
 
std::map< std::string, std::string > map () const
 Returns the environment as a map. More...
 

Static Public Member Functions

static std::string get (const std::string &name, const std::string &default_)
 Returns the environment variable value or the given default. More...
 
static G::Path getPath (const std::string &name, const G::Path &={})
 Returns the environment variable value as a G::Path object. More...
 
static void put (const std::string &name, const std::string &value)
 Sets the environment variable value. More...
 
static Environment minimal (bool sbin=false)
 Returns a minimal, safe set of environment variables. More...
 
static Environment inherit ()
 Returns an empty() environment, as if default constructed. More...
 
static std::vector< char * > array (const std::string &block)
 Returns a pointer array pointing into the given block(), with const-casts. More...
 
static std::vector< char * > array (std::string &&envblock)=delete
 Deleted overload.
 

Detailed Description

Holds a set of environment variables and also provides static methods to wrap getenv() and putenv().

Definition at line 41 of file genvironment.h.

Constructor & Destructor Documentation

◆ Environment()

G::Environment::Environment ( const std::map< std::string, std::string > &  map)
explicit

Constructor from a map.

Definition at line 30 of file genvironment.cpp.

Member Function Documentation

◆ add()

bool G::Environment::add ( std::string_view  key,
std::string_view  value 
)

Adds an environment variable. Returns false if invalid.

Definition at line 48 of file genvironment.cpp.

◆ array()

std::vector< char * > G::Environment::array ( const std::string &  block)
static

Returns a pointer array pointing into the given block(), with const-casts.

Definition at line 100 of file genvironment.cpp.

◆ block() [1/2]

std::string G::Environment::block ( ) const

Returns a contiguous block of memory containing the null-terminated strings with an extra zero byte at the end.

Definition at line 74 of file genvironment.cpp.

◆ block() [2/2]

std::wstring G::Environment::block ( std::wstring(*)(std::string_view)  fn) const

Returns a contiguous block of memory containing the null-terminated strings with an extra zero character at the end.

Definition at line 87 of file genvironment.cpp.

◆ contains()

bool G::Environment::contains ( const std::string &  name) const

Returns true if the given variable is in this set.

Definition at line 62 of file genvironment.cpp.

◆ empty()

bool G::Environment::empty ( ) const
inlinenoexcept

Returns true if empty.

Definition at line 115 of file genvironment.h.

◆ get()

std::string G::Environment::get ( const std::string &  name,
const std::string &  default_ 
)
static

Returns the environment variable value or the given default.

Definition at line 35 of file genvironment_unix.cpp.

◆ getPath()

G::Path G::Environment::getPath ( const std::string &  name,
const G::Path default_ = {} 
)
static

Returns the environment variable value as a G::Path object.

Definition at line 42 of file genvironment_unix.cpp.

◆ inherit()

G::Environment G::Environment::inherit ( )
inlinestatic

Returns an empty() environment, as if default constructed.

This is syntactic sugar for the G::NewProcess interface.

Definition at line 121 of file genvironment.h.

◆ map()

std::map< std::string, std::string > G::Environment::map ( ) const
inline

Returns the environment as a map.

Definition at line 109 of file genvironment.h.

◆ minimal()

G::Environment G::Environment::minimal ( bool  sbin = false)
static

Returns a minimal, safe set of environment variables.

Definition at line 63 of file genvironment_unix.cpp.

◆ put()

void G::Environment::put ( const std::string &  name,
const std::string &  value 
)
static

Sets the environment variable value.

Definition at line 55 of file genvironment_unix.cpp.

◆ value()

std::string G::Environment::value ( const std::string &  name,
const std::string &  default_ = {} 
) const

Returns the value of the given variable in this set.

Definition at line 68 of file genvironment.cpp.


The documentation for this class was generated from the following files: