A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
global-value.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "global-value.h"
9
10#include "attribute.h"
12#include "fatal-error.h"
13#include "log.h"
14#include "string.h"
15#include "uinteger.h"
16
17#include "ns3/core-config.h"
18
19/**
20 * \file
21 * \ingroup core
22 * ns3::GlobalValue implementation.
23 */
24
25namespace ns3
26{
27
28NS_LOG_COMPONENT_DEFINE("GlobalValue");
29
30GlobalValue::GlobalValue(std::string name,
31 std::string help,
32 const AttributeValue& initialValue,
34 : m_name(name),
35 m_help(help),
36 m_initialValue(nullptr),
37 m_currentValue(nullptr),
38 m_checker(checker)
39{
40 NS_LOG_FUNCTION(name << help << &initialValue << checker);
41 if (!m_checker)
42 {
43 NS_FATAL_ERROR("Checker should not be zero on " << name);
44 }
45 m_initialValue = m_checker->CreateValidValue(initialValue);
47 if (!m_initialValue)
48 {
49 NS_FATAL_ERROR("Value set by user on " << name << " is invalid.");
50 }
51 GetVector()->push_back(this);
53}
54
55void
57{
58 NS_LOG_FUNCTION(this);
59
60 auto [found, value] = EnvironmentVariable::Get("NS_GLOBAL_VALUE", m_name);
61 if (found)
62 {
63 Ptr<AttributeValue> v = m_checker->CreateValidValue(StringValue(value));
64 if (v)
65 {
68 }
69 }
70}
71
72std::string
74{
76 return m_name;
77}
78
79std::string
81{
83 return m_help;
84}
85
86void
88{
89 NS_LOG_FUNCTION(&value);
90 bool ok = m_checker->Copy(*m_currentValue, value);
91 if (ok)
92 {
93 return;
94 }
95 auto str = dynamic_cast<StringValue*>(&value);
96 if (str == nullptr)
97 {
98 NS_FATAL_ERROR("GlobalValue name=" << m_name << ": input value is not a string");
99 }
100 str->Set(m_currentValue->SerializeToString(m_checker));
101}
102
105{
106 NS_LOG_FUNCTION(this);
107
108 return m_checker;
109}
110
111bool
113{
114 NS_LOG_FUNCTION(&value);
115
116 Ptr<AttributeValue> v = m_checker->CreateValidValue(value);
117 if (!v)
118 {
119 return false;
120 }
121 m_currentValue = v;
122 return true;
123}
124
125void
126GlobalValue::Bind(std::string name, const AttributeValue& value)
127{
128 NS_LOG_FUNCTION(name << &value);
129
130 for (auto i = Begin(); i != End(); i++)
131 {
132 if ((*i)->GetName() == name)
133 {
134 if (!(*i)->SetValue(value))
135 {
136 NS_FATAL_ERROR("Invalid new value for global value: " << name);
137 }
138 return;
139 }
140 }
141 NS_FATAL_ERROR("Non-existent global value: " << name);
142}
143
144bool
145GlobalValue::BindFailSafe(std::string name, const AttributeValue& value)
146{
147 NS_LOG_FUNCTION(name << &value);
148
149 for (auto i = Begin(); i != End(); i++)
150 {
151 if ((*i)->GetName() == name)
152 {
153 return (*i)->SetValue(value);
154 }
155 }
156 return false;
157}
158
161{
163
164 return GetVector()->begin();
165}
166
169{
171 return GetVector()->end();
172}
173
174void
180
181bool
183{
184 NS_LOG_FUNCTION(name << &value);
185 for (auto gvit = GlobalValue::Begin(); gvit != GlobalValue::End(); ++gvit)
186 {
187 if ((*gvit)->GetName() == name)
188 {
189 (*gvit)->GetValue(value);
190 return true;
191 }
192 }
193 return false; // not found
194}
195
196void
198{
199 NS_LOG_FUNCTION(name << &value);
200 if (!GetValueByNameFailSafe(name, value))
201 {
202 NS_FATAL_ERROR("Could not find GlobalValue named \"" << name << "\"");
203 }
204}
205
208{
210 static Vector vector;
211 return &vector;
212}
213
214} // namespace ns3
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition attribute.h:59
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.
Vector::const_iterator Iterator
Iterator type for the list of all global values.
std::string GetHelp() const
Get the help string.
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
void GetValue(AttributeValue &value) const
Get the value.
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
static bool GetValueByNameFailSafe(std::string name, AttributeValue &value)
Finds the GlobalValue with the given name and returns its value.
static Iterator Begin()
The Begin iterator.
std::string GetName() const
Get the name.
std::string m_help
The help string.
Ptr< const AttributeChecker > m_checker
The AttributeChecker for this GlobalValue.
Ptr< AttributeValue > m_currentValue
The current value.
bool SetValue(const AttributeValue &value)
Set the value of this GlobalValue.
static void GetValueByName(std::string name, AttributeValue &value)
Finds the GlobalValue with the given name and returns its value.
void ResetInitialValue()
Reset to the initial value.
static bool BindFailSafe(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
Ptr< const AttributeChecker > GetChecker() const
Get the AttributeChecker.
Ptr< AttributeValue > m_initialValue
The initial value.
static Iterator End()
The End iterator.
GlobalValue(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeChecker > checker)
Constructor.
std::string m_name
The name of this GlobalValue.
void InitializeFromEnv()
Initialize from the NS_GLOBAL_VALUE environment variable.
static Vector * GetVector()
Get the static vector of all GlobalValues.
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
Definition string.h:45
Class Environment declaration.
NS_FATAL_x macro definitions.
ns3::GlobalValue declaration.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::StringValue attribute value declarations.
ns3::UintegerValue attribute value declarations and template implementations.