A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
queue-size.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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 "queue-size.h"
10
11#include "ns3/log.h"
12
13namespace ns3
14{
15
16NS_LOG_COMPONENT_DEFINE("QueueSize");
17
19
20/* static */
21bool
22QueueSize::DoParse(const std::string s, QueueSizeUnit* unit, uint32_t* value)
23{
24 NS_LOG_FUNCTION(s << unit << value);
25 std::string::size_type n = s.find_first_not_of("0123456789.");
26 if (n != std::string::npos)
27 { // Found non-numeric
28 std::istringstream iss;
29 iss.str(s.substr(0, n));
30 double r;
31 iss >> r;
32 std::string trailer = s.substr(n, std::string::npos);
33 if (trailer == "B")
34 {
35 // bytes
37 *value = static_cast<uint32_t>(r);
38 }
39 else if (trailer == "kB" || trailer == "KB")
40 {
41 // kilobytes
43 *value = static_cast<uint32_t>(r * 1000);
44 }
45 else if (trailer == "KiB")
46 {
47 // kibibytes
49 *value = static_cast<uint32_t>(r * 1024);
50 }
51 else if (trailer == "MB")
52 {
53 // MegaBytes
55 *value = static_cast<uint32_t>(r * 1000000);
56 }
57 else if (trailer == "MiB")
58 {
59 // MebiBytes
61 *value = static_cast<uint32_t>(r * 1048576);
62 }
63 else if (trailer == "p")
64 {
65 // packets
67 *value = static_cast<uint32_t>(r);
68 }
69 else if (trailer == "kp" || trailer == "Kp")
70 {
71 // kilopackets
73 *value = static_cast<uint32_t>(r * 1000);
74 }
75 else if (trailer == "Kip")
76 {
77 // kibipackets
79 *value = static_cast<uint32_t>(r * 1024);
80 }
81 else if (trailer == "Mp")
82 {
83 // MegaPackets
85 *value = static_cast<uint32_t>(r * 1000000);
86 }
87 else if (trailer == "Mip")
88 {
89 // MebiPackets
91 *value = static_cast<uint32_t>(r * 1048576);
92 }
93 else
94 {
95 return false; // unsupported unit string
96 }
97 return true;
98 }
99 return false; // a unit string is required
100}
101
103 : m_unit(QueueSizeUnit::PACKETS),
104 m_value(0)
105{
106 NS_LOG_FUNCTION(this);
107}
108
110 : m_unit(unit),
111 m_value(value)
112{
113 NS_LOG_FUNCTION(this << static_cast<uint16_t>(unit) << value);
114}
115
116bool
117QueueSize::operator<(const QueueSize& rhs) const
118{
119 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
120
121 return m_value < rhs.m_value;
122}
123
124bool
125QueueSize::operator<=(const QueueSize& rhs) const
126{
127 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
128
129 return m_value <= rhs.m_value;
130}
131
132bool
134{
135 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
136
137 return m_value > rhs.m_value;
138}
139
140bool
142{
143 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
144
145 return m_value >= rhs.m_value;
146}
147
148bool
150{
151 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
152
153 return m_value == rhs.m_value;
154}
155
156bool
158{
159 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
160
161 return m_value != rhs.m_value;
162}
163
166{
167 NS_LOG_FUNCTION(this);
168 return m_unit;
169}
170
173{
174 NS_LOG_FUNCTION(this);
175 return m_value;
176}
177
178QueueSize::QueueSize(std::string size)
179{
180 NS_LOG_FUNCTION(this << size);
181 bool ok = DoParse(size, &m_unit, &m_value);
182 NS_ABORT_MSG_IF(!ok, "Could not parse queue size: " << size);
183}
184
185/* For printing of queue size */
186std::ostream&
187operator<<(std::ostream& os, const QueueSize& size)
188{
189 os << size.GetValue() << (size.GetUnit() == QueueSizeUnit::PACKETS ? "p" : "B");
190 return os;
191}
192
193/* Initialize a queue size from an input stream */
194std::istream&
195operator>>(std::istream& is, QueueSize& size)
196{
197 std::string value;
198 is >> value;
200 uint32_t l;
201 bool ok = QueueSize::DoParse(value, &m, &l);
202 if (!ok)
203 {
204 is.setstate(std::ios_base::failbit);
205 }
206 size = QueueSize(m, l);
207 return is;
208}
209
210} // namespace ns3
Class for representing queue sizes.
Definition queue-size.h:85
bool operator>(const QueueSize &rhs) const
bool operator<(const QueueSize &rhs) const
bool operator<=(const QueueSize &rhs) const
QueueSizeUnit GetUnit() const
Get the underlying unit.
bool operator!=(const QueueSize &rhs) const
uint32_t m_value
queue size [bytes or packets]
Definition queue-size.h:189
bool operator>=(const QueueSize &rhs) const
bool operator==(const QueueSize &rhs) const
QueueSizeUnit m_unit
unit
Definition queue-size.h:188
static bool DoParse(const std::string s, QueueSizeUnit *unit, uint32_t *value)
Parse a string representing a QueueSize.
Definition queue-size.cc:22
uint32_t GetValue() const
Get the underlying value.
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition queue-size.h:33
@ BYTES
Use number of bytes for queue size.
Definition queue-size.h:35
@ PACKETS
Use number of packets for queue size.
Definition queue-size.h:34
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