rippled
Loading...
Searching...
No Matches
SeqProxy.h
1#ifndef XRPL_PROTOCOL_SEQ_PROXY_H_INCLUDED
2#define XRPL_PROTOCOL_SEQ_PROXY_H_INCLUDED
3
4#include <cstdint>
5#include <ostream>
6
7namespace ripple {
8
37{
38public:
39 enum Type : std::uint8_t { seq = 0, ticket };
40
41private:
44
45public:
46 constexpr explicit SeqProxy(Type t, std::uint32_t v) : value_{v}, type_{t}
47 {
48 }
49
50 SeqProxy(SeqProxy const& other) = default;
51
53 operator=(SeqProxy const& other) = default;
54
56 static constexpr SeqProxy
58 {
59 return SeqProxy{Type::seq, v};
60 }
61
62 constexpr std::uint32_t
63 value() const
64 {
65 return value_;
66 }
67
68 constexpr bool
69 isSeq() const
70 {
71 return type_ == seq;
72 }
73
74 constexpr bool
75 isTicket() const
76 {
77 return type_ == ticket;
78 }
79
80 // Occasionally it is convenient to be able to increase the value_
81 // of a SeqProxy. But it's unusual. So, rather than putting in an
82 // addition operator, you must invoke the method by name. That makes
83 // if more difficult to invoke accidentally.
86 {
87 value_ += amount;
88 return *this;
89 }
90
91 // Comparison
92 //
93 // The comparison is designed specifically so _all_ Sequence
94 // representations sort in front of Ticket representations. This
95 // is true even if the Ticket value() is less that the Sequence
96 // value().
97 //
98 // This somewhat surprising sort order has benefits for transaction
99 // processing. It guarantees that transactions creating Tickets are
100 // sorted in from of transactions that consume Tickets.
101 friend constexpr bool
103 {
104 if (lhs.type_ != rhs.type_)
105 return false;
106 return (lhs.value() == rhs.value());
107 }
108
109 friend constexpr bool
111 {
112 return !(lhs == rhs);
113 }
114
115 friend constexpr bool
116 operator<(SeqProxy lhs, SeqProxy rhs)
117 {
118 if (lhs.type_ != rhs.type_)
119 return lhs.type_ < rhs.type_;
120 return lhs.value() < rhs.value();
121 }
122
123 friend constexpr bool
125 {
126 return rhs < lhs;
127 }
128
129 friend constexpr bool
131 {
132 return !(lhs < rhs);
133 }
134
135 friend constexpr bool
137 {
138 return !(lhs > rhs);
139 }
140
142 operator<<(std::ostream& os, SeqProxy seqProx)
143 {
144 os << (seqProx.isSeq() ? "sequence " : "ticket ");
145 os << seqProx.value();
146 return os;
147 }
148};
149} // namespace ripple
150
151#endif
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
friend constexpr bool operator<(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:116
std::uint32_t value_
Definition SeqProxy.h:42
friend constexpr bool operator>(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:124
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:57
constexpr bool isSeq() const
Definition SeqProxy.h:69
friend constexpr bool operator!=(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:110
constexpr std::uint32_t value() const
Definition SeqProxy.h:63
constexpr SeqProxy(Type t, std::uint32_t v)
Definition SeqProxy.h:46
friend constexpr bool operator>=(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:130
friend constexpr bool operator==(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:102
SeqProxy & operator=(SeqProxy const &other)=default
friend std::ostream & operator<<(std::ostream &os, SeqProxy seqProx)
Definition SeqProxy.h:142
constexpr bool isTicket() const
Definition SeqProxy.h:75
friend constexpr bool operator<=(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:136
SeqProxy(SeqProxy const &other)=default
SeqProxy & advanceBy(std::uint32_t amount)
Definition SeqProxy.h:85
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6