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