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