A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
queue-size.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Universita' degli Studi di Napoli Federico II
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Stefano Avallone <stavallo@unina.it>
18 */
19
20#ifndef QUEUE_SIZE_H
21#define QUEUE_SIZE_H
22
23#include "ns3/abort.h"
24#include "ns3/attribute-helper.h"
25#include "ns3/attribute.h"
26
27#include <iostream>
28#include <string>
29
30namespace ns3
31{
32
33/**
34 * \ingroup network
35 * \defgroup queuesize Queue size
36 */
37
38/**
39 * \ingroup queuesize
40 * \brief Enumeration of the operating modes of queues.
41 *
42 */
44{
45 PACKETS, /**< Use number of packets for queue size */
46 BYTES, /**< Use number of bytes for queue size */
47};
48
49/**
50 * \ingroup queuesize
51 * \brief Class for representing queue sizes
52 *
53 * Allows for natural and familiar use of queue sizes. Allows construction
54 * from strings, natural sum e.g.:
55 * \code
56 * QueueSize x("7kB");
57 * Ptr<Packet> p = Create<Packet> (1000);
58 * QueueSize y = x + p; // 8kB
59 * \endcode
60 * This class also supports the regular comparison operators \c <, \c >,
61 * \c <=, \c >=, \c ==, and \c !=
62 *
63 * Queue size specifiers consist of
64 * * A numeric value,
65 * * An optional multiplier prefix and
66 * * A unit.
67 *
68 * Whitespace is allowed but not required between the numeric value and
69 * multiplier or unit.
70 *
71 * Supported multiplier prefixes:
72 *
73 * | Prefix | Value |
74 * | :------- | ----------: |
75 * | "k", "K" | 1000 |
76 * | "Ki" | 1024 |
77 * | "M" | 1000000 |
78 * | "Mi" | 1024 Ki |
79 *
80 * Supported unit strings:
81 *
82 * | Symbol | Meaning |
83 * | :------- | :---------- |
84 * | "B" | 8-bit bytes |
85 * | "p" | packets |
86 *
87 * Examples:
88 * * "56kB" = 56,000 bytes
89 * * "128 kB" = 128,000 bytes
90 * * "8KiB" = 8,192 bytes
91 * * "1000p" = 1,000 packets
92 *
93 * \see attribute_QueueSize
94 */
96{
97 public:
98 QueueSize();
99 /**
100 * \brief Integer constructor
101 *
102 * Construct a queue size from a mode and a value.
103 * \param unit whether the value is expressed in terms of packets or bytes
104 * \param value the value
105 */
106 QueueSize(QueueSizeUnit unit, uint32_t value);
107 /**
108 * \brief String constructor
109 *
110 * Construct a queue size from a string. Many different unit strings are supported
111 * Supported unit strings:
112 * B, p \n
113 * kB, KB, KiB, kp, Kp, Kip \n
114 * MB, MiB, Mp, Mip \n
115 *
116 * Examples:
117 * "56kB" = 56,000 bytes
118 * "128 kB" = 128,000 bytes
119 * "8KiB" = 8,192 bytes
120 * "1000p" = 1,000 packets
121 *
122 * \param size string representing the size
123 */
124 QueueSize(std::string size);
125
126 /**
127 * \return true if this size is less than rhs
128 *
129 * \param rhs the queue size to compare to this queue size
130 */
131 bool operator<(const QueueSize& rhs) const;
132
133 /**
134 * \return true if this size is less than or equal to rhs
135 *
136 * \param rhs the queue size to compare to this queue size
137 */
138 bool operator<=(const QueueSize& rhs) const;
139
140 /**
141 * \return true if this size is greater than rhs
142 *
143 * \param rhs the queue size to compare to this queue size
144 */
145 bool operator>(const QueueSize& rhs) const;
146
147 /**
148 * \return true if this size is greater than or equal to rhs
149 *
150 * \param rhs the queue size to compare to this queue size
151 */
152 bool operator>=(const QueueSize& rhs) const;
153
154 /**
155 * \return true if this size is equal to rhs
156 *
157 * \param rhs the queue size to compare to this queue size
158 */
159 bool operator==(const QueueSize& rhs) const;
160
161 /**
162 * \return true if this size is not equal to rhs
163 *
164 * \param rhs the queue size to compare to this queue size
165 */
166 bool operator!=(const QueueSize& rhs) const;
167
168 /**
169 * Get the underlying unit
170 * \return The underlying unit
171 */
172 QueueSizeUnit GetUnit() const;
173
174 /**
175 * Get the underlying value
176 * \return The underlying value
177 */
178 uint32_t GetValue() const;
179
180 private:
181 /**
182 * \brief Parse a string representing a QueueSize
183 *
184 * Allowed unit representations include all combinations of
185 *
186 * * An SI prefix: k, K, M
187 * * Bytes (8 bits) or packets
188 *
189 * \param [in] s The string representation, including unit
190 * \param [in,out] unit The location to put the unit.
191 * \param [in,out] value The location to put the value, in bytes or packets.
192 * \return true if parsing was successful.
193 */
194 static bool DoParse(const std::string s, QueueSizeUnit* unit, uint32_t* value);
195
196 // Uses DoParse
197 friend std::istream& operator>>(std::istream& is, QueueSize& size);
198
200 uint32_t m_value; //!< queue size [bytes or packets]
201};
202
203/**
204 * \brief Stream insertion operator.
205 *
206 * \param os the stream
207 * \param size the queue size
208 * \returns a reference to the stream
209 */
210std::ostream& operator<<(std::ostream& os, const QueueSize& size);
211
212/**
213 * \brief Stream extraction operator.
214 *
215 * \param is the stream
216 * \param size the queue size
217 * \returns a reference to the stream
218 */
219std::istream& operator>>(std::istream& is, QueueSize& size);
220
222
223/**
224 * Increase the queue size by a packet size, if the queue size is in bytes,
225 * or by one, otherwise.
226 *
227 * \param lhs queue size
228 * \param rhs packet
229 * \return the queue size increased by the packet size
230 */
231template <typename Item>
232QueueSize operator+(const QueueSize& lhs, const Ptr<Item>& rhs);
233/**
234 * Increase the queue size by a packet size, if the queue size is in bytes,
235 * or by one, otherwise.
236 *
237 * \param lhs packet
238 * \param rhs queue size
239 * \return the queue size increased by the packet size
240 */
241template <typename Item>
242QueueSize operator+(const Ptr<Item>& lhs, const QueueSize& rhs);
243
244/**
245 * Decrease the queue size by a packet size, if the queue size is in bytes,
246 * or by one, otherwise.
247 *
248 * \param lhs queue size
249 * \param rhs packet
250 * \return the queue size decreased by the packet size
251 */
252template <typename Item>
253QueueSize operator-(const QueueSize& lhs, const Ptr<Item>& rhs);
254/**
255 * Decrease the queue size by a packet size, if the queue size is in bytes,
256 * or by one, otherwise.
257 *
258 * \param lhs packet
259 * \param rhs queue size
260 * \return the queue size decreased by the packet size
261 */
262template <typename Item>
263QueueSize operator-(const Ptr<Item>& lhs, const QueueSize& rhs);
264
265/**
266 * Implementation of the templates declared above.
267 */
268
269template <typename Item>
271operator+(const QueueSize& lhs, const Ptr<Item>& rhs)
272{
273 if (lhs.GetUnit() == QueueSizeUnit::PACKETS)
274 {
275 return QueueSize(lhs.GetUnit(), lhs.GetValue() + 1);
276 }
277 if (lhs.GetUnit() == QueueSizeUnit::BYTES)
278 {
279 return QueueSize(lhs.GetUnit(), lhs.GetValue() + rhs->GetSize());
280 }
281 NS_FATAL_ERROR("Unknown queue size mode");
282}
283
284template <typename Item>
285QueueSize
286operator+(const Ptr<Item>& lhs, const QueueSize& rhs)
287{
288 if (rhs.GetUnit() == QueueSizeUnit::PACKETS)
289 {
290 return QueueSize(rhs.GetUnit(), rhs.GetValue() + 1);
291 }
292 if (rhs.GetUnit() == QueueSizeUnit::BYTES)
293 {
294 return QueueSize(rhs.GetUnit(), rhs.GetValue() + lhs->GetSize());
295 }
296 NS_FATAL_ERROR("Unknown queue size mode");
297}
298
299template <typename Item>
300QueueSize
301operator-(const QueueSize& lhs, const Ptr<Item>& rhs)
302{
303 if (lhs.GetUnit() == QueueSizeUnit::PACKETS)
304 {
305 NS_ABORT_IF(lhs.GetValue() < 1);
306 return QueueSize(lhs.GetUnit(), lhs.GetValue() - 1);
307 }
308 if (lhs.GetUnit() == QueueSizeUnit::BYTES)
309 {
310 NS_ABORT_IF(lhs.GetValue() < rhs->GetSize());
311 return QueueSize(lhs.GetUnit(), lhs.GetValue() - rhs->GetSize());
312 }
313 NS_FATAL_ERROR("Unknown queue size mode");
314}
315
316template <typename Item>
317QueueSize
318operator-(const Ptr<Item>& lhs, const QueueSize& rhs)
319{
320 if (rhs.GetUnit() == QueueSizeUnit::PACKETS)
321 {
322 NS_ABORT_IF(rhs.GetValue() < 1);
323 return QueueSize(rhs.GetUnit(), rhs.GetValue() - 1);
324 }
325 if (rhs.GetUnit() == QueueSizeUnit::BYTES)
326 {
327 NS_ABORT_IF(rhs.GetValue() < lhs->GetSize());
328 return QueueSize(rhs.GetUnit(), rhs.GetValue() - lhs->GetSize());
329 }
330 NS_FATAL_ERROR("Unknown queue size mode");
331}
332
333} // namespace ns3
334
335#endif /* QUEUE_SIZE_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Class for representing queue sizes.
Definition: queue-size.h:96
bool operator>(const QueueSize &rhs) const
Definition: queue-size.cc:144
friend std::istream & operator>>(std::istream &is, QueueSize &size)
Stream extraction operator.
Definition: queue-size.cc:206
bool operator<(const QueueSize &rhs) const
Definition: queue-size.cc:128
bool operator<=(const QueueSize &rhs) const
Definition: queue-size.cc:136
QueueSizeUnit GetUnit() const
Get the underlying unit.
Definition: queue-size.cc:176
bool operator!=(const QueueSize &rhs) const
Definition: queue-size.cc:168
uint32_t m_value
queue size [bytes or packets]
Definition: queue-size.h:200
bool operator>=(const QueueSize &rhs) const
Definition: queue-size.cc:152
bool operator==(const QueueSize &rhs) const
Definition: queue-size.cc:160
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:33
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:183
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:76
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition: int64x64.h:103
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition: int64x64.h:88
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:44
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:46
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:45
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:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183