A Discrete-Event Network Simulator
API
test-string-value-formatting.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Tom Henderson
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 
18 #include "ns3/core-module.h"
19 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE("TestStringValueFormatting");
36 
37 namespace
38 {
39 
49 {
50  public:
55  static TypeId GetTypeId();
62  Ptr<RandomVariableStream> GetTestVariable() const;
63 
64  private:
66 };
67 
69 
70 TypeId
71 FormattingTestObject::GetTypeId()
72 {
73  static TypeId tid =
74  TypeId("ns3::FormattingTestObject")
75  .SetParent<Object>()
76  .AddConstructor<FormattingTestObject>()
77  .AddAttribute("OnTime",
78  "A RandomVariableStream used to pick the duration of the 'On' state.",
79  StringValue("ns3::ConstantRandomVariable[Constant=1.0]"),
80  MakePointerAccessor(&FormattingTestObject::m_testVariable),
81  MakePointerChecker<RandomVariableStream>());
82  return tid;
83 }
84 
85 FormattingTestObject::FormattingTestObject()
86 {
87 }
88 
90 FormattingTestObject::GetTestVariable() const
91 {
92  return m_testVariable;
93 }
94 
101 {
102  public:
110  void SetAttribute(std::string name, const AttributeValue& value);
115  Ptr<Object> CreateFromFactory();
116 
117  private:
119 };
120 
121 FormattingTestObjectHelper::FormattingTestObjectHelper()
122 {
123  m_factory.SetTypeId(FormattingTestObject::GetTypeId());
124 }
125 
126 void
127 FormattingTestObjectHelper::SetAttribute(std::string name, const AttributeValue& value)
128 {
129  m_factory.Set(name, value);
130 }
131 
133 FormattingTestObjectHelper::CreateFromFactory()
134 {
135  return m_factory.Create();
136 }
137 
138 } // unnamed namespace
139 
140 int
141 main(int argc, char* argv[])
142 {
143  // CreateObject parsing
144  Ptr<FormattingTestObject> obj = CreateObject<FormattingTestObject>();
145  obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable"));
146  obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.]"));
147  obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
148  obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=50.|Max=100.]"));
149 
150  Ptr<RandomVariableStream> rvStream = obj->GetTestVariable();
151  // Either GetObject () or DynamicCast may be used to get subclass pointer
152  Ptr<UniformRandomVariable> uniformStream = rvStream->GetObject<UniformRandomVariable>();
153  NS_ASSERT(uniformStream);
154 
155  // Check that the last setting of Min to 50 and Max to 100 worked
156  DoubleValue val;
157  uniformStream->GetAttribute("Min", val);
158  NS_ASSERT_MSG(val.Get() == 50, "Minimum not set to 50");
159  uniformStream->GetAttribute("Max", val);
160  NS_ASSERT_MSG(val.Get() == 100, "Maximum not set to 100");
161 
162  // The following malformed values should result in an error exit
163  // if uncommented
164 
165  // Attribute doesn't exist
166  // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
167  // Missing right bracket
168  // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0."));
169  // Comma delimiter fails
170  // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
171  // Incomplete specification
172  // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=]"));
173  // Incomplete specification
174  // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max]"));
175 
176  // ObjectFactory parsing
177  FormattingTestObjectHelper formattingHelper;
178  formattingHelper.SetAttribute("OnTime",
179  StringValue("ns3::UniformRandomVariable[Min=30.|Max=60.0]"));
180  // Attribute doesn't exist
181  // formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
182  // Missing right bracket
183  // formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
184  // Comma delimiter fails
185  // formattingHelper.SetAttribute ("OnTime", StringValue
186  // ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
187  // Incomplete specification
188  // formattingHelper.SetAttribute ("OnTime", StringValue
189  // ("ns3::UniformRandomVariable[Min=30.|Max=]"));
190  // Incomplete specification
191  // formattingHelper.SetAttribute ("OnTime", StringValue
192  // ("ns3::UniformRandomVariable[Min=30.|Max]"));
193 
194  // verify that creation occurs correctly
195  Ptr<Object> outputObj = formattingHelper.CreateFromFactory();
196  Ptr<FormattingTestObject> fto = DynamicCast<FormattingTestObject>(outputObj);
197  NS_ASSERT_MSG(fto, "object creation failed");
198  rvStream = fto->GetTestVariable();
199  uniformStream = rvStream->GetObject<UniformRandomVariable>();
200  NS_ASSERT(uniformStream);
201  // Check that the last setting of Min to 30 and Max to 60 worked
202  uniformStream->GetAttribute("Min", val);
203  NS_ASSERT_MSG(val.Get() == 30, "Minimum not set to 30");
204  uniformStream->GetAttribute("Max", val);
205  NS_ASSERT_MSG(val.Get() == 60, "Maximum not set to 60");
206 
207  return 0;
208 }
Hold a value for an Attribute.
Definition: attribute.h:70
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
double Get() const
Definition: double.cc:37
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:244
Instantiate subclasses of ns3::Object.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
The uniform distribution Random Number Generator (RNG).
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227