/usr/include/ns3.17/ns3/sequence-number.h is in libns3-dev 3.17+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
//
// Copyright (c) 2008-2010 INESC Porto
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
//
#ifndef NS3_SEQ_NUM_H
#define NS3_SEQ_NUM_H
#include <limits>
#include <iostream>
#include <stdint.h>
namespace ns3 {
/**
* \brief Generic "sequence number" class
*
* This class can be used to handle sequence numbers. In networking
* protocols, sequence numbers are fixed precision integer numbers
* that are used to order events relative to each other. A sequence
* number is expected to increase over time but, since it has a
* limited number of bits, the number will "wrap around" from the
* maximum value that can represented with the given number of bits
* back to zero. For this reason, comparison of two sequence numbers,
* and subtraction, is non-trivial. The SequenceNumber class behaves
* like a number, with the usual arythmetic operators implemented, but
* knows how to correctly compare and subtract sequence numbers.
*
* This is a templated class. To use it you need to supply two
* fundamental types as template parameters: NUMERIC_TYPE and
* SIGNED_TYPE. For instance, SequenceNumber<uint32_t, int32_t> gives
* you a 32-bit sequence number, while SequenceNumber<uint16_t,
* int16_t> is a 16-bit one. For your convenience, these are
* typedef'ed as SequenceNumber32 and SequenceNumber16, respectively.
*/
template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
class SequenceNumber
{
public:
SequenceNumber ()
: m_value (0)
{}
/**
* \brief Constructs a SequenceNumber with the given value
* \param value the sequence number value
*/
explicit SequenceNumber (NUMERIC_TYPE value)
: m_value (value)
{}
// copy contructor
SequenceNumber (SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> const &value)
: m_value (value.m_value)
{}
// assignment from a plain number
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& operator= (NUMERIC_TYPE value)
{
m_value = value;
return *this;
}
// assignment from a sequence number
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& operator= (SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> const &value)
{
m_value = value.m_value;
return *this;
}
#if 0
// a SequenceNumber implicitly converts to a plain number, but not the other way around
operator NUMERIC_TYPE () const
{
return m_value;
}
#endif
/**
* \brief Extracts the numeric value of the sequence number
* \returns the sequence number value
*/
NUMERIC_TYPE GetValue () const
{
return m_value;
}
// prefix ++
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator++ ()
{
m_value++;
return *this;
}
// postfix ++
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator++ (int)
{
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> retval (m_value);
m_value++;
return retval;
}
// prefix --
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator-- ()
{
m_value--;
return *this;
}
// postfix --
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator-- (int)
{
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> retval (m_value);
m_value--;
return retval;
}
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& operator+= (SIGNED_TYPE value)
{
m_value += value;
return *this;
}
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& operator-= (SIGNED_TYPE value)
{
m_value -= value;
return *this;
}
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator + (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
return SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> (m_value + other.m_value);
}
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator + (SIGNED_TYPE delta) const
{
return SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> (m_value + delta);
}
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator - (SIGNED_TYPE delta) const
{
return SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> (m_value - delta);
}
SIGNED_TYPE operator - (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
static const NUMERIC_TYPE maxValue = std::numeric_limits<NUMERIC_TYPE>::max ();
static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max () / 2;
if (m_value > other.m_value)
{
NUMERIC_TYPE diff = m_value - other.m_value;
if (diff < halfMaxValue)
{
return static_cast<SIGNED_TYPE> (diff);
}
else
{
// |------------|------------|
// ==== ===
// ^ ^
// other.m_value m_value
return -(static_cast<SIGNED_TYPE> (maxValue - m_value + 1 + other.m_value));
}
}
else
{
NUMERIC_TYPE diff = other.m_value - m_value;
if (diff < halfMaxValue)
{
// |------------|------------|
// ========
// ^ ^
// m_value other.m_value
return -(static_cast<SIGNED_TYPE> (diff));
}
else
{
// |------------|------------|
// ==== ===
// ^ ^
// m_value other.m_value
return static_cast<SIGNED_TYPE> (maxValue - other.m_value + 1 + m_value);
}
}
}
// Here is the critical part, how the comparison is made taking into
// account wrap-around. From RFC 3626:
//
// The sequence number S1 is said to be "greater than" the sequence
// number S2 if:
//
// S1 > S2 AND S1 - S2 <= MAXVALUE/2 OR
//
// S2 > S1 AND S2 - S1 > MAXVALUE/2
bool operator > (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
static const NUMERIC_TYPE halfMaxValue = std::numeric_limits<NUMERIC_TYPE>::max () / 2;
return (((m_value > other.m_value) && (m_value - other.m_value) <= halfMaxValue)
|| ((other.m_value > m_value) && (other.m_value - m_value) > halfMaxValue));
}
bool operator == (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
return (m_value == other.m_value);
}
bool operator != (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
return (m_value != other.m_value);
}
bool operator <= (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
return (!this->operator> (other));
}
bool operator >= (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
return (this->operator> (other) || this->operator== (other));
}
bool operator < (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &other) const
{
return !this->operator> (other) && m_value != other.m_value;
}
template<typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
friend std::ostream & operator<< (std::ostream& os, const SequenceNumber<NUMERIC_TYPE2, SIGNED_TYPE2> &val);
template<typename NUMERIC_TYPE2, typename SIGNED_TYPE2>
friend std::istream & operator >> (std::istream &is, const SequenceNumber<NUMERIC_TYPE2, SIGNED_TYPE2> &val);
private: // unimplemented operators
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& operator+= (SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> const &value);
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& operator-= (SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> const &value);
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator* (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator/ (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator% (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
bool operator ! () const;
bool operator && (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
bool operator || (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator~ () const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator& (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator| (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator^ (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator<< (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> operator>> (const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>& b) const;
int operator* ();
//SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE>* operator& ();
private:
NUMERIC_TYPE m_value;
};
template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
std::ostream &
operator<< (std::ostream& os, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &val)
{
os << val.m_value;
return os;
}
template<typename NUMERIC_TYPE, typename SIGNED_TYPE>
std::istream & operator >> (std::istream &is, const SequenceNumber<NUMERIC_TYPE, SIGNED_TYPE> &val)
{
is >> val.m_value;
return is;
}
typedef SequenceNumber<uint32_t, int32_t> SequenceNumber32;
typedef SequenceNumber<uint16_t, int16_t> SequenceNumber16;
} // namespace ns3
#endif /* NS3_SEQ_NUM_H */
|