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
108
110 : m_unit(unit),
111 m_value(value)
112{
113 NS_LOG_FUNCTION(this << static_cast<uint16_t>(unit) << value);
114}
115
118{
119 NS_LOG_FUNCTION(this);
120 return m_unit;
121}
122
125{
126 NS_LOG_FUNCTION(this);
127 return m_value;
128}
129
130QueueSize::QueueSize(std::string size)
131{
132 NS_LOG_FUNCTION(this << size);
133 bool ok = DoParse(size, &m_unit, &m_value);
134 NS_ABORT_MSG_IF(!ok, "Could not parse queue size: " << size);
135}
136
137/* For printing of queue size */
138std::ostream&
139operator<<(std::ostream& os, const QueueSize& size)
140{
141 os << size.GetValue() << (size.GetUnit() == QueueSizeUnit::PACKETS ? "p" : "B");
142 return os;
143}
144
145/* Initialize a queue size from an input stream */
146std::istream&
147operator>>(std::istream& is, QueueSize& size)
148{
149 std::string value;
150 is >> value;
152 uint32_t l;
153 bool ok = QueueSize::DoParse(value, &m, &l);
154 if (!ok)
155 {
156 is.setstate(std::ios_base::failbit);
157 }
158 size = QueueSize(m, l);
159 return is;
160}
161
162} // namespace ns3
uint32_t r
Class for representing queue sizes.
Definition queue-size.h:85
QueueSizeUnit GetUnit() const
Get the underlying unit.
uint32_t m_value
queue size [bytes or packets]
Definition queue-size.h:169
QueueSizeUnit m_unit
unit
Definition queue-size.h:168
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:194
#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