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 #ifdef HAVE_STDLIB_H
29 #include <cstdlib>
30 #endif
31 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("GlobalValue");
41 
42 GlobalValue::GlobalValue (std::string name, std::string help,
43  const AttributeValue &initialValue,
45  : m_name (name),
46  m_help (help),
47  m_initialValue (0),
48  m_currentValue (0),
49  m_checker (checker)
50 {
51  NS_LOG_FUNCTION (name << help << &initialValue << checker);
52  if (m_checker == 0)
53  {
54  NS_FATAL_ERROR ("Checker should not be zero on " << name );
55  }
56  m_initialValue = m_checker->CreateValidValue (initialValue);
58  if (m_initialValue == 0)
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 #ifdef HAVE_GETENV
71  char *envVar = getenv ("NS_GLOBAL_VALUE");
72  if (envVar == 0)
73  {
74  return;
75  }
76  std::string env = std::string (envVar);
77  std::string::size_type cur = 0;
78  std::string::size_type next = 0;
79  while (next != std::string::npos)
80  {
81  next = env.find (";", cur);
82  std::string tmp = std::string (env, cur, next-cur);
83  std::string::size_type equal = tmp.find ("=");
84  if (equal != std::string::npos)
85  {
86  std::string name = tmp.substr (0, equal);
87  std::string value = tmp.substr (equal+1, tmp.size () - equal - 1);
88  if (name == m_name)
89  {
90  Ptr<AttributeValue> v = m_checker->CreateValidValue (StringValue (value));
91  if (v != 0)
92  {
93  m_initialValue = v;
94  m_currentValue = v;
95  }
96  return;
97  }
98  }
99  cur = next + 1;
100  }
101 #endif /* HAVE_GETENV */
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 
NS_FATAL_x macro definitions.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Hold variables of type string.
Definition: string.h:41
void Set(const std::string &value)
Set the value.
Definition: string.cc:31
std::string m_help
The help string.
Definition: global-value.h:198
String attribute value declarations.
Hold a value for an Attribute.
Definition: attribute.h:68
Unsigned integer attribute value declarations and template implementations.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:77
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Ptr< AttributeValue > m_initialValue
The initial value.
Definition: global-value.h:200
GlobalValue(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeChecker > checker)
Constructor.
Definition: global-value.cc:42
std::string GetName(void) const
Get the name.
void ResetInitialValue(void)
Reset to the initial value.
static Vector * GetVector(void)
Get the static vector of all GlobalValues.
static Iterator Begin(void)
The Begin iterator.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Ptr< const AttributeChecker > m_checker
The AttributeChecker for this GlobalValue.
Definition: global-value.h:204
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
Definition: global-value.h:73
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool SetValue(const AttributeValue &value)
Set the value of this GlobalValue.
ns3::GlobalValue declaration.
std::string m_name
The name of this GlobalValue.
Definition: global-value.h:196
Ptr< AttributeValue > m_currentValue
The current value.
Definition: global-value.h:202
static Iterator End(void)
The End iterator.
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...
std::string GetHelp(void) const
Get the help string.
static void GetValueByName(std::string name, AttributeValue &value)
Finds the GlobalValue with the given name and returns its value.
Ptr< const AttributeChecker > GetChecker(void) const
Get the AttributeChecker.
Debug message logging.
void InitializeFromEnv(void)
Initialize from the NS_GLOBAL_VALUE environment variable.
Definition: global-value.cc:67
void GetValue(AttributeValue &value) const
Get the value.