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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Stefano Avallone <stavallo@unina.it>
7 */
8
9#ifndef QUEUE_SIZE_H
10#define QUEUE_SIZE_H
11
12#include "ns3/abort.h"
13#include "ns3/attribute-helper.h"
14#include "ns3/attribute.h"
15
16#include <iostream>
17#include <string>
18
19namespace ns3
20{
21
22/**
23 * @ingroup network
24 * @defgroup queuesize Queue size
25 */
26
27/**
28 * @ingroup queuesize
29 * @brief Enumeration of the operating modes of queues.
30 *
31 */
33{
34 PACKETS, /**< Use number of packets for queue size */
35 BYTES, /**< Use number of bytes for queue size */
36};
37
38/**
39 * @ingroup queuesize
40 * @brief Class for representing queue sizes
41 *
42 * Allows for natural and familiar use of queue sizes. Allows construction
43 * from strings, natural sum e.g.:
44 * @code
45 * QueueSize x("7kB");
46 * Ptr<Packet> p = Create<Packet> (1000);
47 * QueueSize y = x + p; // 8kB
48 * @endcode
49 * This class also supports the regular comparison operators \c <, \c >,
50 * \c <=, \c >=, \c ==, and \c !=
51 *
52 * Queue size specifiers consist of
53 * * A numeric value,
54 * * An optional multiplier prefix and
55 * * A unit.
56 *
57 * Whitespace is allowed but not required between the numeric value and
58 * multiplier or unit.
59 *
60 * Supported multiplier prefixes:
61 *
62 * | Prefix | Value |
63 * | :------- | ----------: |
64 * | "k", "K" | 1000 |
65 * | "Ki" | 1024 |
66 * | "M" | 1000000 |
67 * | "Mi" | 1024 Ki |
68 *
69 * Supported unit strings:
70 *
71 * | Symbol | Meaning |
72 * | :------- | :---------- |
73 * | "B" | 8-bit bytes |
74 * | "p" | packets |
75 *
76 * Examples:
77 * * "56kB" = 56,000 bytes
78 * * "128 kB" = 128,000 bytes
79 * * "8KiB" = 8,192 bytes
80 * * "1000p" = 1,000 packets
81 *
82 * @see attribute_QueueSize
83 */
85{
86 public:
87 QueueSize();
88 /**
89 * @brief Integer constructor
90 *
91 * Construct a queue size from a mode and a value.
92 * @param unit whether the value is expressed in terms of packets or bytes
93 * @param value the value
94 */
95 QueueSize(QueueSizeUnit unit, uint32_t value);
96 /**
97 * @brief String constructor
98 *
99 * Construct a queue size from a string. Many different unit strings are supported
100 * Supported unit strings:
101 * B, p \n
102 * kB, KB, KiB, kp, Kp, Kip \n
103 * MB, MiB, Mp, Mip \n
104 *
105 * Examples:
106 * "56kB" = 56,000 bytes
107 * "128 kB" = 128,000 bytes
108 * "8KiB" = 8,192 bytes
109 * "1000p" = 1,000 packets
110 *
111 * @param size string representing the size
112 */
113 QueueSize(std::string size);
114
115 /**
116 * @brief Equality operator.
117 * @param rhs the queue size to compare to this queue size
118 * @returns true if this size is equal to rhs
119 */
120 inline bool operator==(const QueueSize& rhs) const
121 {
122 NS_ABORT_MSG_IF(m_unit != rhs.m_unit, "Cannot compare heterogeneous sizes");
123 return m_value == rhs.m_value;
124 }
125
126 /**
127 * @brief Three-way comparison operator.
128 * @param rhs the queue size to compare to this queue size
129 * @returns The result of the comparison.
130 */
131 inline std::strong_ordering operator<=>(const QueueSize& rhs) const
132 {
133 NS_ABORT_MSG_IF(m_unit != rhs.m_unit, "Cannot compare heterogeneous sizes");
134 return m_value <=> rhs.m_value;
135 }
136
137 /**
138 * Get the underlying unit
139 * @return The underlying unit
140 */
141 QueueSizeUnit GetUnit() const;
142
143 /**
144 * Get the underlying value
145 * @return The underlying value
146 */
147 uint32_t GetValue() const;
148
149 private:
150 /**
151 * @brief Parse a string representing a QueueSize
152 *
153 * Allowed unit representations include all combinations of
154 *
155 * * An SI prefix: k, K, M
156 * * Bytes (8 bits) or packets
157 *
158 * @param [in] s The string representation, including unit
159 * @param [in,out] unit The location to put the unit.
160 * @param [in,out] value The location to put the value, in bytes or packets.
161 * @return true if parsing was successful.
162 */
163 static bool DoParse(const std::string s, QueueSizeUnit* unit, uint32_t* value);
164
165 // Uses DoParse
166 friend std::istream& operator>>(std::istream& is, QueueSize& size);
167
169 uint32_t m_value; //!< queue size [bytes or packets]
170};
171
172/**
173 * @brief Stream insertion operator.
174 *
175 * @param os the stream
176 * @param size the queue size
177 * @returns a reference to the stream
178 */
179std::ostream& operator<<(std::ostream& os, const QueueSize& size);
180
181/**
182 * @brief Stream extraction operator.
183 *
184 * @param is the stream
185 * @param size the queue size
186 * @returns a reference to the stream
187 */
188std::istream& operator>>(std::istream& is, QueueSize& size);
189
191
192/**
193 * Increase the queue size by a packet size, if the queue size is in bytes,
194 * or by one, otherwise.
195 *
196 * @param lhs queue size
197 * @param rhs packet
198 * @return the queue size increased by the packet size
199 */
200template <typename Item>
201QueueSize operator+(const QueueSize& lhs, const Ptr<Item>& rhs);
202/**
203 * Increase the queue size by a packet size, if the queue size is in bytes,
204 * or by one, otherwise.
205 *
206 * @param lhs packet
207 * @param rhs queue size
208 * @return the queue size increased by the packet size
209 */
210template <typename Item>
211QueueSize operator+(const Ptr<Item>& lhs, const QueueSize& rhs);
212
213/**
214 * Decrease the queue size by a packet size, if the queue size is in bytes,
215 * or by one, otherwise.
216 *
217 * @param lhs queue size
218 * @param rhs packet
219 * @return the queue size decreased by the packet size
220 */
221template <typename Item>
222QueueSize operator-(const QueueSize& lhs, const Ptr<Item>& rhs);
223/**
224 * Decrease the queue size by a packet size, if the queue size is in bytes,
225 * or by one, otherwise.
226 *
227 * @param lhs packet
228 * @param rhs queue size
229 * @return the queue size decreased by the packet size
230 */
231template <typename Item>
232QueueSize operator-(const Ptr<Item>& lhs, const QueueSize& rhs);
233
234/**
235 * Implementation of the templates declared above.
236 */
237
238template <typename Item>
240operator+(const QueueSize& lhs, const Ptr<Item>& rhs)
241{
242 if (lhs.GetUnit() == QueueSizeUnit::PACKETS)
243 {
244 return QueueSize(lhs.GetUnit(), lhs.GetValue() + 1);
245 }
246 if (lhs.GetUnit() == QueueSizeUnit::BYTES)
247 {
248 return QueueSize(lhs.GetUnit(), lhs.GetValue() + rhs->GetSize());
249 }
250 NS_FATAL_ERROR("Unknown queue size mode");
251}
252
253template <typename Item>
254QueueSize
255operator+(const Ptr<Item>& lhs, const QueueSize& rhs)
256{
257 if (rhs.GetUnit() == QueueSizeUnit::PACKETS)
258 {
259 return QueueSize(rhs.GetUnit(), rhs.GetValue() + 1);
260 }
261 if (rhs.GetUnit() == QueueSizeUnit::BYTES)
262 {
263 return QueueSize(rhs.GetUnit(), rhs.GetValue() + lhs->GetSize());
264 }
265 NS_FATAL_ERROR("Unknown queue size mode");
266}
267
268template <typename Item>
269QueueSize
270operator-(const QueueSize& lhs, const Ptr<Item>& rhs)
271{
272 if (lhs.GetUnit() == QueueSizeUnit::PACKETS)
273 {
274 NS_ABORT_IF(lhs.GetValue() < 1);
275 return QueueSize(lhs.GetUnit(), lhs.GetValue() - 1);
276 }
277 if (lhs.GetUnit() == QueueSizeUnit::BYTES)
278 {
279 NS_ABORT_IF(lhs.GetValue() < rhs->GetSize());
280 return QueueSize(lhs.GetUnit(), lhs.GetValue() - rhs->GetSize());
281 }
282 NS_FATAL_ERROR("Unknown queue size mode");
283}
284
285template <typename Item>
286QueueSize
287operator-(const Ptr<Item>& lhs, const QueueSize& rhs)
288{
289 if (rhs.GetUnit() == QueueSizeUnit::PACKETS)
290 {
291 NS_ABORT_IF(rhs.GetValue() < 1);
292 return QueueSize(rhs.GetUnit(), rhs.GetValue() - 1);
293 }
294 if (rhs.GetUnit() == QueueSizeUnit::BYTES)
295 {
296 NS_ABORT_IF(rhs.GetValue() < lhs->GetSize());
297 return QueueSize(rhs.GetUnit(), rhs.GetValue() - lhs->GetSize());
298 }
299 NS_FATAL_ERROR("Unknown queue size mode");
300}
301
302} // namespace ns3
303
304#endif /* QUEUE_SIZE_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Class for representing queue sizes.
Definition queue-size.h:85
friend std::istream & operator>>(std::istream &is, QueueSize &size)
Stream extraction operator.
QueueSizeUnit GetUnit() const
Get the underlying unit.
uint32_t m_value
queue size [bytes or packets]
Definition queue-size.h:169
bool operator==(const QueueSize &rhs) const
Equality operator.
Definition queue-size.h:120
std::strong_ordering operator<=>(const QueueSize &rhs) const
Three-way comparison operator.
Definition queue-size.h:131
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_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.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition abort.h:65
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition int64x64.h:91
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition int64x64.h:76
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