A Discrete-Event Network Simulator
API
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 
29 #include <memory> // shared_ptr
30 #include <string>
31 #include <unordered_map>
32 #include <utility> // pair
33 
34 namespace ns3
35 {
36 
40 
41 // Forward declaration
42 namespace tests
43 {
44 class EnvVarTestCase;
45 }
46 
60 {
61  public:
68  using KeyFoundType = std::pair<bool, std::string>;
69 
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 
121  static std::shared_ptr<Dictionary> GetDictionary(const std::string& envvar,
122  const std::string& delim = ";");
123 
126  {
127  public:
142  Dictionary(const std::string& envvar, const std::string& delim = ";");
143 
150  KeyFoundType Get(const std::string& key = "") const;
151 
153  using KeyValueStore = std::unordered_map<std::string, std::string>;
154 
158  KeyValueStore GetStore() const;
159 
160  private:
162  bool m_exists;
164  std::string m_variable;
167 
168  }; // class Dictionary
169 
180  static bool Set(const std::string& variable, const std::string& value);
181 
190  static bool Unset(const std::string& variable);
191 
206  private:
214  using DictionaryList = std::unordered_map<std::string, std::shared_ptr<Dictionary>>;
215 
220  static DictionaryList& Instance();
221 
222  // Test needs to clear the instance
223  friend class tests::EnvVarTestCase;
224 
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.
Dictionary(const std::string &envvar, const std::string &delim=";")
Constructor.
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.
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.
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.
EnvironmentVariable & operator=(const EnvironmentVariable &)=delete
static void Clear()
Clear the instance, forcing all new lookups.
EnvironmentVariable & operator=(EnvironmentVariable &&)=delete
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.