summaryrefslogtreecommitdiffstats
path: root/src/sequence_number.h
blob: c1920127df4989b5b480a547c8d74aeb527dee8d (plain)
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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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
*/

/**
 * @file
 * Sequence number operations.
 */

#ifndef _SEQUENCE_NUMBER_H
#define _SEQUENCE_NUMBER_H

#include <cc++/config.h>

/**
 * Sequence numbers.
 * Sequence numbers with comparison operators that deal with sequence number
 * roll-overs using serial number arithmetic.
 * See http://en.wikipedia.org/wiki/Serial_Number_Arithmetic
 *
 * @param U The unsigned int type for the sequence number.
 * @param S The corresponsing signed int type having the same width.
 */
template< typename U, typename S >
class sequence_number_t {
private:
	/**
	 * The sequence number.
	 */
	U _number;

public:
	/**
	 * Constructor.
	 * @param number The sequence number.
	 */
	explicit sequence_number_t(U number) : _number(number)
	{};

	/**
	 * Get the sequence number.
	 * @return The sequence number.
	 */
	U get_number(void) const {
		return _number;
	}

	/**
	 * Cast to the sequence number.
	 */
	operator U(void) const {
		return get_number();
	}

	/**
	 * Calculate the distance to another sequence number.
	 * @param number The sequence number to which the distance must be calculated.
	 * @return The distance.
	 */
	S distance(const sequence_number_t &number) const {
		return static_cast<S>(_number - number.get_number());
	}

	/**
	 * Calculate the distance to another distance sequence number.
	 * @param number The sequence number to which the distance must be calculated.
	 * @return The distance.
	 */
	S operator-(const sequence_number_t &number) const {
		return distance(number);
	}

	/**
	 * Less-than comparison.
	 * @param number The sequence number to compare with.
	 * @return true, if this sequence number is less than number.
	 * @return false, otherwise.
	 */
	bool operator<(const sequence_number_t &number) const {
		return (distance(number) < 0);
	}

	/**
	 * Less-than-equal comparison.
	 * @param number The sequence number to compare with.
	 * @return true, if this sequence number is less than or equal to number.
	 * @return false, otherwise.
	 */
	bool operator<=(const sequence_number_t &number) const {
		return (distance(number) <= 0);
	}

	/**
	 * Equality comparison.
	 * @param number The sequence number to compare with.
	 * @return true, if this sequence number is equal to number.
	 * @return false, otherwise.
	 */
	bool operator==(const sequence_number_t &number) const {
		return (number.get_number() == _number);
	}

	/**
	 * Greater-than comparison.
	 * @param number The sequence number to compare with.
	 * @return true, if this sequence number is greater than number.
	 * @return false, otherwise.
	 */
	bool operator>(const sequence_number_t &number) const {
		return (distance(number) > 0);
	}

	/**
	 * Greater-than-equal comparison.
	 * @param number The sequence number to compare with.
	 * @return true, if this sequence number is greater than or equal to number.
	 * @return false, otherwise.
	 */
	bool operator>=(const sequence_number_t &number) const {
		return (distance(number) >= 0);
	}
};

/**
 * 16-bit sequence number
 */
typedef sequence_number_t<uint16, int16> seq16_t;

/**
 * 32-bit sequence number
 */
typedef sequence_number_t<uint32, int32> seq32_t;

#endif