A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
test-string-value-formatting.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2016 Tom Henderson
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
19
#include "ns3/core-module.h"
20
34
using namespace
ns3
;
35
36
NS_LOG_COMPONENT_DEFINE
(
"TestStringValueFormatting"
);
37
38
namespace
{
39
48
class
FormattingTestObject
:
public
Object
49
{
50
public
:
55
static
TypeId
GetTypeId (
void
);
57
FormattingTestObject
();
62
Ptr<RandomVariableStream>
GetTestVariable (
void
)
const
;
63
64
private
:
65
Ptr<RandomVariableStream>
m_testVariable
;
66
};
67
68
NS_OBJECT_ENSURE_REGISTERED
(
FormattingTestObject
);
69
70
TypeId
71
FormattingTestObject::GetTypeId (
void
)
72
{
73
static
TypeId
tid =
TypeId
(
"ns3::FormattingTestObject"
)
74
.
SetParent
<
Object
> ()
75
.AddConstructor<FormattingTestObject> ()
76
.AddAttribute (
"OnTime"
,
"A RandomVariableStream used to pick the duration of the 'On' state."
,
77
StringValue
(
"ns3::ConstantRandomVariable[Constant=1.0]"
),
78
MakePointerAccessor
(&FormattingTestObject::m_testVariable),
79
MakePointerChecker <RandomVariableStream>())
80
;
81
return
tid;
82
}
83
84
FormattingTestObject::FormattingTestObject ()
85
{}
86
87
Ptr<RandomVariableStream>
88
FormattingTestObject::GetTestVariable (
void
)
const
89
{
90
return
m_testVariable;
91
}
92
98
class
FormattingTestObjectHelper
99
{
100
public
:
102
FormattingTestObjectHelper
();
108
void
SetAttribute (std::string name,
const
AttributeValue
&value);
113
Ptr<Object>
CreateFromFactory (
void
);
114
115
private
:
116
ObjectFactory
m_factory
;
117
};
118
119
FormattingTestObjectHelper::FormattingTestObjectHelper ()
120
{
121
m_factory.
SetTypeId
(FormattingTestObject::GetTypeId ());
122
}
123
124
void
125
FormattingTestObjectHelper::SetAttribute (std::string name,
const
AttributeValue
&value)
126
{
127
m_factory.Set (name, value);
128
}
129
130
Ptr<Object>
131
FormattingTestObjectHelper::CreateFromFactory (
void
)
132
{
133
return
m_factory.Create ();
134
}
135
136
}
// unnamed namespace
137
138
139
int
140
main (
int
argc,
char
*argv[])
141
{
142
// CreateObject parsing
143
Ptr<FormattingTestObject>
obj = CreateObject<FormattingTestObject> ();
144
obj->SetAttribute (
"OnTime"
,
StringValue
(
"ns3::UniformRandomVariable"
));
145
obj->SetAttribute (
"OnTime"
,
StringValue
(
"ns3::UniformRandomVariable[Min=0.]"
));
146
obj->SetAttribute (
"OnTime"
,
StringValue
(
"ns3::UniformRandomVariable[Min=0.|Max=1.]"
));
147
obj->SetAttribute (
"OnTime"
,
StringValue
(
"ns3::UniformRandomVariable[Min=50.|Max=100.]"
));
148
149
Ptr<RandomVariableStream>
rvStream = obj->GetTestVariable ();
150
// Either GetObject () or DynamicCast may be used to get subclass pointer
151
Ptr<UniformRandomVariable>
uniformStream = rvStream->
GetObject
<
UniformRandomVariable
> ();
152
NS_ASSERT
(uniformStream);
153
154
// Check that the last setting of Min to 50 and Max to 100 worked
155
DoubleValue
val;
156
uniformStream->
GetAttribute
(
"Min"
, val);
157
NS_ASSERT_MSG
(val.
Get
() == 50,
"Minimum not set to 50"
);
158
uniformStream->
GetAttribute
(
"Max"
, val);
159
NS_ASSERT_MSG
(val.
Get
() == 100,
"Maximum not set to 100"
);
160
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"
,
StringValue
(
"ns3::UniformRandomVariable[Min=30.|Max=60.0]"
));
179
// Attribute doesn't exist
180
//formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
181
// Missing right bracket
182
//formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
183
// Comma delimiter fails
184
//formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
185
// Incomplete specification
186
//formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=]"));
187
// Incomplete specification
188
//formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max]"));
189
190
// verify that creation occurs correctly
191
Ptr<Object>
outputObj = formattingHelper.CreateFromFactory ();
192
Ptr<FormattingTestObject>
fto = DynamicCast<FormattingTestObject> (outputObj);
193
NS_ASSERT_MSG
(fto,
"object creation failed"
);
194
rvStream = fto->GetTestVariable ();
195
uniformStream = rvStream->
GetObject
<
UniformRandomVariable
> ();
196
NS_ASSERT
(uniformStream);
197
// Check that the last setting of Min to 30 and Max to 60 worked
198
uniformStream->
GetAttribute
(
"Min"
, val);
199
NS_ASSERT_MSG
(val.
Get
() == 30,
"Minimum not set to 30"
);
200
uniformStream->
GetAttribute
(
"Max"
, val);
201
NS_ASSERT_MSG
(val.
Get
() == 60,
"Maximum not set to 60"
);
202
}
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition:
assert.h:67
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
anonymous_namespace{test-string-value-formatting.cc}::FormattingTestObject::m_testVariable
Ptr< RandomVariableStream > m_testVariable
The test variable.
Definition:
test-string-value-formatting.cc:65
anonymous_namespace{test-string-value-formatting.cc}::FormattingTestObjectHelper::m_factory
ObjectFactory m_factory
Object factory.
Definition:
test-string-value-formatting.cc:116
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition:
object.h:470
ns3::AttributeValue
Hold a value for an Attribute.
Definition:
attribute.h:69
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:923
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition:
double.h:41
ns3::Ptr< RandomVariableStream >
ns3::UniformRandomVariable
The uniform distribution Random Number Generator (RNG).
Definition:
random-variable-stream.h:235
ns3::Object
A base class which provides memory management and object aggregation.
Definition:
object.h:88
ns3::ObjectBase::GetAttribute
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition:
object-base.cc:223
ns3::ObjectFactory
Instantiate subclasses of ns3::Object.
Definition:
object-factory.h:48
anonymous_namespace{test-string-value-formatting.cc}::FormattingTestObject
StringValue formatting example test object.
Definition:
test-string-value-formatting.cc:49
NS_ASSERT_MSG
#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:88
ns3::DoubleValue::Get
double Get(void) const
Definition:
double.cc:35
ns3::MakePointerAccessor
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition:
pointer.h:227
ns3::StringValue
Hold variables of type string.
Definition:
string.h:41
anonymous_namespace{test-string-value-formatting.cc}::FormattingTestObjectHelper
StringValue formatting example test helper class.
Definition:
test-string-value-formatting.cc:99
ns3::ObjectFactory::SetTypeId
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Definition:
object-factory.cc:40
src
core
examples
test-string-value-formatting.cc
Generated on Fri Oct 1 2021 17:02:57 for ns-3 by
1.8.20