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 "environment-variable.h"
23 #include "fatal-error.h"
24 #include "log.h"
25 #include "string.h"
26 #include "uinteger.h"
27 
28 #include "ns3/core-config.h"
29 
36 namespace ns3
37 {
38 
39 NS_LOG_COMPONENT_DEFINE("GlobalValue");
40 
41 GlobalValue::GlobalValue(std::string name,
42  std::string help,
43  const AttributeValue& initialValue,
45  : m_name(name),
46  m_help(help),
47  m_initialValue(nullptr),
48  m_currentValue(nullptr),
49  m_checker(checker)
50 {
51  NS_LOG_FUNCTION(name << help << &initialValue << checker);
52  if (!m_checker)
53  {
54  NS_FATAL_ERROR("Checker should not be zero on " << name);
55  }
56  m_initialValue = m_checker->CreateValidValue(initialValue);
58  if (!m_initialValue)
59  {
60  NS_FATAL_ERROR("Value set by user on " << name << " is invalid.");
61  }
62  GetVector()->push_back(this);
64 }
65 
66 void
68 {
69  NS_LOG_FUNCTION(this);
70 
71  auto [found, value] = EnvironmentVariable::Get("NS_GLOBAL_VALUE", m_name);
72  if (found)
73  {
74  Ptr<AttributeValue> v = m_checker->CreateValidValue(StringValue(value));
75  if (v)
76  {
77  m_initialValue = v;
78  m_currentValue = v;
79  }
80  }
81 }
82 
83 std::string
85 {
87  return m_name;
88 }
89 
90 std::string
92 {
94  return m_help;
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION(&value);
101  bool ok = m_checker->Copy(*m_currentValue, value);
102  if (ok)
103  {
104  return;
105  }
106  auto str = dynamic_cast<StringValue*>(&value);
107  if (str == nullptr)
108  {
109  NS_FATAL_ERROR("GlobalValue name=" << m_name << ": input value is not a string");
110  }
111  str->Set(m_currentValue->SerializeToString(m_checker));
112 }
113 
116 {
117  NS_LOG_FUNCTION(this);
118 
119  return m_checker;
120 }
121 
122 bool
124 {
125  NS_LOG_FUNCTION(&value);
126 
127  Ptr<AttributeValue> v = m_checker->CreateValidValue(value);
128  if (!v)
129  {
130  return false;
131  }
132  m_currentValue = v;
133  return true;
134 }
135 
136 void
137 GlobalValue::Bind(std::string name, const AttributeValue& value)
138 {
139  NS_LOG_FUNCTION(name << &value);
140 
141  for (auto i = Begin(); i != End(); i++)
142  {
143  if ((*i)->GetName() == name)
144  {
145  if (!(*i)->SetValue(value))
146  {
147  NS_FATAL_ERROR("Invalid new value for global value: " << name);
148  }
149  return;
150  }
151  }
152  NS_FATAL_ERROR("Non-existent global value: " << name);
153 }
154 
155 bool
156 GlobalValue::BindFailSafe(std::string name, const AttributeValue& value)
157 {
158  NS_LOG_FUNCTION(name << &value);
159 
160  for (auto i = Begin(); i != End(); i++)
161  {
162  if ((*i)->GetName() == name)
163  {
164  return (*i)->SetValue(value);
165  }
166  }
167  return false;
168 }
169 
172 {
174 
175  return GetVector()->begin();
176 }
177 
180 {
182  return GetVector()->end();
183 }
184 
185 void
187 {
188  NS_LOG_FUNCTION(this);
190 }
191 
192 bool
194 {
195  NS_LOG_FUNCTION(name << &value);
196  for (auto gvit = GlobalValue::Begin(); gvit != GlobalValue::End(); ++gvit)
197  {
198  if ((*gvit)->GetName() == name)
199  {
200  (*gvit)->GetValue(value);
201  return true;
202  }
203  }
204  return false; // not found
205 }
206 
207 void
209 {
210  NS_LOG_FUNCTION(name << &value);
211  if (!GetValueByNameFailSafe(name, value))
212  {
213  NS_FATAL_ERROR("Could not find GlobalValue named \"" << name << "\"");
214  }
215 }
216 
219 {
221  static Vector vector;
222  return &vector;
223 }
224 
225 } // namespace ns3
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition: attribute.h:70
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.
Definition: global-value.h:82
std::string GetHelp() const
Get the help string.
Definition: global-value.cc:91
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
Definition: global-value.h:78
void GetValue(AttributeValue &value) const
Get the value.
Definition: global-value.cc:98
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.
Definition: global-value.cc:84
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:41
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:67
static Vector * GetVector()
Get the static vector of all GlobalValues.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Hold variables of type string.
Definition: string.h:56
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.
Definition: fatal-error.h:179
#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.
ns3::StringValue attribute value declarations.
ns3::UintegerValue attribute value declarations and template implementations.