xrpld
Loading...
Searching...
No Matches
SeqProxy.h
1#pragma once
2
3#include <cstdint>
4#include <ostream>
5
6namespace xrpl {
7
37{
38public:
39 enum class 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
61 static constexpr SeqProxy
63 {
64 return SeqProxy{Type::Seq, v};
65 }
66
73 static constexpr SeqProxy
75 {
76 return SeqProxy{Type::Ticket, v};
77 }
78
79 [[nodiscard]] constexpr std::uint32_t
80 value() const
81 {
82 return value_;
83 }
84
85 [[nodiscard]] constexpr bool
86 isSeq() const
87 {
88 return type_ == Type::Seq;
89 }
90
91 [[nodiscard]] constexpr bool
92 isTicket() const
93 {
94 return type_ == Type::Ticket;
95 }
96
97 // Occasionally it is convenient to be able to increase the value_
98 // of a SeqProxy. But it's unusual. So, rather than putting in an
99 // addition operator, you must invoke the method by name. That makes
100 // if more difficult to invoke accidentally.
101 SeqProxy&
103 {
104 value_ += amount;
105 return *this;
106 }
107
108 // Comparison
109 //
110 // The comparison is designed specifically so _all_ Sequence
111 // representations sort in front of Ticket representations. This
112 // is true even if the Ticket value() is less that the Sequence
113 // value().
114 //
115 // This somewhat surprising sort order has benefits for transaction
116 // processing. It guarantees that transactions creating Tickets are
117 // sorted in from of transactions that consume Tickets.
118 friend constexpr bool
120 {
121 if (lhs.type_ != rhs.type_)
122 return false;
123 return (lhs.value() == rhs.value());
124 }
125
126 friend constexpr bool
127 operator<(SeqProxy lhs, SeqProxy rhs)
128 {
129 if (lhs.type_ != rhs.type_)
130 return lhs.type_ < rhs.type_;
131 return lhs.value() < rhs.value();
132 }
133
134 friend constexpr bool
136 {
137 return rhs < lhs;
138 }
139
140 friend constexpr bool
142 {
143 return !(lhs < rhs);
144 }
145
146 friend constexpr bool
148 {
149 return !(lhs > rhs);
150 }
151
153 operator<<(std::ostream& os, SeqProxy seqProx)
154 {
155 os << (seqProx.isSeq() ? "sequence " : "ticket ");
156 os << seqProx.value();
157 return os;
158 }
159};
160} // namespace xrpl
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:127
std::uint32_t value_
Definition SeqProxy.h:42
friend constexpr bool operator>(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:135
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
constexpr SeqProxy(Type t, std::uint32_t v)
Definition SeqProxy.h:46
SeqProxy & operator=(SeqProxy const &other)=default
friend constexpr bool operator>=(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:141
friend constexpr bool operator==(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:119
SeqProxy(SeqProxy const &other)=default
constexpr bool isTicket() const
Definition SeqProxy.h:92
friend std::ostream & operator<<(std::ostream &os, SeqProxy seqProx)
Definition SeqProxy.h:153
SeqProxy & advanceBy(std::uint32_t amount)
Definition SeqProxy.h:102
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Definition SeqProxy.h:74
friend constexpr bool operator<=(SeqProxy lhs, SeqProxy rhs)
Definition SeqProxy.h:147
constexpr std::uint32_t value() const
Definition SeqProxy.h:80
constexpr bool isSeq() const
Definition SeqProxy.h:86
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5