A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
timespan-example.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2026 Universita' degli Studi di Napoli Federico II
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Stefano Avallone <stavallo@unina.it>
7
*/
8
9
/**
10
* @file
11
* Example to demonstrate that both a TimeSpan attribute and a TimeSpan variable can be set
12
* via the CommandLine system.
13
*
14
* The example expects that the command line arguments are such that the TimeSpan attribute is set
15
* to {100ms,200ms} and the TimeSpan variable is set to {300ms,400ms}. If that is not the case, a
16
* non-zero value is returned.
17
*/
18
19
#include "ns3/core-module.h"
20
21
NS_LOG_COMPONENT_DEFINE
(
"TimeSpanExample"
);
22
23
namespace
ns3
24
{
25
26
/** Object containing an attribute represented by a TimeSpan. */
27
class
ObjectWithTimeSpanAttribute
:
public
Object
28
{
29
public
:
30
/**
31
* Get the type ID.
32
*
33
* @return the object TypeId
34
*/
35
static
TypeId
GetTypeId
();
36
37
TimeSpan
m_timeSpan
;
//!< TimeSpan field
38
};
39
40
TypeId
41
ObjectWithTimeSpanAttribute::GetTypeId
()
42
{
43
static
TypeId
tid =
44
TypeId
(
"ns3::ObjectWithTimeSpanAttribute"
)
45
.
SetParent
<
Object
>()
46
.SetGroupName(
"Test"
)
47
.AddConstructor<
ObjectWithTimeSpanAttribute
>()
48
.AddAttribute(
49
"TimeSpanAttr"
,
50
"Attribute whose value is represented by a TimeSpan"
,
51
TimeSpanValue
(
TimeSpan
{
MilliSeconds
(500),
Time
{
"1s"
}}),
52
MakeStructAccessor<TimeSpanValue>
(&
ObjectWithTimeSpanAttribute::m_timeSpan
),
53
MakeStructChecker<TimeSpanValue>
(
MakeTimeChecker
(),
MakeTimeChecker
()));
54
return
tid;
55
}
56
57
NS_OBJECT_ENSURE_REGISTERED
(
ObjectWithTimeSpanAttribute
);
58
59
}
// namespace ns3
60
61
using namespace
ns3
;
62
63
int
64
main(
int
argc,
char
* argv[])
65
{
66
const
TimeSpan
expectedTsAttr(
"{100ms, 200ms}"
);
67
const
TimeSpan
expectedTsVar(
"{300ms, 400ms}"
);
68
69
TimeSpan
tsVar;
70
71
CommandLine
cmd(__FILE__);
72
cmd.AddValue(
"tsAttr"
,
"ns3::ObjectWithTimeSpanAttribute::TimeSpanAttr"
);
73
cmd.AddValue(
"tsVar"
,
"Example to set a TimeSpan variable"
, tsVar);
74
cmd.Parse(argc, argv);
75
76
auto
obj =
CreateObject<ObjectWithTimeSpanAttribute>
();
77
78
if
(obj->m_timeSpan != expectedTsAttr)
79
{
80
std::cerr <<
"Unexpected TimeSpan attribute value: "
<< obj->m_timeSpan <<
"\n"
;
81
return
-1;
82
}
83
84
if
(tsVar != expectedTsVar)
85
{
86
std::cerr <<
"Unexpected TimeSpan variable value: "
<< tsVar <<
"\n"
;
87
return
-1;
88
}
89
90
return
0;
91
}
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::Object::Object
Object()
Caller graph was not generated because of its size.
Definition
object.cc:93
ns3::ObjectWithTimeSpanAttribute
Object containing an attribute represented by a TimeSpan.
Definition
timespan-example.cc:28
ns3::ObjectWithTimeSpanAttribute::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
timespan-example.cc:41
ns3::ObjectWithTimeSpanAttribute::m_timeSpan
TimeSpan m_timeSpan
TimeSpan field.
Definition
timespan-example.cc:37
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition
nstime.h:95
ns3::TimeSpan
Class representing a period of time defined via a begin time and an end time.
Definition
time-span.h:30
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:50
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1003
ns3::MakeStructChecker
Ptr< const AttributeChecker > MakeStructChecker(Ts... checkers)
Create a StructChecker from the AttributeCheckers associated with the fields.
Definition
struct.h:758
ns3::MakeStructAccessor
Ptr< const AttributeAccessor > MakeStructAccessor(T1 a1)
Create an AttributeAccessor for a structure data member, or for a lone class get functor or set metho...
Definition
struct.h:765
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:201
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:627
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition
nstime.h:1330
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition
nstime.h:1436
ns3::TimeSpanValue
StructValue< TimeSpan, StructConstructor<&TimeSpan::Create >, StructField< TimeValue, &TimeSpan::Begin >, StructField< TimeValue, &TimeSpan::End > > TimeSpanValue
Definition of the TimeSpanValue attribute value.
Definition
time-span.h:112
src
core
examples
timespan-example.cc
Generated on
for ns-3 by
1.16.1