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