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 
58 template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
60 {
61 public:
63  : m_value (0)
64  {}
65 
70  explicit SequenceNumber (NUMERIC_TYPE value)
71  : m_value (value)
72  {}
73 
79  : m_value (value.m_value)
80  {}
81 
88  {
89  m_value = value;
90  return *this;
91  }
92 
99  {
100  m_value = value.m_value;
101  return *this;
102  }
103 
104 #if 0
105  // a SequenceNumber implicitly converts to a plain number, but not the other way around
106  operator NUMERIC_TYPE () const
107  {
108  return m_value;
109  }
110 #endif
111 
116  NUMERIC_TYPE GetValue () const
117  {
118  return m_value;
119  }
120 
126  {
127  m_value++;
128  return *this;
129  }
130 
136  {
138  m_value++;
139  return retval;
140  }
141 
147  {
148  m_value--;
149  return *this;
150  }
151 
157  {
159  m_value--;
160  return retval;
161  }
162 
169  {
170  m_value += value;
171  return *this;
172  }
173 
180  {
181  m_value -= value;
182  return *this;
183  }
184 
191  {
193  }
194 
201  {
203  }
204 
211  {
213  }
214 
221  {
222  static const NUMERIC_TYPE maxValue = std::numeric_limits<NUMERIC_TYPE>::max ();
223  static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max () / 2;
224  if (m_value > other.m_value)
225  {
226  NUMERIC_TYPE diff = m_value - other.m_value;
227  if (diff < halfMaxValue)
228  {
229  return static_cast<SIGNED_TYPE> (diff);
230  }
231  else
232  {
233  // |------------|------------|
234  // ==== ===
235  // ^ ^
236  // other.m_value m_value
237  return -(static_cast<SIGNED_TYPE> (maxValue - m_value + 1 + other.m_value));
238  }
239  }
240  else
241  {
242  NUMERIC_TYPE diff = other.m_value - m_value;
243  if (diff < halfMaxValue)
244  {
245  // |------------|------------|
246  // ========
247  // ^ ^
248  // m_value other.m_value
249  return -(static_cast<SIGNED_TYPE> (diff));
250  }
251  else
252  {
253  // |------------|------------|
254  // ==== ===
255  // ^ ^
256  // m_value other.m_value
257  return static_cast<SIGNED_TYPE> (maxValue - other.m_value + 1 + m_value);
258  }
259  }
260  }
261 
275  {
276  static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max () / 2;
277 
278  return (((m_value > other.m_value) && (m_value - other.m_value) <= halfMaxValue)
279  || ((other.m_value > m_value) && (other.m_value - m_value) > halfMaxValue));
280  }
281 
288  {
289  return (m_value == other.m_value);
290  }
291 
298  {
299  return (m_value != other.m_value);
300  }
301 
307  bool operator <= (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
308  {
309  return (!this->operator> (other));
310  }
311 
318  {
319  return (this->operator> (other) || this->operator== (other));
320  }
321 
327  bool operator < (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
328  {
329  return !this->operator> (other) && m_value != other.m_value;
330  }
331 
338  template<typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
339  friend std::ostream & operator<< (std::ostream& os, const SequenceNumber<NUMERIC_TYPE2, SIGNED_TYPE2> &val);
340 
347  template<typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
348  friend std::istream & operator >> (std::istream &is, const SequenceNumber<NUMERIC_TYPE2, SIGNED_TYPE2> &val);
349 
350 private: // unimplemented operators
385  bool operator ! () const;
426  SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator<< (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
437  int operator* ();
438  //SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>* operator& ();
439 
440 private:
441  NUMERIC_TYPE m_value;
442 };
443 
444 
452 template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
453 std::ostream &
454 operator<< (std::ostream& os, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &val)
455 {
456  os << val.m_value;
457  return os;
458 }
459 
460 
468 template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
469 std::istream & operator >> (std::istream &is, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &val)
470 {
471  is >> val.m_value;
472  return is;
473 }
474 
490 
491 namespace TracedValueCallback {
492 
500 typedef void (* SequenceNumber32)(SequenceNumber32 oldValue,
501  SequenceNumber32 newValue);
502 
503 } // namespace TracedValueCallback
504 
505 } // namespace ns3
506 
507 #endif /* NS3_SEQ_NUM_H */
508 
509 
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.
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
SequenceNumber< uint16_t, int16_t > SequenceNumber16
16 bit 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 > &b) const
Modulo operator - unimplemented.
SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > operator^(const SequenceNumber< NUMERIC_TYPE, SIGNED_TYPE > &b) const
Bitwise XOR operator - unimplemented.
SequenceNumber< uint8_t, int8_t > SequenceNumber8
8 bit Sequence number.
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< 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< 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.