A Discrete-Event Network Simulator
API
sequence-number.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2008-2010 INESC Porto
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
19 //
20 
21 #ifndef NS3_SEQ_NUM_H
22 #define NS3_SEQ_NUM_H
23 
24 #include <limits>
25 #include <iostream>
26 #include <stdint.h>
27 
28 namespace ns3 {
29 
52 template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
54 {
55 public:
57  : m_value (0)
58  {}
59 
64  explicit SequenceNumber (NUMERIC_TYPE value)
65  : m_value (value)
66  {}
67 
73  : m_value (value.m_value)
74  {}
75 
82  {
83  m_value = value;
84  return *this;
85  }
86 
93  {
94  m_value = value.m_value;
95  return *this;
96  }
97 
98 #if 0
99  // a SequenceNumber implicitly converts to a plain number, but not the other way around
100  operator NUMERIC_TYPE () const
101  {
102  return m_value;
103  }
104 #endif
105 
110  NUMERIC_TYPE GetValue () const
111  {
112  return m_value;
113  }
114 
120  {
121  m_value++;
122  return *this;
123  }
124 
130  {
132  m_value++;
133  return retval;
134  }
135 
141  {
142  m_value--;
143  return *this;
144  }
145 
151  {
153  m_value--;
154  return retval;
155  }
156 
163  {
164  m_value += value;
165  return *this;
166  }
167 
174  {
175  m_value -= value;
176  return *this;
177  }
178 
185  {
187  }
188 
195  {
197  }
198 
205  {
207  }
208 
215  {
216  static const NUMERIC_TYPE maxValue = std::numeric_limits<NUMERIC_TYPE>::max ();
217  static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max () / 2;
218  if (m_value > other.m_value)
219  {
220  NUMERIC_TYPE diff = m_value - other.m_value;
221  if (diff < halfMaxValue)
222  {
223  return static_cast<SIGNED_TYPE> (diff);
224  }
225  else
226  {
227  // |------------|------------|
228  // ==== ===
229  // ^ ^
230  // other.m_value m_value
231  return -(static_cast<SIGNED_TYPE> (maxValue - m_value + 1 + other.m_value));
232  }
233  }
234  else
235  {
236  NUMERIC_TYPE diff = other.m_value - m_value;
237  if (diff < halfMaxValue)
238  {
239  // |------------|------------|
240  // ========
241  // ^ ^
242  // m_value other.m_value
243  return -(static_cast<SIGNED_TYPE> (diff));
244  }
245  else
246  {
247  // |------------|------------|
248  // ==== ===
249  // ^ ^
250  // m_value other.m_value
251  return static_cast<SIGNED_TYPE> (maxValue - other.m_value + 1 + m_value);
252  }
253  }
254  }
255 
269  {
270  static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max () / 2;
271 
272  return (((m_value > other.m_value) && (m_value - other.m_value) <= halfMaxValue)
273  || ((other.m_value > m_value) && (other.m_value - m_value) > halfMaxValue));
274  }
275 
282  {
283  return (m_value == other.m_value);
284  }
285 
292  {
293  return (m_value != other.m_value);
294  }
295 
301  bool operator <= (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
302  {
303  return (!this->operator> (other));
304  }
305 
312  {
313  return (this->operator> (other) || this->operator== (other));
314  }
315 
321  bool operator < (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
322  {
323  return !this->operator> (other) && m_value != other.m_value;
324  }
325 
332  template<typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
333  friend std::ostream & operator<< (std::ostream& os, const SequenceNumber<NUMERIC_TYPE2, SIGNED_TYPE2> &val);
334 
341  template<typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
342  friend std::istream & operator >> (std::istream &is, const SequenceNumber<NUMERIC_TYPE2, SIGNED_TYPE2> &val);
343 
344 private: // unimplemented operators
379  bool operator ! () const;
420  SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator<< (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
431  int operator* ();
432  //SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>* operator& ();
433 
434 private:
435  NUMERIC_TYPE m_value;
436 };
437 
438 
446 template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
447 std::ostream &
448 operator<< (std::ostream& os, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &val)
449 {
450  os << val.m_value;
451  return os;
452 }
453 
454 
462 template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
463 std::istream & operator >> (std::istream &is, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &val)
464 {
465  is >> val.m_value;
466  return is;
467 }
468 
484 
485 namespace TracedValueCallback {
486 
494 typedef void (* SequenceNumber32)(SequenceNumber32 oldValue,
495  SequenceNumber32 newValue);
496 
497 } // namespace TracedValueCallback
498 
499 } // namespace ns3
500 
501 #endif /* NS3_SEQ_NUM_H */
502 
503 
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
bool operator!() const
Logical NOT operator - unimplemented.
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 > &b) const
Modulo operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator^(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &b) const
Bitwise XOR operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator-(SIGNED_TYPE delta) const
Subtraction operator for subtracting numeric value from sequence number.
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 > &b) const
Logical OR operator - unimplemented.
#define max(a, b)
Definition: 80211b.c:43
SequenceNumber< uint16_t, int16_t > SequenceNumber16
16 bit Sequence number.
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=(NUMERIC_TYPE value)
Constructs a SequenceNumber from an assignment of given value.
SequenceNumber(SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > const &value)
Constructs a SequenceNumber from a copy.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Generic "sequence number" class.
int operator*()
Indirection operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator~() const
Bitwise NOT operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator/(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &b) const
Division operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator+=(SIGNED_TYPE value)
Plus equals operator.
bool operator!=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Inequality operator for comparing sequence numbers.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator++()
Prefix increment operator.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator &(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &b) const
Bitwise AND operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator|(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &b) const
Bitwise OR operator - unimplemented.
friend std::istream & operator>>(std::istream &is, const SequenceNumber< NUMERIC_TYPE2, SIGNED_TYPE2 > &val)
For loading sequence number from input streams.
SequenceNumber< uint8_t, int8_t > SequenceNumber8
8 bit Sequence number.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator--()
Prefix decrement operator.
SequenceNumber(NUMERIC_TYPE value)
Constructs a SequenceNumber with the given value.
bool operator>=(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &other) const
Greater than or equal operator for comparing sequence numbers.
NUMERIC_TYPE m_value
Sequence number value.
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > & operator-=(SIGNED_TYPE value)
Minus equals operator.
bool operator&&(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &b) const
Logical AND operator - unimplemented.
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.