E-MailRelay
genvironment.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file genvironment.h
19///
20
21#ifndef G_ENVIRONMENT_H
22#define G_ENVIRONMENT_H
23
24#include "gdef.h"
25#include <string>
26#include <vector>
27#include <map>
28
29namespace G
30{
31 class Environment ;
32}
33
34//| \class G::Environment
35/// Holds a set of environment variables and also provides static methods
36/// to wrap getenv() and putenv().
37///
39{
40public:
41 static std::string get( const std::string & name , const std::string & default_ ) ;
42 ///< Returns the environment variable value or the given default.
43
44 static void put( const std::string & name , const std::string & value ) ;
45 ///< Sets the environment variable value.
46
47 static Environment minimal() ;
48 ///< Returns a minimal, safe set of environment variables.
49
50 static Environment inherit() ;
51 ///< Returns an empty() environment, as if default constructed.
52 ///< This is syntactic sugar for the G::NewProcess interface.
53
54 explicit Environment( const std::map<std::string,std::string> & ) ;
55 ///< Constructor from a map.
56
57 ~Environment() = default ;
58 ///< Destructor.
59
60 void add( const std::string & name , const std::string & value ) ;
61 ///< Adds a variable to this set. Does nothing if already
62 ///< present.
63
64 bool contains( const std::string & name ) const ;
65 ///< Returns true if the given variable is in this set.
66
67 std::string value( const std::string & name , const std::string & default_ = std::string() ) const ;
68 ///< Returns the value of the given variable in this set.
69
70 void set( const std::string & name , const std::string & value ) ;
71 ///< Inserts or updates a variable in this set.
72
73 const char * ptr() const noexcept ;
74 ///< Returns a contiguous block of memory containing the
75 ///< null-terminated strings with an extra zero byte
76 ///< at the end.
77
78 char ** v() const noexcept ;
79 ///< Returns a null-terminated array of pointers.
80
81 bool empty() const noexcept ;
82 ///< Returns true if empty.
83
84 Environment( const Environment & ) ;
85 ///< Copy constructor.
86
87 Environment( Environment && ) noexcept ;
88 ///< Move constructor.
89
90 Environment & operator=( const Environment & ) ;
91 ///< Assigment operator.
92
93 Environment & operator=( Environment && ) noexcept ;
94 ///< Move assigment operator.
95
96 bool valid() const ;
97 ///< Returns true if the class invariants are
98 ///< satisfied. Used in testing.
99
100private:
101 Environment() ;
102 void swap( Environment & ) noexcept ;
103
104private:
105 using Map = std::map<std::string,std::string> ;
106 using List = std::vector<std::string> ;
107 static char * stringdup( const std::string & ) ;
108 void setup() ;
109 void setList() ;
110 void setPointers() ;
111 void setBlock() ;
112
113private:
114 Map m_map ;
115 std::vector<std::string> m_list ;
116 std::vector<char*> m_pointers ;
117 std::string m_block ;
118} ;
119
120inline
121bool G::Environment::empty() const noexcept
122{
123 return m_map.empty() ;
124}
125
126inline
128{
129 return {} ;
130}
131
132#endif
Holds a set of environment variables and also provides static methods to wrap getenv() and putenv().
Definition: genvironment.h:39
bool valid() const
Returns true if the class invariants are satisfied.
static void put(const std::string &name, const std::string &value)
Sets the environment variable value.
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
~Environment()=default
Destructor.
bool empty() const noexcept
Returns true if empty.
Definition: genvironment.h:121
static Environment inherit()
Returns an empty() environment, as if default constructed.
Definition: genvironment.h:127
static Environment minimal()
Returns a minimal, safe set of environment variables.
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.
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.
Low-level classes.
Definition: galign.h:28