A Discrete-Event Network Simulator
API
global-value.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
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 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "global-value.h"
20
21#include "attribute.h"
22#include "fatal-error.h"
23#include "log.h"
24#include "string.h"
25#include "uinteger.h"
26
27#include "ns3/core-config.h"
28
29#include <cstdlib> // getenv
30#include <cstring> // strlen
31
38namespace ns3
39{
40
41NS_LOG_COMPONENT_DEFINE("GlobalValue");
42
43GlobalValue::GlobalValue(std::string name,
44 std::string help,
45 const AttributeValue& initialValue,
47 : m_name(name),
48 m_help(help),
49 m_initialValue(nullptr),
50 m_currentValue(nullptr),
51 m_checker(checker)
52{
53 NS_LOG_FUNCTION(name << help << &initialValue << checker);
54 if (!m_checker)
55 {
56 NS_FATAL_ERROR("Checker should not be zero on " << name);
57 }
58 m_initialValue = m_checker->CreateValidValue(initialValue);
60 if (!m_initialValue)
61 {
62 NS_FATAL_ERROR("Value set by user on " << name << " is invalid.");
63 }
64 GetVector()->push_back(this);
66}
67
68void
70{
71 NS_LOG_FUNCTION(this);
72
73 const char* envVar = getenv("NS_GLOBAL_VALUE");
74 if (envVar == nullptr || std::strlen(envVar) == 0)
75 {
76 return;
77 }
78 std::string env = envVar;
79 std::string::size_type cur = 0;
80 std::string::size_type next = 0;
81 while (next != std::string::npos)
82 {
83 next = env.find(';', cur);
84 std::string tmp = std::string(env, cur, next - cur);
85 std::string::size_type equal = tmp.find('=');
86 if (equal != std::string::npos)
87 {
88 std::string name = tmp.substr(0, equal);
89 std::string value = tmp.substr(equal + 1, tmp.size() - equal - 1);
90 if (name == m_name)
91 {
92 Ptr<AttributeValue> v = m_checker->CreateValidValue(StringValue(value));
93 if (v)
94 {
97 }
98 return;
99 }
100 }
101 cur = next + 1;
102 }
103}
104
105std::string
107{
109 return m_name;
110}
111
112std::string
114{
116 return m_help;
117}
118
119void
121{
123 bool ok = m_checker->Copy(*m_currentValue, value);
124 if (ok)
125 {
126 return;
127 }
128 StringValue* str = dynamic_cast<StringValue*>(&value);
129 if (str == nullptr)
130 {
131 NS_FATAL_ERROR("GlobalValue name=" << m_name << ": input value is not a string");
132 }
133 str->Set(m_currentValue->SerializeToString(m_checker));
134}
135
138{
139 NS_LOG_FUNCTION(this);
140
141 return m_checker;
142}
143
144bool
146{
148
149 Ptr<AttributeValue> v = m_checker->CreateValidValue(value);
150 if (!v)
151 {
152 return 0;
153 }
154 m_currentValue = v;
155 return true;
156}
157
158void
159GlobalValue::Bind(std::string name, const AttributeValue& value)
160{
161 NS_LOG_FUNCTION(name << &value);
162
163 for (Iterator i = Begin(); i != End(); i++)
164 {
165 if ((*i)->GetName() == name)
166 {
167 if (!(*i)->SetValue(value))
168 {
169 NS_FATAL_ERROR("Invalid new value for global value: " << name);
170 }
171 return;
172 }
173 }
174 NS_FATAL_ERROR("Non-existant global value: " << name);
175}
176
177bool
179{
180 NS_LOG_FUNCTION(name << &value);
181
182 for (Iterator i = Begin(); i != End(); i++)
183 {
184 if ((*i)->GetName() == name)
185 {
186 return (*i)->SetValue(value);
187 }
188 }
189 return false;
190}
191
194{
196
197 return GetVector()->begin();
198}
199
202{
204 return GetVector()->end();
205}
206
207void
209{
210 NS_LOG_FUNCTION(this);
212}
213
214bool
216{
217 NS_LOG_FUNCTION(name << &value);
218 for (GlobalValue::Iterator gvit = GlobalValue::Begin(); gvit != GlobalValue::End(); ++gvit)
219 {
220 if ((*gvit)->GetName() == name)
221 {
222 (*gvit)->GetValue(value);
223 return true;
224 }
225 }
226 return false; // not found
227}
228
229void
231{
232 NS_LOG_FUNCTION(name << &value);
233 if (!GetValueByNameFailSafe(name, value))
234 {
235 NS_FATAL_ERROR("Could not find GlobalValue named \"" << name << "\"");
236 }
237}
238
241{
243 static Vector vector;
244 return &vector;
245}
246
247} // namespace ns3
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition: attribute.h:70
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:82
std::string GetHelp() const
Get the help string.
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
Definition: global-value.h:78
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.
Definition: global-value.h:202
Ptr< const AttributeChecker > m_checker
The AttributeChecker for this GlobalValue.
Definition: global-value.h:208
Ptr< AttributeValue > m_currentValue
The current value.
Definition: global-value.h:206
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.
Definition: global-value.h:204
static Iterator End()
The End iterator.
GlobalValue(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeChecker > checker)
Constructor.
Definition: global-value.cc:43
std::string m_name
The name of this GlobalValue.
Definition: global-value.h:200
void InitializeFromEnv()
Initialize from the NS_GLOBAL_VALUE environment variable.
Definition: global-value.cc:69
static Vector * GetVector()
Get the static vector of all GlobalValues.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Hold variables of type string.
Definition: string.h:42
void Set(const std::string &value)
Set the value.
Definition: string.cc:31
NS_FATAL_x macro definitions.
ns3::GlobalValue declaration.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:160
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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.
value
Definition: second.py:41
ns3::StringValue attribute value declarations.
ns3::UintegerValue attribute value declarations and template implementations.