A Discrete-Event Network Simulator
API
queue-size.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2018 Universita' degli Studi di Napoli Federico II
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Stefano Avallone <stavallo@unina.it>
19 //
20 
21 #include "queue-size.h"
22 #include "ns3/log.h"
23 
24 namespace ns3 {
25 
26 NS_LOG_COMPONENT_DEFINE ("QueueSize");
27 
29 
30 /* static */
31 bool
32 QueueSize::DoParse (const std::string s, QueueSizeUnit *unit, uint32_t *value)
33 {
34  NS_LOG_FUNCTION (s << unit << value);
35  std::string::size_type n = s.find_first_not_of ("0123456789.");
36  if (n != std::string::npos)
37  { // Found non-numeric
38  std::istringstream iss;
39  iss.str (s.substr (0, n));
40  double r;
41  iss >> r;
42  std::string trailer = s.substr (n, std::string::npos);
43  if (trailer == "B")
44  {
45  // bytes
46  *unit = QueueSizeUnit::BYTES;
47  *value = static_cast<uint32_t>(r);
48  }
49  else if (trailer == "kB" || trailer == "KB")
50  {
51  // kilobytes
52  *unit = QueueSizeUnit::BYTES;
53  *value = static_cast<uint32_t>(r * 1000);
54  }
55  else if (trailer == "KiB")
56  {
57  // kibibytes
58  *unit = QueueSizeUnit::BYTES;
59  *value = static_cast<uint32_t>(r * 1024);
60  }
61  else if (trailer == "MB")
62  {
63  // MegaBytes
64  *unit = QueueSizeUnit::BYTES;
65  *value = static_cast<uint32_t>(r * 1000000);
66  }
67  else if (trailer == "MiB")
68  {
69  // MebiBytes
70  *unit = QueueSizeUnit::BYTES;
71  *value = static_cast<uint32_t>(r * 1048576);
72  }
73  else if (trailer == "p")
74  {
75  // packets
76  *unit = QueueSizeUnit::PACKETS;
77  *value = static_cast<uint32_t>(r);
78  }
79  else if (trailer == "kp" || trailer == "Kp")
80  {
81  // kilopackets
82  *unit = QueueSizeUnit::PACKETS;
83  *value = static_cast<uint32_t>(r * 1000);
84  }
85  else if (trailer == "Kip")
86  {
87  // kibipackets
88  *unit = QueueSizeUnit::PACKETS;
89  *value = static_cast<uint32_t>(r * 1024);
90  }
91  else if (trailer == "Mp")
92  {
93  // MegaPackets
94  *unit = QueueSizeUnit::PACKETS;
95  *value = static_cast<uint32_t>(r * 1000000);
96  }
97  else if (trailer == "Mip")
98  {
99  // MebiPackets
100  *unit = QueueSizeUnit::PACKETS;
101  *value = static_cast<uint32_t>(r * 1048576);
102  }
103  else
104  {
105  return false; // unsupported unit string
106  }
107  return true;
108  }
109  return false; // a unit string is required
110 }
111 
113  : m_unit (QueueSizeUnit::PACKETS),
114  m_value (0)
115 {
116  NS_LOG_FUNCTION (this);
117 }
118 
119 QueueSize::QueueSize (QueueSizeUnit unit, uint32_t value)
120  : m_unit (unit),
121  m_value (value)
122 {
123  NS_LOG_FUNCTION (this << static_cast<uint16_t>(unit) << value);
124 }
125 
126 bool QueueSize::operator < (const QueueSize& rhs) const
127 {
128  NS_ABORT_MSG_IF (m_unit != rhs.GetUnit (), "Cannot compare heterogeneous sizes");
129 
130  return m_value<rhs.m_value;
131 }
132 
133 bool QueueSize::operator <= (const QueueSize& rhs) const
134 {
135  NS_ABORT_MSG_IF (m_unit != rhs.GetUnit (), "Cannot compare heterogeneous sizes");
136 
137  return m_value<=rhs.m_value;
138 }
139 
140 bool QueueSize::operator > (const QueueSize& rhs) const
141 {
142  NS_ABORT_MSG_IF (m_unit != rhs.GetUnit (), "Cannot compare heterogeneous sizes");
143 
144  return m_value>rhs.m_value;
145 }
146 
147 bool QueueSize::operator >= (const QueueSize& rhs) const
148 {
149  NS_ABORT_MSG_IF (m_unit != rhs.GetUnit (), "Cannot compare heterogeneous sizes");
150 
151  return m_value>=rhs.m_value;
152 }
153 
154 bool QueueSize::operator == (const QueueSize& rhs) const
155 {
156  NS_ABORT_MSG_IF (m_unit != rhs.GetUnit (), "Cannot compare heterogeneous sizes");
157 
158  return m_value==rhs.m_value;
159 }
160 
161 bool QueueSize::operator != (const QueueSize& rhs) const
162 {
163  NS_ABORT_MSG_IF (m_unit != rhs.GetUnit (), "Cannot compare heterogeneous sizes");
164 
165  return m_value!=rhs.m_value;
166 }
167 
169 {
170  NS_LOG_FUNCTION (this);
171  return m_unit;
172 }
173 
174 uint32_t QueueSize::GetValue () const
175 {
176  NS_LOG_FUNCTION (this);
177  return m_value;
178 }
179 
180 QueueSize::QueueSize (std::string size)
181 {
182  NS_LOG_FUNCTION (this << size);
183  [[maybe_unused]] bool ok = DoParse (size, &m_unit, &m_value);
184  NS_ABORT_MSG_IF (!ok, "Could not parse queue size: " << size);
185 }
186 
187 /* For printing of queue size */
188 std::ostream &operator << (std::ostream &os, const QueueSize &size)
189 {
190  os << size.GetValue () << (size.GetUnit () == QueueSizeUnit::PACKETS ? "p" : "B");
191  return os;
192 }
193 /* Initialize a queue size from an input stream */
194 std::istream &operator >> (std::istream &is, QueueSize &size)
195 {
196  std::string value;
197  is >> value;
198  QueueSizeUnit m;
199  uint32_t l;
200  bool ok = QueueSize::DoParse (value, &m, &l);
201  if (!ok)
202  {
203  is.setstate (std::ios_base::failbit);
204  }
205  size = QueueSize (m, l);
206  return is;
207 }
208 
209 } // namespace ns3
Class for representing queue sizes.
Definition: queue-size.h:95
bool operator>(const QueueSize &rhs) const
Definition: queue-size.cc:140
bool operator<(const QueueSize &rhs) const
Definition: queue-size.cc:126
bool operator<=(const QueueSize &rhs) const
Definition: queue-size.cc:133
QueueSizeUnit GetUnit() const
Get the underlying unit.
Definition: queue-size.cc:168
bool operator!=(const QueueSize &rhs) const
Definition: queue-size.cc:161
uint32_t m_value
queue size [bytes or packets]
Definition: queue-size.h:200
bool operator>=(const QueueSize &rhs) const
Definition: queue-size.cc:147
bool operator==(const QueueSize &rhs) const
Definition: queue-size.cc:154
QueueSizeUnit m_unit
unit
Definition: queue-size.h:199
static bool DoParse(const std::string s, QueueSizeUnit *unit, uint32_t *value)
Parse a string representing a QueueSize.
Definition: queue-size.cc:32
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:174
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:43
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:45
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139