Hold key,value dictionaries for environment variables. More...
#include "environment-variable.h"
Classes | |
class | Dictionary |
Key, value dictionary for a single environment variable. More... | |
Public Types | |
using | KeyFoundType = std::pair<bool, std::string> |
Result of a key lookup. | |
Public Member Functions | |
Singleton | |
This class is a singleton, accessed by static member functions, so the Rule of Five functions are all deleted. | |
EnvironmentVariable ()=delete | |
EnvironmentVariable (const EnvironmentVariable &)=delete | |
EnvironmentVariable & | operator= (const EnvironmentVariable &)=delete |
EnvironmentVariable (EnvironmentVariable &&)=delete | |
EnvironmentVariable & | operator= (EnvironmentVariable &&)=delete |
Static Public Member Functions | |
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 std::shared_ptr< Dictionary > | GetDictionary (const std::string &envvar, const std::string &delim=";") |
Get the dictionary for a particular environment variable. | |
static bool | Set (const std::string &variable, const std::string &value) |
Set an environment variable. | |
static bool | Unset (const std::string &variable) |
Unset an environment variable. | |
Private Types | |
using | DictionaryList = std::unordered_map<std::string, std::shared_ptr<Dictionary>> |
How Dictionaries are stored. | |
Static Private Member Functions | |
static void | Clear () |
Clear the instance, forcing all new lookups. | |
static DictionaryList & | Instance () |
Access the DictionaryStore instance. | |
Friends | |
class | tests::EnvVarTestCase |
Hold key,value dictionaries for environment variables.
The environment variable can have multiple key,value pairs separated by a delimiter, which is ";" by default.
Individual pairs are connected by '='. As an extension a bare key will be assigned the empty string ""
.
For example, ENVVAR="key1=value1;key2;key3=value3"
.
Definition at line 48 of file environment-variable.h.
|
private |
How Dictionaries are stored.
key:
the environment variable name
Dictionary:
the parsed Dictionary for the key
Definition at line 203 of file environment-variable.h.
using ns3::EnvironmentVariable::KeyFoundType = std::pair<bool, std::string> |
Result of a key lookup.
The first
is true
if the key was found.
The second
contains the value associated with the key.
Definition at line 57 of file environment-variable.h.
|
delete |
|
delete |
|
delete |
|
staticprivate |
Clear the instance, forcing all new lookups.
Definition at line 132 of file environment-variable.cc.
References Instance().
Referenced by ns3::tests::EnvVarTestCase::SetVariable(), and ns3::tests::EnvVarTestCase::UnsetVariable().
|
static |
Get the value corresponding to a key from an environment variable.
If the key
is empty just return the environment variable, (or {false, ""}
if the variable doesn't exist).
If the key
is not empty return the associated value (or {false, ""}
if the key is not found). If the key is present but has no value return {true, ""}
.
Key-value pairs are separated by delim
. Individual keys and values are separated by an =
sign. If the =
is not present the value returned is the empty string.
Notice that two cases both return {false, ""}
:
key
wasn't found.Notice that two cases both return {true, ""}
: the environment variable exists and
key
was empty and the environment variable was empty, orIn practice neither of these ambiguities is important:
false
the key doesn't exist.true
but the string is empty either the (not empty) key exists with no value, or the key was empty and the environment variable itself is empty.[in] | envvar | The environment variable. |
[in] | key | The key to extract from the environment variable. |
[in] | delim | The delimiter between key,value pairs. |
Definition at line 161 of file environment-variable.cc.
References GetDictionary().
Referenced by ns3::PrintList::PrintList(), ns3::tests::EnvVarTestCase::CheckGet(), ns3::ObjectBase::ConstructSelf(), ns3::ExampleAsTestCase::DoRun(), ns3::tests::EnvVarTestCase::DoRun(), ns3::LogComponent::EnvVarCheck(), ns3::GlobalValue::InitializeFromEnv(), ns3::SystemPath::MakeTemporaryDirectoryName(), and ns3::CommandLine::PrintDoxygenUsage().
|
static |
Get the dictionary for a particular environment variable.
This should be used when one needs to process all key,value pairs, perhaps without knowing the set of possible keys.
[in] | envvar | The environment variable. |
[in] | delim | The delimiter between key,value pairs. |
Definition at line 139 of file environment-variable.cc.
References Instance(), and NS_LOCAL_LOG.
Referenced by ns3::tests::EnvVarTestCase::Check(), ns3::CheckEnvironmentVariables(), and Get().
|
staticprivate |
Access the DictionaryStore instance.
Definition at line 124 of file environment-variable.cc.
Referenced by Clear(), and GetDictionary().
|
delete |
|
delete |
|
static |
Set an environment variable.
To set a variable to the empty string use Set(variable, "")
. Note: empty environment variables are not portable (unsupported on Windows).
[in] | variable | The environment variable to set. Note this may not contain the = character. |
[in] | value | The value to set. Note this must not be an empty string on Windows. |
true
if the variable was set successfully Definition at line 171 of file environment-variable.cc.
Referenced by ns3::tests::EnvVarTestCase::SetVariable().
|
static |
Unset an environment variable.
This removes the variable from the environment. To set a variable to the empty string use Set(variable, "")
.
[in] | variable | The environment variable to unset. Note this may not contain the = character. |
true
if the variable was unset successfully. Definition at line 179 of file environment-variable.cc.
Referenced by ns3::tests::EnvVarTestCase::UnsetVariable().
|
friend |
Definition at line 212 of file environment-variable.h.