A Discrete-Event Network Simulator
API
sequence-number.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2008-2010 INESC Porto
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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
18 //
19 
20 #ifndef NS3_SEQ_NUM_H
21 #define NS3_SEQ_NUM_H
22 
23 #include "ns3/type-name.h"
24 
25 #include <iostream>
26 #include <limits>
27 #include <stdint.h>
28 
29 namespace ns3
30 {
31 
60 template <typename NUMERIC_TYPE, typename SIGNED_TYPE>
62 {
63  public:
65  : m_value(0)
66  {
67  }
68 
73  explicit SequenceNumber(NUMERIC_TYPE value)
74  : m_value(value)
75  {
76  }
77 
83  : m_value(value.m_value)
84  {
85  }
86 
93  {
94  m_value = value;
95  return *this;
96  }
97 
105  {
106  m_value = value.m_value;
107  return *this;
108  }
109 
110 #if 0
111  // a SequenceNumber implicitly converts to a plain number, but not the other way around
112  operator NUMERIC_TYPE () const
113  {
114  return m_value;
115  }
116 #endif
117 
122  NUMERIC_TYPE GetValue() const
123  {
124  return m_value;
125  }
126 
132  {
133  m_value++;
134  return *this;
135  }
136 
142  {
144  m_value++;
145  return retval;
146  }
147 
153  {
154  m_value--;
155  return *this;
156  }
157 
163  {
165  m_value--;
166  return retval;
167  }
168 
175  {
176  m_value += value;
177  return *this;
178  }
179 
186  {
187  m_value -= value;
188  return *this;
189  }
190 
198  {
200  }
201 
208  {
210  }
211 
218  {
220  }
221 
227  SIGNED_TYPE operator-(const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& other) const
228  {
229  static const NUMERIC_TYPE maxValue = std::numeric_limits<NUMERIC_TYPE>::max();
230  static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max() / 2;
231  if (m_value > other.m_value)
232  {
233  NUMERIC_TYPE diff = m_value - other.m_value;
234  if (diff < halfMaxValue)
235  {
236  return static_cast<SIGNED_TYPE>(diff);
237  }
238  else
239  {
240  // |------------|------------|
241  // ==== ===
242  // ^ ^
243  // other.m_value m_value
244  return -(static_cast<SIGNED_TYPE>(maxValue - m_value + 1 + other.m_value));
245  }
246  }
247  else
248  {
249  NUMERIC_TYPE diff = other.m_value - m_value;
250  if (diff < halfMaxValue)
251  {
252  // |------------|------------|
253  // ========
254  // ^ ^
255  // m_value other.m_value
256  return -(static_cast<SIGNED_TYPE>(diff));
257  }
258  else
259  {
260  // |------------|------------|
261  // ==== ===
262  // ^ ^
263  // m_value other.m_value
264  return static_cast<SIGNED_TYPE>(maxValue - other.m_value + 1 + m_value);
265  }
266  }
267  }
268 
282  {
283  static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max() / 2;
284 
285  return (((m_value > other.m_value) && (m_value - other.m_value) <= halfMaxValue) ||
286  ((other.m_value > m_value) && (other.m_value - m_value) > halfMaxValue));
287  }
288 
295  {
296  return m_value == other.m_value;
297  }
298 
305  {
306  return m_value != other.m_value;
307  }
308 
315  {
316  return (!this->operator>(other));
317  }
318 
325  {
326  return (this->operator>(other) || this->operator==(other));
327  }
328 
335  {
336  return !this->operator>(other) && m_value != other.m_value;
337  }
338 
345  template <typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
346  friend std::ostream& operator<<(std::ostream& os,
348 
355  template <typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
356  friend std::istream& operator>>(std::istream& is,
358 
359  public:
360  // Unimplemented operators
366  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
368  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
370  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
371  bool operator!() const = delete;
376  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
378  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
380  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
382  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
384  const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>&) const = delete;
385  int operator*() = delete;
386  // SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>* operator& () = delete;
387 
388  private:
389  NUMERIC_TYPE m_value;
390 };
391 
399 template <typename NUMERIC_TYPE, typename SIGNED_TYPE>
400 std::ostream&
401 operator<<(std::ostream& os, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& val)
402 {
403  os << val.m_value;
404  return os;
405 }
406 
414 template <typename NUMERIC_TYPE, typename SIGNED_TYPE>
415 std::istream&
417 {
418  is >> val.m_value;
419  return is;
420 }
421 
437 
438 namespace TracedValueCallback
439 {
440 
448 typedef void (*SequenceNumber32)(SequenceNumber32 oldValue, SequenceNumber32 newValue);
449 
450 } // namespace TracedValueCallback
451 
459 
460 } // namespace ns3
461 
462 #endif /* NS3_SEQ_NUM_H */
#define max(a, b)
Definition: 80211b.c:42
Generic "sequence number" class.
bool operator<(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Less than operator for comparing sequence numbers.
int operator*()=delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator%(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
bool operator>(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Here is the critical part, how the comparison is made taking into account wrap-around.
bool operator&&(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator&(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator+=(SIGNED_TYPE value)
Plus equals operator.
bool operator<=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Less than or equal operator for comparing sequence numbers.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator~() const =delete
friend std::istream & operator>>(std::istream &is, const SequenceNumber< NUMERIC_TYPE2, SIGNED_TYPE2 > &val)
For loading sequence number from input streams.
bool operator!() const =delete
bool operator!=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Inequality operator for comparing sequence numbers.
SequenceNumber(NUMERIC_TYPE value)
Constructs a SequenceNumber with the given value.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator++(int)
Postfix increment operator.
SIGNED_TYPE operator-(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Subtraction operator for subtracting sequence number from sequence number.
bool operator==(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Equality operator for comparing sequence number.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator>>(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator=(NUMERIC_TYPE value)
Constructs a SequenceNumber from an assignment of given value.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator+=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &)=delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator--()
Prefix decrement operator.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator-=(SIGNED_TYPE value)
Minus equals operator.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator-=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &)=delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator--(int)
Postfix decrement operator.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator^(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator+(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Operator defining addition of two sequence numbers.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator+(SIGNED_TYPE delta) const
Addition operator for adding numeric value to sequence number.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator|(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator-(SIGNED_TYPE delta) const
Subtraction operator for subtracting numeric value from sequence number.
friend std::ostream & operator<<(std::ostream &os, const SequenceNumber< NUMERIC_TYPE2, SIGNED_TYPE2 > &val)
For printing sequence number.
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator/(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
bool operator||(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
SequenceNumber(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &value)
Constructs a SequenceNumber from a copy.
NUMERIC_TYPE m_value
Sequence number value.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator++()
Prefix increment operator.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &value)
Constructs a SequenceNumber from an assignment of another sequence number.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator*(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &) const =delete
bool operator>=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Greater than or equal operator for comparing sequence numbers.
SequenceNumber< uint8_t, int8_t > SequenceNumber8
8 bit Sequence number.
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
SequenceNumber< uint16_t, int16_t > SequenceNumber16
16 bit Sequence number.
TYPENAMEGET_DEFINE(Time)
ns3::TypeNameGet<Time>() specialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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