GComm  0.2.3
evs_seqno.hpp
1 /*
2  * Copyright (C) 2009-2012 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef EVS_SEQNO_HPP
6 #define EVS_SEQNO_HPP
7 
8 #include "gcomm/types.hpp"
9 
10 #include "gu_serialize.hpp"
11 
12 //#include <stdint.h> // for uint16_t
13 #include <ostream>
14 #include <cassert>
15 
16 namespace gcomm
17 {
18  namespace evs
19  {
20  typedef int64_t seqno_t;
21 
22  class Range;
23  std::ostream& operator<<(std::ostream&, const Range&);
24  }
25 }
26 
27 
32 {
33 public:
34  Range(const seqno_t lu = -1, const seqno_t hs = -1) :
35  lu_(lu),
36  hs_(hs)
37  {}
38  seqno_t lu() const { return lu_; }
39  seqno_t hs() const { return hs_; }
40 
41  void set_lu(const seqno_t s) { lu_ = s; }
42  void set_hs(const seqno_t s) { hs_ = s; }
43 
44  size_t serialize(gu::byte_t* buf, size_t buflen, size_t offset) const
45  {
46  gu_trace(offset = gu::serialize8(lu_, buf, buflen, offset));
47  gu_trace(offset = gu::serialize8(hs_, buf, buflen, offset));
48  return offset;
49  }
50 
51  size_t unserialize(const gu::byte_t* buf, size_t buflen, size_t offset)
52  {
53  gu_trace(offset = gu::unserialize8(buf, buflen, offset, lu_));
54  gu_trace(offset = gu::unserialize8(buf, buflen, offset, hs_));
55  return offset;
56  }
57 
58  static size_t serial_size()
59  {
60  return 2 * sizeof(seqno_t);
61  }
62 
63  bool operator==(const Range& cmp) const
64  {
65  return (lu_ == cmp.lu_ && hs_ == cmp.hs_);
66  }
67 
68 private:
69  seqno_t lu_;
70  seqno_t hs_;
71 };
72 
73 inline std::ostream& gcomm::evs::operator<<(std::ostream& os, const gcomm::evs::Range& r)
74 {
75  return (os << "[" << r.lu() << "," << r.hs() << "]");
76 }
77 
78 #endif // EVS_SEQNO_HPP
Definition: evs_seqno.hpp:31