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
44{
47};
48
96{
97 public:
98 QueueSize();
106 QueueSize(QueueSizeUnit unit, uint32_t value);
124 QueueSize(std::string size);
125
131 bool operator<(const QueueSize& rhs) const;
132
138 bool operator<=(const QueueSize& rhs) const;
139
145 bool operator>(const QueueSize& rhs) const;
146
152 bool operator>=(const QueueSize& rhs) const;
153
159 bool operator==(const QueueSize& rhs) const;
160
166 bool operator!=(const QueueSize& rhs) const;
167
172 QueueSizeUnit GetUnit() const;
173
178 uint32_t GetValue() const;
179
180 private:
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
201};
202
210std::ostream& operator<<(std::ostream& os, const QueueSize& size);
211
219std::istream& operator>>(std::istream& is, QueueSize& size);
220
222
231template <typename Item>
232QueueSize operator+(const QueueSize& lhs, const Ptr<Item>& rhs);
241template <typename Item>
242QueueSize operator+(const Ptr<Item>& lhs, const QueueSize& rhs);
243
252template <typename Item>
253QueueSize operator-(const QueueSize& lhs, const Ptr<Item>& rhs);
262template <typename Item>
263QueueSize operator-(const Ptr<Item>& lhs, const QueueSize& rhs);
264
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:78
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:129
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153