rippled
Loading...
Searching...
No Matches
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
26namespace ripple {
27
56{
57public:
58 enum Type : std::uint8_t { seq = 0, ticket };
59
60private:
63
64public:
65 constexpr explicit SeqProxy(Type t, std::uint32_t v) : value_{v}, type_{t}
66 {
67 }
68
69 SeqProxy(SeqProxy const& other) = default;
70
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
135 operator<(SeqProxy lhs, SeqProxy rhs)
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
161 operator<<(std::ostream& os, SeqProxy seqProx)
162 {
163 os << (seqProx.isSeq() ? "sequence " : "ticket ");
164 os << seqProx.value();
165 return os;
166 }
167};
168} // namespace ripple
169
170#endif
A type that represents either a sequence value or a ticket value.
Definition: SeqProxy.h:56
friend constexpr bool operator<(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:135
std::uint32_t value_
Definition: SeqProxy.h:61
friend constexpr bool operator>(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:143
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition: SeqProxy.h:76
constexpr bool isSeq() const
Definition: SeqProxy.h:88
friend constexpr bool operator!=(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:129
constexpr std::uint32_t value() const
Definition: SeqProxy.h:82
constexpr SeqProxy(Type t, std::uint32_t v)
Definition: SeqProxy.h:65
friend constexpr bool operator>=(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:149
friend constexpr bool operator==(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:121
SeqProxy & operator=(SeqProxy const &other)=default
friend std::ostream & operator<<(std::ostream &os, SeqProxy seqProx)
Definition: SeqProxy.h:161
constexpr bool isTicket() const
Definition: SeqProxy.h:94
friend constexpr bool operator<=(SeqProxy lhs, SeqProxy rhs)
Definition: SeqProxy.h:155
SeqProxy(SeqProxy const &other)=default
SeqProxy & advanceBy(std::uint32_t amount)
Definition: SeqProxy.h:104
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26