A Discrete-Event Network Simulator
API
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 
30 namespace ns3
31 {
32 
44 {
47 };
48 
95 class QueueSize
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 
200  uint32_t m_value;
201 };
202 
210 std::ostream& operator<<(std::ostream& os, const QueueSize& size);
211 
219 std::istream& operator>>(std::istream& is, QueueSize& size);
220 
222 
231 template <typename Item>
232 QueueSize operator+(const QueueSize& lhs, const Ptr<Item>& rhs);
241 template <typename Item>
242 QueueSize operator+(const Ptr<Item>& lhs, const QueueSize& rhs);
243 
252 template <typename Item>
253 QueueSize operator-(const QueueSize& lhs, const Ptr<Item>& rhs);
262 template <typename Item>
263 QueueSize operator-(const Ptr<Item>& lhs, const QueueSize& rhs);
264 
269 template <typename Item>
270 QueueSize
271 operator+(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 
284 template <typename Item>
285 QueueSize
286 operator+(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 
299 template <typename Item>
300 QueueSize
301 operator-(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 
316 template <typename Item>
317 QueueSize
318 operator-(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 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.
ATTRIBUTE_HELPER_HEADER(ValueClassTest)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159