A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
environment-variable.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Lawrence Livermore National Laboratory
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 version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20#ifndef ENVIRONMENT_VARIABLE_H
21#define ENVIRONMENT_VARIABLE_H
22
23/**
24 * \file
25 * \ingroup core-environ
26 * Class Environment declaration.
27 */
28
29#include <memory> // shared_ptr
30#include <string>
31#include <unordered_map>
32#include <utility> // pair
33
34namespace ns3
35{
36
37/// \todo Reconsider the name?
38/// Rename to just EnvironmentVariableDictionary?
39/// System::EnvironmentVariable?
40
41// Forward declaration
42namespace tests
43{
44class EnvVarTestCase;
45}
46
47/**
48 * \ingroup core-environ
49 * Hold key,value dictionaries for environment variables.
50 *
51 * The environment variable can have multiple key,value pairs
52 * separated by a delimiter, which is ";" by default.
53 *
54 * Individual pairs are connected by '='. As an extension a bare key
55 * will be assigned the empty string \c "".
56 *
57 * For example, `ENVVAR="key1=value1;key2;key3=value3"`.
58 */
60{
61 public:
62 /**
63 * Result of a key lookup.
64 * The \p first is \c true if the key was found.
65 *
66 * The \p second contains the value associated with the key.
67 */
68 using KeyFoundType = std::pair<bool, std::string>;
69
70 /**
71 * Get the value corresponding to a key from an environment variable.
72 *
73 * If the \p key is empty just return the environment variable,
74 * (or `{false, ""}` if the variable doesn't exist).
75 *
76 * If the \p key is not empty return the associated value
77 * (or `{false, ""}` if the key is not found).
78 * If the key is present but has no value return `{true, ""}`.
79 *
80 * Key-value pairs are separated by \p delim. Individual keys and
81 * values are separated by an `=` sign. If the `=` is not present
82 * the value returned is the empty string.
83 *
84 * Notice that two cases both return `{false, ""}`:
85 * * The environment variable doesn't exist, or
86 * * It exists but the (non-empty) \p key wasn't found.
87 *
88 * Notice that two cases both return `{true, ""}`: the environment
89 * variable exists and
90 * * The \p key was empty and the environment variable was empty, or
91 * * The (not empty) key was found, but with no value.
92 *
93 * In practice neither of these ambiguities is important:
94 * * If the return contains \c false the key doesn't exist.
95 * * If the return contains \c true but the string is empty either the
96 * (not empty) key exists with no value, or the key was empty and the
97 * environment variable itself is empty.
98 *
99 * \param [in] envvar The environment variable.
100 * \param [in] key The key to extract from the environment variable.
101 * \param [in] delim The delimiter between key,value pairs.
102 * \returns Whether the key was found, and its value, as explained above.
103 */
104 static KeyFoundType Get(const std::string& envvar,
105 const std::string& key = "",
106 const std::string& delim = ";");
107
108 // Forward
109 class Dictionary;
110
111 /**
112 * Get the dictionary for a particular environment variable.
113 *
114 * This should be used when one needs to process all key,value
115 * pairs, perhaps without knowing the set of possible keys.
116 *
117 * \param [in] envvar The environment variable.
118 * \param [in] delim The delimiter between key,value pairs.
119 * \returns The Dictionary.
120 */
121 static std::shared_ptr<Dictionary> GetDictionary(const std::string& envvar,
122 const std::string& delim = ";");
123
124 /** Key, value dictionary for a single environment variable. */
126 {
127 public:
128 /**
129 * Constructor.
130 *
131 * Parse an environment variable containing keys and optional values.
132 *
133 * Keys in the environment variable are separated by the
134 * \p delim character. Keys may be assigned values by following
135 * the key with the `=` character; any remaining text up to the next
136 * delimiter will be taken as the value. If no `=`
137 * is given the enpty string will be stored as the value.
138 *
139 * \param [in] envvar The environment variable.
140 * \param [in] delim The delimiter between key,value pairs.
141 */
142 Dictionary(const std::string& envvar, const std::string& delim = ";");
143
144 /**
145 * Get the value corresponding to a key from this dictionary.
146 * If the \p key is empty return the full environment variable value.
147 * \param [in] key The key to extract from the environment variable.
148 * \returns \c true if the key was found, and the associated value.
149 */
150 KeyFoundType Get(const std::string& key = "") const;
151
152 /** Key, value store type. */
153 using KeyValueStore = std::unordered_map<std::string, std::string>;
154
155 /** Get the underlying store, for iterating.
156 * \returns The key, value store.
157 */
158 KeyValueStore GetStore() const;
159
160 private:
161 /** Whether the environment variable exists in the environment. */
163 /** The raw environment variable. */
164 std::string m_variable;
165 /** The key, value store. */
167
168 }; // class Dictionary
169
170 /**
171 * Set an environment variable.
172 *
173 * To set a variable to the empty string use `Set(variable, "")`.
174 * Note: empty environment variables are not portable (unsupported on Windows).
175 *
176 * \param [in] variable The environment variable to set. Note this may not contain the `=`
177 * character. \param [in] value The value to set. Note this must not be an empty string on
178 * Windows. \returns \c true if the variable was set successfully
179 */
180 static bool Set(const std::string& variable, const std::string& value);
181
182 /**
183 * Unset an environment variable.
184 * This removes the variable from the environment.
185 * To set a variable to the empty string use `Set(variable, "")`.
186 *
187 * \param [in] variable The environment variable to unset. Note this may not contain the `=`
188 * character. \returns \c true if the variable was unset successfully.
189 */
190 static bool Unset(const std::string& variable);
191
192 /**
193 * \name Singleton
194 *
195 * This class is a singleton, accessed by static member functions,
196 * so the Rule of Five functions are all deleted.
197 */
198 /** @{ */
204 /** @} */
205
206 private:
207 /**
208 * How Dictionaries are stored.
209 *
210 * \p key: the environment variable name
211 *
212 * \p Dictionary: the parsed Dictionary for the \p key
213 */
214 using DictionaryList = std::unordered_map<std::string, std::shared_ptr<Dictionary>>;
215
216 /**
217 * Access the DictionaryStore instance.
218 * \returns the DictionaryStore.
219 */
220 static DictionaryList& Instance();
221
222 // Test needs to clear the instance
224
225 /** Clear the instance, forcing all new lookups. */
226 static void Clear();
227
228}; // class EnvironmentVariable
229
230} // namespace ns3
231
232#endif /* ENVIRONMENT_VARIABLE_H */
Key, value dictionary for a single environment variable.
KeyFoundType Get(const std::string &key="") const
Get the value corresponding to a key from this dictionary.
std::unordered_map< std::string, std::string > KeyValueStore
Key, value store type.
std::string m_variable
The raw environment variable.
bool m_exists
Whether the environment variable exists in the environment.
KeyValueStore m_dict
The key, value store.
KeyValueStore GetStore() const
Get the underlying store, for iterating.
Hold key,value dictionaries for environment variables.
EnvironmentVariable & operator=(const EnvironmentVariable &)=delete
static DictionaryList & Instance()
Access the DictionaryStore instance.
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
EnvironmentVariable & operator=(EnvironmentVariable &&)=delete
static bool Unset(const std::string &variable)
Unset an environment variable.
std::unordered_map< std::string, std::shared_ptr< Dictionary > > DictionaryList
How Dictionaries are stored.
std::pair< bool, std::string > KeyFoundType
Result of a key lookup.
static void Clear()
Clear the instance, forcing all new lookups.
static bool Set(const std::string &variable, const std::string &value)
Set an environment variable.
EnvironmentVariable(EnvironmentVariable &&)=delete
static std::shared_ptr< Dictionary > GetDictionary(const std::string &envvar, const std::string &delim=";")
Get the dictionary for a particular environment variable.
EnvironmentVariable(const EnvironmentVariable &)=delete
Every class exported by the ns3 library is enclosed in the ns3 namespace.