45 m_map(std::move(other.m_map)) ,
46 m_list(std::move(other.m_list)) ,
47 m_pointers(std::move(other.m_pointers)) ,
48 m_block(std::move(other.m_block))
52void G::Environment::swap(
Environment & other )
noexcept
54 m_map.swap( other.m_map ) ;
55 m_list.swap( other.m_list ) ;
56 m_pointers.swap( other.m_pointers ) ;
57 std::swap( m_block , other.m_block ) ;
63 m_map.size() == m_list.size() &&
64 (m_list.size()+1U) == m_pointers.size() &&
65 ( m_list.empty() || m_pointers.at(0U) == m_list.at(0U).c_str() ) ;
80void G::Environment::setup()
87void G::Environment::setList()
90 m_list.reserve( m_map.size() ) ;
92 std::sort( keys.begin() , keys.end() ) ;
93 for(
const auto & key : keys )
95 m_list.push_back( key +
"=" + (*m_map.find(key)).second ) ;
99void G::Environment::setPointers()
102 m_pointers.reserve( m_list.size() + 1U ) ;
103 for(
const auto & s : m_list )
104 m_pointers.push_back(
const_cast<char*
>(s.c_str()) ) ;
105 m_pointers.push_back(
nullptr ) ;
108void G::Environment::setBlock()
111 for(
auto & s : m_list )
113 m_block.reserve( n + 1U ) ;
114 for(
auto & s : m_list )
116 m_block.append( s ) ;
117 m_block.append( 1U ,
'\0' ) ;
119 m_block.append( 1U ,
'\0' ) ;
124 if( name.find(
'=') != std::string::npos )
throw std::runtime_error(
"invalid environment variable [" + name +
"]" ) ;
125 m_map.insert( std::make_pair(name,value) ) ;
131 m_map[name] = value ;
137 return const_cast<char**
>(&m_pointers[0]) ;
142 return m_block.data() ;
147 return m_map.find(name) != m_map.end() ;
152 return contains(name) ? (*m_map.find(name)).second : default_ ;
Holds a set of environment variables and also provides static methods to wrap getenv() and putenv().
bool valid() const
Returns true if the class invariants are satisfied.
Environment(const std::map< std::string, std::string > &)
Constructor from a map.
std::string value(const std::string &name, const std::string &default_=std::string()) const
Returns the value of the given variable in this set.
void add(const std::string &name, const std::string &value)
Adds a variable to this set.
const char * ptr() const noexcept
Returns a contiguous block of memory containing the null-terminated strings with an extra zero byte a...
void set(const std::string &name, const std::string &value)
Inserts or updates a variable in this set.
Environment & operator=(const Environment &)
Assigment operator.
bool contains(const std::string &name) const
Returns true if the given variable is in this set.
char ** v() const noexcept
Returns a null-terminated array of pointers.
static StringArray keys(const StringMap &string_map)
Extracts the keys from a map of strings.
std::vector< std::string > StringArray
A std::vector of std::strings.