OpenDDS  Snapshot(2023/04/28-20:55)
SequenceNumber.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_SEQUENCENUMBER_H
9 #define OPENDDS_DCPS_SEQUENCENUMBER_H
10 
11 #include "Serializer.h"
12 
13 #include <ace/Global_Macros.h>
14 
15 #if !defined (ACE_LACKS_PRAGMA_ONCE)
16 #pragma once
17 #endif /* ACE_LACKS_PRAGMA_ONCE */
18 
19 #include <utility>
20 
22 
23 namespace OpenDDS {
24 namespace DCPS {
25 
26 /// Sequence number abstraction. Only allows positive 64 bit values.
28 public:
29  typedef ACE_INT64 Value;
30  /// Construct with a value, default to one (starting point).
31  SequenceNumber(Value value = INITIAL_VALUE) {
32  setValue(value);
33  }
34 
35  /// Pre-increment.
37  if (this->low_ == ACE_UINT32_MAX) {
38  if (this->high_ == ACE_INT32_MAX) {
39  // this code is here, despite the RTPS spec statement:
40  // "sequence numbers never wrap"
41  this->high_ = 0;
42  this->low_ = 1;
43  } else {
44  ++this->high_;
45  this->low_ = 0;
46  }
47  } else {
48  ++this->low_;
49  }
50  return *this;
51  }
52 
53  /// Post-increment.
55  SequenceNumber value(*this);
56  ++*this;
57  return value;
58  }
59 
61  SequenceNumber retVal(*this);
62  if ((this->low_ == 0) && (this->high_ == 0)) {
63  retVal.high_ = ACE_INT32_MAX;
64  retVal.low_ = ACE_UINT32_MAX;
65  return retVal;
66  }
67  if (this->low_ == 0) {
68  --retVal.high_;
69  retVal.low_ = ACE_UINT32_MAX;
70  } else {
71  --retVal.low_;
72  }
73  return retVal;
74  }
75 
76  void setValue(Value value) {
77  if (value < MIN_VALUE) {
78  value = MIN_VALUE;
79  }
80  this->high_ = ACE_INT32(value / LOW_BASE);
81  this->low_ = ACE_UINT32(value % LOW_BASE);
82  }
83 
84  void setValue(ACE_INT32 high, ACE_UINT32 low) {
85  this->high_ = high;
86  this->low_ = low;
87  if (this->getValue() < MIN_VALUE) {
88  this->setValue(MIN_VALUE);
89  }
90  }
91 
92  Value getValue() const {
93  return LOW_BASE * this->high_ + this->low_;
94  }
95 
96  bool operator<(const SequenceNumber& rvalue) const {
97  return (this->high_ < rvalue.high_)
98  || (this->high_ == rvalue.high_ && this->low_ < rvalue.low_);
99  }
100 
101  /// Derive a full suite of logical operations.
102  bool operator==(const SequenceNumber& rvalue) const {
103  return (this->high_ == rvalue.high_) &&
104  (this->low_ == rvalue.low_);
105  }
106  bool operator!=(const SequenceNumber& rvalue) const {
107  return (this->high_ != rvalue.high_) ||
108  (this->low_ != rvalue.low_);
109  }
110  bool operator>=(const SequenceNumber& rvalue) const {
111  return !(*this < rvalue);
112  }
113  bool operator<=(const SequenceNumber& rvalue) const {
114  return !(rvalue < *this);
115  }
116  bool operator>(const SequenceNumber& rvalue) const {
117  return (rvalue < *this)
118  && (*this != rvalue);
119  }
120 
121  ACE_INT32 getHigh() const {
122  return high_;
123  }
124  ACE_UINT32 getLow() const {
125  return low_;
126  }
127 
128  // SEQUENCENUMBER_UNKNOWN is defined by the RTPS spec.
130  return SequenceNumber(-1, 0);
131  }
132 
133  static SequenceNumber ZERO() {
134  return SequenceNumber(0, 0);
135  }
136 
137  static const Value MAX_VALUE = ACE_INT64_MAX;
138  static const Value INITIAL_VALUE = 1;
139  static const Value MIN_VALUE = 0;
140  static const Value LOW_BASE = 0x0000000100000000LL;
141 
143 
144 private:
145 
146  // Private constructor used to force construction of SEQUENCENUMBER_UNKNOWN.
147  // Also used by operator>> to allow deserialization of the same value.
148  SequenceNumber(ACE_INT32 high, ACE_UINT32 low)
149  : high_(high), low_(low) {
150  }
151 
152  ACE_INT32 high_;
153  ACE_UINT32 low_;
154 };
155 
156 inline ACE_CDR::Boolean
158  s << x.getHigh();
159  s << x.getLow();
160  return s.good_bit();
161 }
162 
163 inline ACE_CDR::Boolean
165  ACE_INT32 high;
166  ACE_UINT32 low;
167  if (!(s >> high)) {
168  return false;
169  }
170  if (!(s >> low)) {
171  return false;
172  }
173  x = SequenceNumber(high, low);
174  return true;
175 }
176 
177 inline SequenceNumber
178 operator+(const SequenceNumber& lhs, int rhs)
179 {
180  return SequenceNumber(lhs.getValue() + rhs);
181 }
182 
183 inline SequenceNumber
185 {
186  lhs.setValue(lhs.getValue() + rhs);
187  return lhs;
188 }
189 
190 inline SequenceNumber
191 operator+(int lhs, const SequenceNumber& rhs)
192 {
193  return rhs + lhs;
194 }
195 
196 inline
197 void serialized_size(const Encoding& encoding, size_t& size,
198  const SequenceNumber& /*sn*/)
199 {
200  primitive_serialized_size_ulong(encoding, size, 2);
201 }
202 
203 typedef std::pair<SequenceNumber, SequenceNumber> SequenceRange;
204 extern OpenDDS_Dcps_Export const SequenceRange unknown_sequence_range;
205 
207 static const FragmentNumber INVALID_FRAGMENT = -1;
208 
209 } // namespace DCPS
210 } // namespace OpenDDS
211 
213 
214 #endif /* OPENDDS_DCPS_SEQUENCENUMBER_H */
SequenceNumber operator++(int)
Post-increment.
bool operator<(const SequenceNumber &rvalue) const
const LogLevel::Value value
Definition: debug.cpp:61
OpenDDS_Dcps_Export void primitive_serialized_size_ulong(const Encoding &encoding, size_t &size, size_t count=1)
SequenceNumber previous() const
SequenceNumber & operator++()
Pre-increment.
SequenceNumber(Value value=INITIAL_VALUE)
Construct with a value, default to one (starting point).
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
bool operator>=(const SequenceNumber &rvalue) const
bool operator==(const SequenceNumber &rvalue) const
Derive a full suite of logical operations.
void serialized_size(const Encoding &encoding, size_t &size, const SequenceNumber &)
OpenDDS_Dcps_Export const SequenceRange unknown_sequence_range
#define ACE_INT64_MAX
SequenceNumber(ACE_INT32 high, ACE_UINT32 low)
Class to serialize and deserialize data for DDS.
Definition: Serializer.h:369
ACE_CDR::Boolean operator<<(Serializer &serializer, CoherentChangeControl &value)
Marshal/Insertion into a buffer.
bool operator<=(const SequenceNumber &rvalue) const
SequenceNumber::Value FragmentNumber
SequenceNumber operator+(const SequenceNumber &lhs, int rhs)
static SequenceNumber ZERO()
void setValue(ACE_INT32 high, ACE_UINT32 low)
std::pair< SequenceNumber, SequenceNumber > SequenceRange
static const FragmentNumber INVALID_FRAGMENT
bool operator>(const SequenceNumber &rvalue) const
Sequence number abstraction. Only allows positive 64 bit values.
static SequenceNumber SEQUENCENUMBER_UNKNOWN()
SequenceNumber operator+=(SequenceNumber &lhs, int rhs)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
ACE_CDR::Boolean operator>>(Serializer &serializer, CoherentChangeControl &value)
long long ACE_INT64
const DCPS::Encoding encoding(DCPS::Encoding::KIND_UNALIGNED_CDR, DCPS::ENDIAN_BIG)
bool Boolean
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
bool operator!=(const SequenceNumber &rvalue) const
#define ACE_UINT32_MAX
#define ACE_INT32_MAX