A Discrete-Event Network Simulator
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
21NS_LOG_COMPONENT_DEFINE("TimeSpanExample");
22
23namespace ns3
24{
25
26/** Object containing an attribute represented by a TimeSpan. */
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
42{
43 static TypeId tid =
44 TypeId("ns3::ObjectWithTimeSpanAttribute")
46 .SetGroupName("Test")
47 .AddConstructor<ObjectWithTimeSpanAttribute>()
48 .AddAttribute(
49 "TimeSpanAttr",
50 "Attribute whose value is represented by a TimeSpan",
54 return tid;
55}
56
58
59} // namespace ns3
60
61using namespace ns3;
62
63int
64main(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
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}
Parse command-line arguments.
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Object containing an attribute represented by a TimeSpan.
static TypeId GetTypeId()
Get the type ID.
TimeSpan m_timeSpan
TimeSpan field.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
Class representing a period of time defined via a begin time and an end time.
Definition time-span.h:30
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
Ptr< const AttributeChecker > MakeStructChecker(Ts... checkers)
Create a StructChecker from the AttributeCheckers associated with the fields.
Definition struct.h:758
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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1330
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1436
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