A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
global-value.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "global-value.h"
9
10#include "attribute.h"
12#include "fatal-error.h"
13#include "log.h"
14#include "string.h"
15
16/**
17 * @file
18 * @ingroup core
19 * ns3::GlobalValue implementation.
20 */
21
22namespace ns3
23{
24
25NS_LOG_COMPONENT_DEFINE("GlobalValue");
26
27GlobalValue::GlobalValue(std::string name,
28 std::string help,
29 const AttributeValue& initialValue,
31 : m_name(name),
32 m_help(help),
33 m_initialValue(nullptr),
34 m_currentValue(nullptr),
35 m_checker(checker)
36{
37 NS_LOG_FUNCTION(name << help << &initialValue << checker);
38 if (!m_checker)
39 {
40 NS_FATAL_ERROR("Checker should not be zero on " << name);
41 }
42 m_initialValue = m_checker->CreateValidValue(initialValue);
44 if (!m_initialValue)
45 {
46 NS_FATAL_ERROR("Value set by user on " << name << " is invalid.");
47 }
48 GetVector()->push_back(this);
50}
51
52void
54{
55 NS_LOG_FUNCTION(this);
56
57 auto [found, value] = EnvironmentVariable::Get("NS_GLOBAL_VALUE", m_name);
58 if (found)
59 {
60 Ptr<AttributeValue> v = m_checker->CreateValidValue(StringValue(value));
61 if (v)
62 {
65 }
66 }
67}
68
69std::string
71{
73 return m_name;
74}
75
76std::string
78{
80 return m_help;
81}
82
83void
85{
86 NS_LOG_FUNCTION(&value);
87 bool ok = m_checker->Copy(*m_currentValue, value);
88 if (ok)
89 {
90 return;
91 }
92 auto str = dynamic_cast<StringValue*>(&value);
93 if (str == nullptr)
94 {
95 NS_FATAL_ERROR("GlobalValue name=" << m_name << ": input value is not a string");
96 }
97 str->Set(m_currentValue->SerializeToString(m_checker));
98}
99
102{
103 NS_LOG_FUNCTION(this);
104
105 return m_checker;
106}
107
108bool
110{
111 NS_LOG_FUNCTION(&value);
112
113 Ptr<AttributeValue> v = m_checker->CreateValidValue(value);
114 if (!v)
115 {
116 return false;
117 }
119 return true;
120}
121
122void
123GlobalValue::Bind(std::string name, const AttributeValue& value)
124{
125 NS_LOG_FUNCTION(name << &value);
126
127 for (auto i = Begin(); i != End(); i++)
128 {
129 if ((*i)->GetName() == name)
130 {
131 if (!(*i)->SetValue(value))
132 {
133 NS_FATAL_ERROR("Invalid new value for global value: " << name);
134 }
135 return;
136 }
137 }
138 NS_FATAL_ERROR("Non-existent global value: " << name);
139}
140
141bool
142GlobalValue::BindFailSafe(std::string name, const AttributeValue& value)
143{
144 NS_LOG_FUNCTION(name << &value);
145
146 for (auto i = Begin(); i != End(); i++)
147 {
148 if ((*i)->GetName() == name)
149 {
150 return (*i)->SetValue(value);
151 }
152 }
153 return false;
154}
155
158{
160
161 return GetVector()->begin();
162}
163
166{
168 return GetVector()->end();
169}
170
171void
177
178bool
180{
181 NS_LOG_FUNCTION(name << &value);
182 for (auto gvit = GlobalValue::Begin(); gvit != GlobalValue::End(); ++gvit)
183 {
184 if ((*gvit)->GetName() == name)
185 {
186 (*gvit)->GetValue(value);
187 return true;
188 }
189 }
190 return false; // not found
191}
192
193void
195{
196 NS_LOG_FUNCTION(name << &value);
197 if (!GetValueByNameFailSafe(name, value))
198 {
199 NS_FATAL_ERROR("Could not find GlobalValue named \"" << name << "\"");
200 }
201}
202
205{
207 static Vector vector;
208 return &vector;
209}
210
211} // namespace ns3
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
uint32_t v
Hold a value for an Attribute.
Definition attribute.h:59
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
Vector::const_iterator Iterator
Iterator type for the list of all global values.
std::string GetHelp() const
Get the help string.
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
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.
static Iterator Begin()
The Begin iterator.
std::string GetName() const
Get the name.
std::string m_help
The help string.
Ptr< const AttributeChecker > m_checker
The AttributeChecker for this GlobalValue.
Ptr< AttributeValue > m_currentValue
The current value.
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.
void ResetInitialValue()
Reset to the initial 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...
Ptr< const AttributeChecker > GetChecker() const
Get the AttributeChecker.
Ptr< AttributeValue > m_initialValue
The initial value.
static Iterator End()
The End iterator.
GlobalValue(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeChecker > checker)
Constructor.
std::string m_name
The name of this GlobalValue.
void InitializeFromEnv()
Initialize from the NS_GLOBAL_VALUE environment variable.
static Vector * GetVector()
Get the static vector of all GlobalValues.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Hold variables of type string.
Definition string.h:45
void Set(const std::string &value)
Set the value.
Definition string.cc:20
STL class.
Class Environment declaration.
NS_FATAL_x macro definitions.
ns3::GlobalValue declaration.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#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.