A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
26
#include "ns3/core-config.h"
27
#ifdef HAVE_STDLIB_H
28
#include <stdlib.h>
29
#endif
30
31
namespace
ns3 {
32
33
GlobalValue::GlobalValue
(std::string name, std::string help,
34
const
AttributeValue
&initialValue,
35
Ptr<const AttributeChecker>
checker)
36
: m_name (name),
37
m_help (help),
38
m_initialValue (0),
39
m_currentValue (0),
40
m_checker (checker)
41
{
42
if
(
m_checker
== 0)
43
{
44
NS_FATAL_ERROR
(
"Checker should not be zero on "
<< name );
45
}
46
m_initialValue
=
m_checker
->
CreateValidValue
(initialValue);
47
m_currentValue
=
m_initialValue
;
48
if
(
m_initialValue
== 0)
49
{
50
NS_FATAL_ERROR
(
"Value set by user on "
<< name <<
" is invalid."
);
51
}
52
GetVector
()->push_back (
this
);
53
InitializeFromEnv
();
54
}
55
56
void
57
GlobalValue::InitializeFromEnv
(
void
)
58
{
59
#ifdef HAVE_GETENV
60
char
*envVar = getenv (
"NS_GLOBAL_VALUE"
);
61
if
(envVar == 0)
62
{
63
return
;
64
}
65
std::string env = std::string (envVar);
66
std::string::size_type cur = 0;
67
std::string::size_type next = 0;
68
while
(next != std::string::npos)
69
{
70
next = env.find (
";"
, cur);
71
std::string tmp = std::string (env, cur, next-cur);
72
std::string::size_type equal = tmp.find (
"="
);
73
if
(equal != std::string::npos)
74
{
75
std::string name = tmp.substr (0, equal);
76
std::string value = tmp.substr (equal+1, tmp.size () - equal - 1);
77
if
(name ==
m_name
)
78
{
79
Ptr<AttributeValue>
v =
m_checker
->
CreateValidValue
(
StringValue
(value));
80
if
(v != 0)
81
{
82
m_initialValue
= v;
83
m_currentValue
= v;
84
}
85
return
;
86
}
87
}
88
cur = next + 1;
89
}
90
#endif
/* HAVE_GETENV */
91
}
92
93
std::string
94
GlobalValue::GetName
(
void
)
const
95
{
96
return
m_name
;
97
}
98
std::string
99
GlobalValue::GetHelp
(
void
)
const
100
{
101
return
m_help
;
102
}
103
void
104
GlobalValue::GetValue
(
AttributeValue
&value)
const
105
{
106
bool
ok =
m_checker
->
Copy
(*
m_currentValue
, value);
107
if
(ok)
108
{
109
return
;
110
}
111
StringValue
*str =
dynamic_cast<
StringValue
*
>
(&value);
112
if
(str == 0)
113
{
114
NS_FATAL_ERROR
(
"GlobalValue name="
<<
m_name
<<
": input value is not a string"
);
115
}
116
str->
Set
(
m_currentValue
->
SerializeToString
(
m_checker
));
117
}
118
Ptr<const AttributeChecker>
119
GlobalValue::GetChecker
(
void
)
const
120
{
121
return
m_checker
;
122
}
123
124
bool
125
GlobalValue::SetValue
(
const
AttributeValue
&value)
126
{
127
Ptr<AttributeValue>
v =
m_checker
->
CreateValidValue
(value);
128
if
(v == 0)
129
{
130
return
0;
131
}
132
m_currentValue
= v;
133
return
true
;
134
}
135
136
void
137
GlobalValue::Bind
(std::string name,
const
AttributeValue
&value)
138
{
139
for
(
Iterator
i =
Begin
(); i !=
End
(); i++)
140
{
141
if
((*i)->GetName () == name)
142
{
143
if
(!(*i)->SetValue (value))
144
{
145
NS_FATAL_ERROR
(
"Invalid new value for global value: "
<<name);
146
}
147
return
;
148
}
149
}
150
NS_FATAL_ERROR
(
"Non-existant global value: "
<<name);
151
}
152
bool
153
GlobalValue::BindFailSafe
(std::string name,
const
AttributeValue
&value)
154
{
155
for
(
Iterator
i =
Begin
(); i !=
End
(); i++)
156
{
157
if
((*i)->GetName () == name)
158
{
159
return
(*i)->SetValue (value);
160
}
161
}
162
return
false
;
163
}
164
GlobalValue::Iterator
165
GlobalValue::Begin
(
void
)
166
{
167
return
GetVector
()->begin ();
168
}
169
GlobalValue::Iterator
170
GlobalValue::End
(
void
)
171
{
172
return
GetVector
()->end ();
173
}
174
175
void
176
GlobalValue::ResetInitialValue
(
void
)
177
{
178
m_currentValue
=
m_initialValue
;
179
}
180
181
bool
182
GlobalValue::GetValueByNameFailSafe
(std::string name,
AttributeValue
&value)
183
{
184
for
(
GlobalValue::Iterator
gvit =
GlobalValue::Begin
(); gvit !=
GlobalValue::End
(); ++gvit)
185
{
186
if
((*gvit)->GetName () == name)
187
{
188
(*gvit)->GetValue (value);
189
return
true
;
190
}
191
}
192
return
false
;
// not found
193
}
194
195
void
196
GlobalValue::GetValueByName
(std::string name,
AttributeValue
&value)
197
{
198
if
(!
GetValueByNameFailSafe
(name, value))
199
{
200
NS_FATAL_ERROR
(
"Could not find GlobalValue named \""
<< name <<
"\""
);
201
}
202
}
203
204
GlobalValue::Vector
*
205
GlobalValue::GetVector
(
void
)
206
{
207
static
Vector
vector;
208
return
&vector;
209
}
210
211
}
// namespace ns3
212
src
core
model
global-value.cc
Generated on Tue Oct 9 2012 16:45:34 for ns-3 by
1.8.1.2