A Discrete-Event Network Simulator
API
global-value.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "global-value.h"
21 #include "fatal-error.h"
22 #include "attribute.h"
23 #include "string.h"
24 #include "uinteger.h"
25 #include "log.h"
26 
27 #include "ns3/core-config.h"
28 
29 #include <cstdlib> // getenv
30 #include <cstring> // strlen
31 
32 
39 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("GlobalValue");
42 
43 GlobalValue::GlobalValue (std::string name, std::string help,
44  const AttributeValue &initialValue,
46  : m_name (name),
47  m_help (help),
48  m_initialValue (0),
49  m_currentValue (0),
50  m_checker (checker)
51 {
52  NS_LOG_FUNCTION (name << help << &initialValue << checker);
53  if (m_checker == 0)
54  {
55  NS_FATAL_ERROR ("Checker should not be zero on " << name );
56  }
57  m_initialValue = m_checker->CreateValidValue (initialValue);
59  if (m_initialValue == 0)
60  {
61  NS_FATAL_ERROR ("Value set by user on " << name << " is invalid.");
62  }
63  GetVector ()->push_back (this);
65 }
66 
67 void
69 {
70  NS_LOG_FUNCTION (this);
71 
72  const char *envVar = getenv ("NS_GLOBAL_VALUE");
73  if (envVar == 0 || std::strlen (envVar) == 0)
74  {
75  return;
76  }
77  std::string env = envVar;
78  std::string::size_type cur = 0;
79  std::string::size_type next = 0;
80  while (next != std::string::npos)
81  {
82  next = env.find (";", cur);
83  std::string tmp = std::string (env, cur, next - cur);
84  std::string::size_type equal = tmp.find ("=");
85  if (equal != std::string::npos)
86  {
87  std::string name = tmp.substr (0, equal);
88  std::string value = tmp.substr (equal + 1, tmp.size () - equal - 1);
89  if (name == m_name)
90  {
91  Ptr<AttributeValue> v = m_checker->CreateValidValue (StringValue (value));
92  if (v != 0)
93  {
94  m_initialValue = v;
95  m_currentValue = v;
96  }
97  return;
98  }
99  }
100  cur = next + 1;
101  }
102 }
103 
104 std::string
106 {
108  return m_name;
109 }
110 std::string
112 {
114  return m_help;
115 }
116 void
118 {
119  NS_LOG_FUNCTION (&value);
120  bool ok = m_checker->Copy (*m_currentValue, value);
121  if (ok)
122  {
123  return;
124  }
125  StringValue *str = dynamic_cast<StringValue *> (&value);
126  if (str == 0)
127  {
128  NS_FATAL_ERROR ("GlobalValue name=" << m_name << ": input value is not a string");
129  }
130  str->Set (m_currentValue->SerializeToString (m_checker));
131 }
134 {
135  NS_LOG_FUNCTION (this);
136 
137  return m_checker;
138 }
139 
140 bool
142 {
143  NS_LOG_FUNCTION (&value);
144 
145  Ptr<AttributeValue> v = m_checker->CreateValidValue (value);
146  if (v == 0)
147  {
148  return 0;
149  }
150  m_currentValue = v;
151  return true;
152 }
153 
154 void
155 GlobalValue::Bind (std::string name, const AttributeValue &value)
156 {
157  NS_LOG_FUNCTION (name << &value);
158 
159  for (Iterator i = Begin (); i != End (); i++)
160  {
161  if ((*i)->GetName () == name)
162  {
163  if (!(*i)->SetValue (value))
164  {
165  NS_FATAL_ERROR ("Invalid new value for global value: " << name);
166  }
167  return;
168  }
169  }
170  NS_FATAL_ERROR ("Non-existant global value: " << name);
171 }
172 bool
173 GlobalValue::BindFailSafe (std::string name, const AttributeValue &value)
174 {
175  NS_LOG_FUNCTION (name << &value);
176 
177  for (Iterator i = Begin (); i != End (); i++)
178  {
179  if ((*i)->GetName () == name)
180  {
181  return (*i)->SetValue (value);
182  }
183  }
184  return false;
185 }
188 {
190 
191  return GetVector ()->begin ();
192 }
195 {
197  return GetVector ()->end ();
198 }
199 
200 void
202 {
203  NS_LOG_FUNCTION (this);
205 }
206 
207 bool
209 {
210  NS_LOG_FUNCTION (name << &value);
211  for (GlobalValue::Iterator gvit = GlobalValue::Begin (); gvit != GlobalValue::End (); ++gvit)
212  {
213  if ((*gvit)->GetName () == name)
214  {
215  (*gvit)->GetValue (value);
216  return true;
217  }
218  }
219  return false; // not found
220 }
221 
222 void
223 GlobalValue::GetValueByName (std::string name, AttributeValue &value)
224 {
225  NS_LOG_FUNCTION (name << &value);
226  if (!GetValueByNameFailSafe (name, value))
227  {
228  NS_FATAL_ERROR ("Could not find GlobalValue named \"" << name << "\"");
229  }
230 }
231 
234 {
236  static Vector vector;
237  return &vector;
238 }
239 
240 } // namespace ns3
241 
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition: attribute.h:69
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:80
static Iterator End(void)
The End iterator.
void InitializeFromEnv(void)
Initialize from the NS_GLOBAL_VALUE environment variable.
Definition: global-value.cc:68
Ptr< const AttributeChecker > GetChecker(void) const
Get the AttributeChecker.
std::string GetHelp(void) const
Get the help string.
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.
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
Definition: global-value.h:76
static Vector * GetVector(void)
Get the static vector of all GlobalValues.
void ResetInitialValue(void)
Reset to the initial value.
std::string m_help
The help string.
Definition: global-value.h:200
Ptr< const AttributeChecker > m_checker
The AttributeChecker for this GlobalValue.
Definition: global-value.h:206
Ptr< AttributeValue > m_currentValue
The current value.
Definition: global-value.h:204
std::string GetName(void) const
Get the name.
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.
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...
static Iterator Begin(void)
The Begin iterator.
Ptr< AttributeValue > m_initialValue
The initial value.
Definition: global-value.h:202
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:198
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Hold variables of type string.
Definition: string.h:41
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:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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.