A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("GlobalValue")
35  ;
36 
37 GlobalValue::GlobalValue (std::string name, std::string help,
38  const AttributeValue &initialValue,
40  : m_name (name),
41  m_help (help),
42  m_initialValue (0),
43  m_currentValue (0),
44  m_checker (checker)
45 {
46  NS_LOG_FUNCTION (name << help << &initialValue << checker);
47  if (m_checker == 0)
48  {
49  NS_FATAL_ERROR ("Checker should not be zero on " << name );
50  }
51  m_initialValue = m_checker->CreateValidValue (initialValue);
53  if (m_initialValue == 0)
54  {
55  NS_FATAL_ERROR ("Value set by user on " << name << " is invalid.");
56  }
57  GetVector ()->push_back (this);
59 }
60 
61 void
63 {
64  NS_LOG_FUNCTION (this);
65 #ifdef HAVE_GETENV
66  char *envVar = getenv ("NS_GLOBAL_VALUE");
67  if (envVar == 0)
68  {
69  return;
70  }
71  std::string env = std::string (envVar);
72  std::string::size_type cur = 0;
73  std::string::size_type next = 0;
74  while (next != std::string::npos)
75  {
76  next = env.find (";", cur);
77  std::string tmp = std::string (env, cur, next-cur);
78  std::string::size_type equal = tmp.find ("=");
79  if (equal != std::string::npos)
80  {
81  std::string name = tmp.substr (0, equal);
82  std::string value = tmp.substr (equal+1, tmp.size () - equal - 1);
83  if (name == m_name)
84  {
85  Ptr<AttributeValue> v = m_checker->CreateValidValue (StringValue (value));
86  if (v != 0)
87  {
88  m_initialValue = v;
89  m_currentValue = v;
90  }
91  return;
92  }
93  }
94  cur = next + 1;
95  }
96 #endif /* HAVE_GETENV */
97 }
98 
99 std::string
101 {
103  return m_name;
104 }
105 std::string
107 {
109  return m_help;
110 }
111 void
113 {
114  NS_LOG_FUNCTION (&value);
115  bool ok = m_checker->Copy (*m_currentValue, value);
116  if (ok)
117  {
118  return;
119  }
120  StringValue *str = dynamic_cast<StringValue *> (&value);
121  if (str == 0)
122  {
123  NS_FATAL_ERROR ("GlobalValue name="<<m_name<<": input value is not a string");
124  }
125  str->Set (m_currentValue->SerializeToString (m_checker));
126 }
129 {
130  NS_LOG_FUNCTION (this);
131 
132  return m_checker;
133 }
134 
135 bool
137 {
138  NS_LOG_FUNCTION (&value);
139 
140  Ptr<AttributeValue> v = m_checker->CreateValidValue (value);
141  if (v == 0)
142  {
143  return 0;
144  }
145  m_currentValue = v;
146  return true;
147 }
148 
149 void
150 GlobalValue::Bind (std::string name, const AttributeValue &value)
151 {
152  NS_LOG_FUNCTION (name << &value);
153 
154  for (Iterator i = Begin (); i != End (); i++)
155  {
156  if ((*i)->GetName () == name)
157  {
158  if (!(*i)->SetValue (value))
159  {
160  NS_FATAL_ERROR ("Invalid new value for global value: "<<name);
161  }
162  return;
163  }
164  }
165  NS_FATAL_ERROR ("Non-existant global value: "<<name);
166 }
167 bool
168 GlobalValue::BindFailSafe (std::string name, const AttributeValue &value)
169 {
170  NS_LOG_FUNCTION (name << &value);
171 
172  for (Iterator i = Begin (); i != End (); i++)
173  {
174  if ((*i)->GetName () == name)
175  {
176  return (*i)->SetValue (value);
177  }
178  }
179  return false;
180 }
183 {
185 
186  return GetVector ()->begin ();
187 }
190 {
192  return GetVector ()->end ();
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION (this);
200 }
201 
202 bool
204 {
205  NS_LOG_FUNCTION (name << &value);
206  for (GlobalValue::Iterator gvit = GlobalValue::Begin (); gvit != GlobalValue::End (); ++gvit)
207  {
208  if ((*gvit)->GetName () == name)
209  {
210  (*gvit)->GetValue (value);
211  return true;
212  }
213  }
214  return false; // not found
215 }
216 
217 void
218 GlobalValue::GetValueByName (std::string name, AttributeValue &value)
219 {
220  NS_LOG_FUNCTION (name << &value);
221  if (!GetValueByNameFailSafe (name, value))
222  {
223  NS_FATAL_ERROR ("Could not find GlobalValue named \"" << name << "\"");
224  }
225 }
226 
229 {
231  static Vector vector;
232  return &vector;
233 }
234 
235 } // namespace ns3
236 
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
hold variables of type string
Definition: string.h:19
void Set(const std::string &value)
std::string m_help
Definition: global-value.h:149
Hold a value for an Attribute.
Definition: attribute.h:56
Vector::const_iterator Iterator
Definition: global-value.h:51
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
Ptr< AttributeValue > m_initialValue
Definition: global-value.h:150
GlobalValue(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeChecker > checker)
Definition: global-value.cc:37
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
std::string GetName(void) const
void ResetInitialValue(void)
static Vector * GetVector(void)
static Iterator Begin(void)
Ptr< const AttributeChecker > m_checker
Definition: global-value.h:152
std::vector< GlobalValue * > Vector
Definition: global-value.h:49
static void Bind(std::string name, const AttributeValue &value)
static bool GetValueByNameFailSafe(std::string name, AttributeValue &value)
finds the GlobalValue with the given name and returns its value
bool SetValue(const AttributeValue &value)
std::string m_name
Definition: global-value.h:148
Ptr< AttributeValue > m_currentValue
Definition: global-value.h:151
static Iterator End(void)
static bool BindFailSafe(std::string name, const AttributeValue &value)
std::string GetHelp(void) const
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
void InitializeFromEnv(void)
Definition: global-value.cc:62
void GetValue(AttributeValue &value) const