Fast DDS  Version 3.6.1.0
Fast DDS
SequenceNumber.hpp
1 // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef FASTDDS_RTPS_COMMON__SEQUENCENUMBER_HPP
20 #define FASTDDS_RTPS_COMMON__SEQUENCENUMBER_HPP
21 
22 #include <algorithm>
23 #include <cassert>
24 #include <limits>
25 #include <vector>
26 
27 #include <fastdds/fastdds_dll.hpp>
28 #include <fastdds/rtps/common/Types.hpp>
29 #include <fastdds/utils/fixed_size_bitmap.hpp>
30 
31 namespace eprosima {
32 namespace fastdds {
33 namespace rtps {
34 
37 struct FASTDDS_EXPORTED_API SequenceNumber_t
38 {
40  int32_t high = 0;
42  uint32_t low = 0;
43 
45  SequenceNumber_t() noexcept
46  {
47  high = 0;
48  low = 0;
49  }
50 
56  int32_t hi,
57  uint32_t lo) noexcept
58  : high(hi)
59  , low(lo)
60  {
61  }
62 
66  explicit SequenceNumber_t(
67  uint64_t u) noexcept
68  : high(static_cast<int32_t>(u >> 32u))
69  , low(static_cast<uint32_t>(u))
70  {
71  }
72 
76  uint64_t to64long() const noexcept
77  {
78  return (static_cast<uint64_t>(high) << 32u) + low;
79  }
80 
82  SequenceNumber_t& operator ++() noexcept
83  {
84  ++low;
85  if (low == 0)
86  {
87  assert(std::numeric_limits<decltype(high)>::max() > high);
88  ++high;
89  }
90 
91  return *this;
92  }
93 
94  SequenceNumber_t operator ++(
95  int) noexcept
96  {
97  SequenceNumber_t result(*this);
98  ++(*this);
99  return result;
100  }
101 
106  SequenceNumber_t& operator +=(
107  int inc) noexcept
108  {
109  assert(inc >= 0);
110  uint32_t aux_low = low;
111  low += static_cast<uint32_t>(inc);
112 
113  if (low < aux_low)
114  {
115  // Being the type of the parameter an 'int', the increment of 'high' will be as much as 1.
116  assert(std::numeric_limits<decltype(high)>::max() > high);
117  ++high;
118  }
119 
120  return *this;
121  }
122 
123  static SequenceNumber_t unknown() noexcept
124  {
125  return {-1, 0};
126  }
127 
128 };
129 
130 #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
131 
138 inline bool operator ==(
139  const SequenceNumber_t& sn1,
140  const SequenceNumber_t& sn2) noexcept
141 {
142  return (sn1.low == sn2.low) && (sn1.high == sn2.high);
143 }
144 
151 inline bool operator !=(
152  const SequenceNumber_t& sn1,
153  const SequenceNumber_t& sn2) noexcept
154 {
155  return (sn1.low != sn2.low) || (sn1.high != sn2.high);
156 }
157 
164 inline bool operator >(
165  const SequenceNumber_t& seq1,
166  const SequenceNumber_t& seq2) noexcept
167 {
168  if (seq1.high == seq2.high)
169  {
170  return seq1.low > seq2.low;
171  }
172 
173  return seq1.high > seq2.high;
174 }
175 
182 inline bool operator <(
183  const SequenceNumber_t& seq1,
184  const SequenceNumber_t& seq2) noexcept
185 {
186  if (seq1.high == seq2.high)
187  {
188  return seq1.low < seq2.low;
189  }
190 
191  return seq1.high < seq2.high;
192 }
193 
200 inline bool operator >=(
201  const SequenceNumber_t& seq1,
202  const SequenceNumber_t& seq2) noexcept
203 {
204  if (seq1.high == seq2.high)
205  {
206  return seq1.low >= seq2.low;
207  }
208 
209  return seq1.high > seq2.high;
210 }
211 
218 inline bool operator <=(
219  const SequenceNumber_t& seq1,
220  const SequenceNumber_t& seq2) noexcept
221 {
222  if (seq1.high == seq2.high)
223  {
224  return seq1.low <= seq2.low;
225  }
226 
227  return seq1.high < seq2.high;
228 }
229 
237  const SequenceNumber_t& seq,
238  const uint32_t inc) noexcept
239 {
240  SequenceNumber_t res(seq.high, seq.low - inc);
241 
242  if (inc > seq.low)
243  {
244  // Being the type of the parameter an 'uint32_t', the decrement of 'high' will be as much as 1.
245  assert(0 < res.high);
246  --res.high;
247  }
248 
249  return res;
250 }
251 
259  const SequenceNumber_t& seq,
260  const uint32_t inc) noexcept
261 {
262  SequenceNumber_t res(seq.high, seq.low + inc);
263 
264  if (res.low < seq.low)
265  {
266  // Being the type of the parameter an 'uint32_t', the increment of 'high' will be as much as 1.
267  assert(std::numeric_limits<decltype(res.high)>::max() > res.high);
268  ++res.high;
269  }
270 
271  return res;
272 }
273 
281  const SequenceNumber_t& minuend,
282  const SequenceNumber_t& subtrahend) noexcept
283 {
284  assert(minuend >= subtrahend);
285  SequenceNumber_t res(minuend.high - subtrahend.high, minuend.low - subtrahend.low);
286 
287  if (minuend.low < subtrahend.low)
288  {
289  assert(0 < res.high);
290  --res.high;
291  }
292 
293  return res;
294 }
295 
296 #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
297 
299 
300 #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
301 
308 inline bool sort_seqNum(
309  const SequenceNumber_t& s1,
310  const SequenceNumber_t& s2) noexcept
311 {
312  return s1 < s2;
313 }
314 
321 inline std::ostream& operator <<(
322  std::ostream& output,
323  const SequenceNumber_t& seqNum)
324 {
325  return output << seqNum.to64long();
326 }
327 
328 inline std::ostream& operator <<(
329  std::ostream& output,
330  const std::vector<SequenceNumber_t>& seqNumSet)
331 {
332  for (const SequenceNumber_t& sn : seqNumSet)
333  {
334  output << sn << " ";
335  }
336 
337  return output;
338 }
339 
344 {
345  std::size_t operator ()(
346  const SequenceNumber_t& sequence_number) const noexcept
347  {
348  return static_cast<std::size_t>(sequence_number.to64long());
349  }
350 
351 };
352 
354 {
355  uint32_t operator ()(
356  const SequenceNumber_t& a,
357  const SequenceNumber_t& b) const noexcept
358  {
359  SequenceNumber_t diff = a - b;
360  return diff.low;
361  }
362 
363 };
364 
365 #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
366 
369 
370 #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
371 
378 inline std::ostream& operator <<(
379  std::ostream& output,
380  const SequenceNumberSet_t& sns)
381 {
382  output << sns.base().to64long() << ":";
383  sns.for_each([&output](
384  SequenceNumber_t it)
385  {
386  output << it.to64long() << "-";
387  });
388 
389  return output;
390 }
391 
398 inline std::istream& operator >>(
399  std::istream& input,
400  SequenceNumber_t& seqNum)
401 {
402  uint64_t aux;
403 
404  if (input >> aux)
405  {
406  seqNum = SequenceNumber_t(aux);
407  }
408 
409  return input;
410 }
411 
412 #endif // DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
413 
414 } // namespace rtps
415 } // namespace fastdds
416 } // namespace eprosima
417 
418 #endif // FASTDDS_RTPS_COMMON__SEQUENCENUMBER_HPP
Template class to hold a range of items using a custom bitmap.
Definition: fixed_size_bitmap.hpp:76
T base() const noexcept
Get base of the range.
Definition: fixed_size_bitmap.hpp:133
void for_each(UnaryFunc f) const
Apply a function on every item on the range.
Definition: fixed_size_bitmap.hpp:472
const SequenceNumber_t c_SequenceNumber_Unknown
Definition: SequenceNumber.hpp:298
std::istream & operator>>(std::istream &input, EntityId_t &enP)
Definition: EntityId_t.hpp:289
bool operator>(const SequenceNumber_t &seq1, const SequenceNumber_t &seq2) noexcept
Checks if a SequenceNumber_t is greater than other.
Definition: SequenceNumber.hpp:164
bool operator<=(const SequenceNumber_t &seq1, const SequenceNumber_t &seq2) noexcept
Checks if a SequenceNumber_t is less or equal than other.
Definition: SequenceNumber.hpp:218
bool operator!=(const EntityId_t &id1, const EntityId_t &id2)
Guid prefix comparison operator.
Definition: EntityId_t.hpp:267
SequenceNumber_t operator+(const SequenceNumber_t &seq, const uint32_t inc) noexcept
Add one uint32_t to a SequenceNumber_t.
Definition: SequenceNumber.hpp:258
bool operator==(const BuiltinTransportsOptions &bto1, const BuiltinTransportsOptions &bto2)
Equal to operator.
Definition: BuiltinTransports.hpp:79
SequenceNumber_t operator-(const SequenceNumber_t &seq, const uint32_t inc) noexcept
Subtract one uint32_t from a SequenceNumber_t.
Definition: SequenceNumber.hpp:236
std::ostream & operator<<(std::ostream &output, BuiltinTransports transports)
Definition: BuiltinTransports.hpp:118
bool operator<(const GUID_t &g1, const GUID_t &g2)
Definition: Guid.hpp:192
bool operator>=(const SequenceNumber_t &seq1, const SequenceNumber_t &seq2) noexcept
Checks if a SequenceNumber_t is greater or equal than other.
Definition: SequenceNumber.hpp:200
bool sort_seqNum(const SequenceNumber_t &s1, const SequenceNumber_t &s2) noexcept
Sorts two instances of SequenceNumber_t.
Definition: SequenceNumber.hpp:308
Structure SequenceNumber_t, different for each change in the same writer.
Definition: SequenceNumber.hpp:38
SequenceNumber_t(int32_t hi, uint32_t lo) noexcept
Definition: SequenceNumber.hpp:55
int32_t high
Definition: SequenceNumber.hpp:40
uint32_t low
Definition: SequenceNumber.hpp:42
SequenceNumber_t(uint64_t u) noexcept
Definition: SequenceNumber.hpp:66
static SequenceNumber_t unknown() noexcept
Definition: SequenceNumber.hpp:123
uint64_t to64long() const noexcept
Convert the number to 64 bit.
Definition: SequenceNumber.hpp:76
SequenceNumber_t() noexcept
Default constructor.
Definition: SequenceNumber.hpp:45
Definition: SequenceNumber.hpp:354
uint32_t operator()(const SequenceNumber_t &a, const SequenceNumber_t &b) const noexcept
Definition: SequenceNumber.hpp:355
Defines the STL hash function for type SequenceNumber_t.
Definition: SequenceNumber.hpp:344
std::size_t operator()(const SequenceNumber_t &sequence_number) const noexcept
Definition: SequenceNumber.hpp:345