A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
time-span.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#include "time-span.h"
10
11#include "assert.h"
12
13/**
14 * @file
15 * @ingroup time
16 * Declaration of class ns3::TimeSpan and definition of the TimeSpanValue attribute value.
17 */
18
19namespace ns3
20{
21
22TimeSpan::TimeSpan(const std::string& s)
23{
24 std::istringstream iss{s};
25 iss >> *this;
26}
27
28TimeSpan::TimeSpan(const Time& begin, const Time& end)
29 : m_begin(begin),
30 m_end(end)
31{
33 "Begin time (" << m_begin << ") must be less than or equal to end time (" << m_end
34 << ")");
35}
36
38TimeSpan::Create(const Time& begin, const Time& end)
39{
40 return TimeSpan(begin, end);
41}
42
43Time
45{
46 return m_begin;
47}
48
49Time
51{
52 return m_end;
53}
54
55Time
57{
58 return m_end - m_begin;
59}
60
61std::ostream&
62operator<<(std::ostream& os, const TimeSpan& timespan)
63{
64 os << "{" << timespan.Begin() << ", " << timespan.End() << "}";
65 return os;
66}
67
68std::istream&
69operator>>(std::istream& is, TimeSpan& timespan)
70{
71 char openingBrace;
72 if (!(is >> openingBrace) || openingBrace != '{')
73 {
74 NS_ABORT_MSG("Can't parse TimeSpan: opening brace not found");
75 }
76
77 std::string beginStr;
78 if (!std::getline(is, beginStr, ',') || is.eof())
79 {
80 NS_ABORT_MSG("Can't parse TimeSpan: ',' separator not found");
81 }
82
83 Time begin;
84 std::istringstream beginStream{beginStr};
85 beginStream >> begin;
86
87 std::string endStr;
88 if (!std::getline(is, endStr, '}') || is.eof())
89 {
90 NS_ABORT_MSG("Can't parse TimeSpan: closing brace not found");
91 }
92
93 Time end;
94 std::istringstream endStream{endStr};
95 endStream >> end;
96
97 timespan = TimeSpan(begin, end);
98 return is;
99}
100
101} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
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
Time Duration() const
Definition time-span.cc:56
static TimeSpan Create(const Time &begin, const Time &end)
TimeSpan factory (required by TimeSpanValue).
Definition time-span.cc:38
Time End() const
Definition time-span.cc:50
Time m_end
end time
Definition time-span.h:85
Time Begin() const
Definition time-span.cc:44
TimeSpan()=default
Time m_begin
begin time
Definition time-span.h:84
#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:76
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
Declaration of class ns3::TimeSpan and definition of the TimeSpanValue attribute value.