rippled
SeqProxy.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2018 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PROTOCOL_SEQ_PROXY_H_INCLUDED
21 #define RIPPLE_PROTOCOL_SEQ_PROXY_H_INCLUDED
22 
23 #include <cstdint>
24 #include <ostream>
25 
26 namespace ripple {
27 
55 class SeqProxy
56 {
57 public:
58  enum Type : std::uint8_t { seq = 0, ticket };
59 
60 private:
63 
64 public:
65  constexpr explicit SeqProxy(Type t, std::uint32_t v) : value_{v}, type_{t}
66  {
67  }
68 
69  SeqProxy(SeqProxy const& other) = default;
70 
71  SeqProxy&
72  operator=(SeqProxy const& other) = default;
73 
75  static constexpr SeqProxy
77  {
78  return SeqProxy{Type::seq, v};
79  }
80 
81  constexpr std::uint32_t
82  value() const
83  {
84  return value_;
85  }
86 
87  constexpr bool
88  isSeq() const
89  {
90  return type_ == seq;
91  }
92 
93  constexpr bool
94  isTicket() const
95  {
96  return type_ == ticket;
97  }
98 
99  // Occasionally it is convenient to be able to increase the value_
100  // of a SeqProxy. But it's unusual. So, rather than putting in an
101  // addition operator, you must invoke the method by name. That makes
102  // if more difficult to invoke accidentally.
103  SeqProxy&
105  {
106  value_ += amount;
107  return *this;
108  }
109 
110  // Comparison
111  //
112  // The comparison is designed specifically so _all_ Sequence
113  // representations sort in front of Ticket representations. This
114  // is true even if the Ticket value() is less that the Sequence
115  // value().
116  //
117  // This somewhat surprising sort order has benefits for transaction
118  // processing. It guarantees that transactions creating Tickets are
119  // sorted in from of transactions that consume Tickets.
120  friend constexpr bool
122  {
123  if (lhs.type_ != rhs.type_)
124  return false;
125  return (lhs.value() == rhs.value());
126  }
127 
128  friend constexpr bool
130  {
131  return !(lhs == rhs);
132  }
133 
134  friend constexpr bool
136  {
137  if (lhs.type_ != rhs.type_)
138  return lhs.type_ < rhs.type_;
139  return lhs.value() < rhs.value();
140  }
141 
142  friend constexpr bool
144  {
145  return rhs < lhs;
146  }
147 
148  friend constexpr bool
150  {
151  return !(lhs < rhs);
152  }
153 
154  friend constexpr bool
156  {
157  return !(lhs > rhs);
158  }
159 
160  friend std::ostream&
162  {
163  os << (seqProx.isSeq() ? "sequence " : "ticket ");
164  os << seqProx.value();
165  return os;
166  }
167 };
168 } // namespace ripple
169 
170 #endif
ripple::SeqProxy::operator>
constexpr friend bool operator>(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:143
ripple::SeqProxy::ticket
@ ticket
Definition: SeqProxy.h:58
ripple::SeqProxy::sequence
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition: SeqProxy.h:76
ripple::SeqProxy::type_
Type type_
Definition: SeqProxy.h:62
ripple::SeqProxy::isTicket
constexpr bool isTicket() const
Definition: SeqProxy.h:94
ripple::SeqProxy::isSeq
constexpr bool isSeq() const
Definition: SeqProxy.h:88
std::ostream
STL class.
ripple::SeqProxy::operator<<
friend std::ostream & operator<<(std::ostream &os, SeqProxy seqProx)
Definition: SeqProxy.h:161
cstdint
ripple::SeqProxy::advanceBy
SeqProxy & advanceBy(std::uint32_t amount)
Definition: SeqProxy.h:104
std::uint8_t
ripple::SeqProxy::Type
Type
Definition: SeqProxy.h:58
ripple::SeqProxy::operator<
constexpr friend bool operator<(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:135
ripple::SeqProxy::operator=
SeqProxy & operator=(SeqProxy const &other)=default
ripple::SeqProxy::value
constexpr std::uint32_t value() const
Definition: SeqProxy.h:82
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SeqProxy::value_
std::uint32_t value_
Definition: SeqProxy.h:61
ripple::SeqProxy
A type that represents either a sequence value or a ticket value.
Definition: SeqProxy.h:55
ripple::SeqProxy::operator!=
constexpr friend bool operator!=(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:129
ripple::SeqProxy::SeqProxy
constexpr SeqProxy(Type t, std::uint32_t v)
Definition: SeqProxy.h:65
ostream
ripple::SeqProxy::operator<=
constexpr friend bool operator<=(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:155
ripple::SeqProxy::operator==
constexpr friend bool operator==(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:121
ripple::SeqProxy::seq
@ seq
Definition: SeqProxy.h:58
ripple::SeqProxy::operator>=
constexpr friend bool operator>=(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:149