A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
global-value.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef GLOBAL_VALUE_H
20#define GLOBAL_VALUE_H
21
22#include "attribute.h"
23#include "ptr.h"
24
25#include <string>
26#include <vector>
27
28/**
29 * \file
30 * \ingroup core
31 * ns3::GlobalValue declaration.
32 */
33
34namespace ns3
35{
36
37/* Forward declaration */
38namespace tests
39{
40class GlobalValueTestCase;
41}
42
43/**
44 * \ingroup core
45 *
46 * \brief Hold a so-called 'global value'.
47 *
48 * A GlobalValue will get its value from (in order):
49 * - The initial value configured where it is defined,
50 * - From the \c NS_GLOBAL_VALUE environment variable,
51 * - From the command line,
52 * - By explicit call to SetValue() or Bind().
53 *
54 * Instances of this class are expected to be allocated as static
55 * global variables and should be used to store configurable global state.
56 * For example:
57 * \code
58 * // source.cc:
59 * static GlobalValue g_myGlobal =
60 * GlobalValue ("myGlobal", "My global value for ...",
61 * IntegerValue (12),
62 * MakeIntegerChecker ());
63 * \endcode
64 *
65 * GlobalValues can be set directly by calling GlobalValue::SetValue()
66 * but they can also be set through the \c NS_GLOBAL_VALUE environment variable.
67 * For example, \c NS_GLOBAL_VALUE='Name=Value;OtherName=OtherValue;' would set
68 * global values \c Name and \c OtherName to \c Value and \c OtherValue,
69 * respectively.
70 *
71 * Users of the CommandLine class also get the ability to set global
72 * values through command line arguments to their program:
73 * \c --Name=Value will set global value \c Name to \c Value.
74 */
76{
77 /** Container type for holding all the GlobalValues. */
78 typedef std::vector<GlobalValue*> Vector;
79
80 public:
81 /** Iterator type for the list of all global values. */
82 typedef Vector::const_iterator Iterator;
83
84 /**
85 * Constructor.
86 * \param [in] name the name of this global value.
87 * \param [in] help some help text which describes the purpose of this
88 * global value.
89 * \param [in] initialValue the value to assign to this global value
90 * during construction.
91 * \param [in] checker a pointer to an AttributeChecker which can verify
92 * that any user-supplied value to override the initial
93 * value matches the requested type constraints.
94 */
95 GlobalValue(std::string name,
96 std::string help,
97 const AttributeValue& initialValue,
99
100 /**
101 * Get the name.
102 * \returns The name of this GlobalValue.
103 */
104 std::string GetName() const;
105 /**
106 * Get the help string.
107 * \returns The help text of this GlobalValue.
108 */
109 std::string GetHelp() const;
110 /**
111 * Get the value.
112 * \param [out] value The AttributeValue to set to the value
113 * of this GlobalValue
114 */
115 void GetValue(AttributeValue& value) const;
116 /**
117 * Get the AttributeChecker.
118 * \returns The checker associated to this GlobalValue.
119 */
121 /**
122 * Set the value of this GlobalValue.
123 * \param [in] value the new value to set in this GlobalValue.
124 * \returns \c true if the Global Value was set successfully.
125 */
126 bool SetValue(const AttributeValue& value);
127
128 /** Reset to the initial value. */
129 void ResetInitialValue();
130
131 /**
132 * Iterate over the set of GlobalValues until a matching name is found
133 * and then set its value with GlobalValue::SetValue.
134 *
135 * \param [in] name the name of the global value
136 * \param [in] value the value to set in the requested global value.
137 *
138 * This method cannot fail. It will crash if the input is not valid.
139 */
140 static void Bind(std::string name, const AttributeValue& value);
141
142 /**
143 * Iterate over the set of GlobalValues until a matching name is found
144 * and then set its value with GlobalValue::SetValue.
145 *
146 * \param [in] name the name of the global value
147 * \param [in] value the value to set in the requested global value.
148 * \returns \c true if the value could be set successfully,
149 * \c false otherwise.
150 */
151 static bool BindFailSafe(std::string name, const AttributeValue& value);
152
153 /**
154 * The Begin iterator.
155 * \returns An iterator which represents a pointer to the first GlobalValue registered.
156 */
157 static Iterator Begin();
158 /**
159 * The End iterator.
160 * \returns An iterator which represents a pointer to the last GlobalValue registered.
161 */
162 static Iterator End();
163
164 /**
165 * Finds the GlobalValue with the given name and returns its value
166 *
167 * \param [in] name the name of the GlobalValue to be found
168 * \param [out] value where to store the value of the found GlobalValue
169 *
170 * \return \c true if the GlobalValue was found, \c false otherwise
171 */
172 static bool GetValueByNameFailSafe(std::string name, AttributeValue& value);
173
174 /**
175 * Finds the GlobalValue with the given name and returns its
176 * value.
177 *
178 * This method cannot fail, i.e., it will trigger a
179 * NS_FATAL_ERROR if the requested GlobalValue is not found.
180 *
181 * \param [in] name the name of the GlobalValue to be found
182 * \param [out] value where to store the value of the found GlobalValue
183 */
184 static void GetValueByName(std::string name, AttributeValue& value);
185
186 private:
187 /** Test case needs direct access to GetVector() */
189
190 /**
191 * Get the static vector of all GlobalValues.
192 *
193 * \returns The vector.
194 */
195 static Vector* GetVector();
196 /** Initialize from the \c NS_GLOBAL_VALUE environment variable. */
197 void InitializeFromEnv();
198
199 /** The name of this GlobalValue. */
200 std::string m_name;
201 /** The help string. */
202 std::string m_help;
203 /** The initial value. */
205 /** The current value. */
207 /** The AttributeChecker for this GlobalValue. */
209};
210
211} // namespace ns3
212
213#endif /* GLOBAL_VALUE_H */
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition: attribute.h:70
Hold a so-called 'global value'.
Definition: global-value.h:76
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:82
std::string GetHelp() const
Get the help string.
Definition: global-value.cc:91
std::vector< GlobalValue * > Vector
Container type for holding all the GlobalValues.
Definition: global-value.h:78
void GetValue(AttributeValue &value) const
Get the value.
Definition: global-value.cc:98
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.
Definition: global-value.cc:84
std::string m_help
The help string.
Definition: global-value.h:202
Ptr< const AttributeChecker > m_checker
The AttributeChecker for this GlobalValue.
Definition: global-value.h:208
Ptr< AttributeValue > m_currentValue
The current value.
Definition: global-value.h:206
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.
Definition: global-value.h:204
static Iterator End()
The End iterator.
std::string m_name
The name of this GlobalValue.
Definition: global-value.h:200
void InitializeFromEnv()
Initialize from the NS_GLOBAL_VALUE environment variable.
Definition: global-value.cc:67
static Vector * GetVector()
Get the static vector of all GlobalValues.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Test for the ability to get at a GlobalValue.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ptr smart pointer declaration and implementation.