rippled
Loading...
Searching...
No Matches
Book.h
1#ifndef XRPL_PROTOCOL_BOOK_H_INCLUDED
2#define XRPL_PROTOCOL_BOOK_H_INCLUDED
3
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/protocol/Issue.h>
7
8#include <boost/utility/base_from_member.hpp>
9
10namespace ripple {
11
16class Book final : public CountedObject<Book>
17{
18public:
22
24 {
25 }
26
28 Issue const& in_,
29 Issue const& out_,
30 std::optional<uint256> const& domain_)
31 : in(in_), out(out_), domain(domain_)
32 {
33 }
34};
35
36bool
37isConsistent(Book const& book);
38
40to_string(Book const& book);
41
43operator<<(std::ostream& os, Book const& x);
44
45template <class Hasher>
46void
47hash_append(Hasher& h, Book const& b)
48{
50 hash_append(h, b.in, b.out);
51 if (b.domain)
52 hash_append(h, *(b.domain));
53}
54
55Book
56reversed(Book const& book);
57
60[[nodiscard]] inline constexpr bool
61operator==(Book const& lhs, Book const& rhs)
62{
63 return (lhs.in == rhs.in) && (lhs.out == rhs.out) &&
64 (lhs.domain == rhs.domain);
65}
70[[nodiscard]] inline constexpr std::weak_ordering
71operator<=>(Book const& lhs, Book const& rhs)
72{
73 if (auto const c{lhs.in <=> rhs.in}; c != 0)
74 return c;
75 if (auto const c{lhs.out <=> rhs.out}; c != 0)
76 return c;
77
78 // Manually compare optionals
79 if (lhs.domain && rhs.domain)
80 return *lhs.domain <=> *rhs.domain; // Compare values if both exist
81 if (!lhs.domain && rhs.domain)
82 return std::weak_ordering::less; // Empty is considered less
83 if (lhs.domain && !rhs.domain)
84 return std::weak_ordering::greater; // Non-empty is greater
85
86 return std::weak_ordering::equivalent; // Both are empty
87}
90} // namespace ripple
91
92//------------------------------------------------------------------------------
93
94namespace std {
95
96template <>
97struct hash<ripple::Issue>
98 : private boost::base_from_member<std::hash<ripple::Currency>, 0>,
99 private boost::base_from_member<std::hash<ripple::AccountID>, 1>
100{
101private:
103 boost::base_from_member<std::hash<ripple::Currency>, 0>;
105 boost::base_from_member<std::hash<ripple::AccountID>, 1>;
106
107public:
108 hash() = default;
109
112
114 operator()(argument_type const& value) const
115 {
116 value_type result(currency_hash_type::member(value.currency));
117 if (!isXRP(value.currency))
118 boost::hash_combine(
119 result, issuer_hash_type::member(value.account));
120 return result;
121 }
122};
123
124//------------------------------------------------------------------------------
125
126template <>
127struct hash<ripple::Book>
128{
129private:
132
135
136public:
137 hash() = default;
138
141
143 operator()(argument_type const& value) const
144 {
145 value_type result(m_issue_hasher(value.in));
146 boost::hash_combine(result, m_issue_hasher(value.out));
147
148 if (value.domain)
149 boost::hash_combine(result, m_uint256_hasher(*value.domain));
150
151 return result;
152 }
153};
154
155} // namespace std
156
157//------------------------------------------------------------------------------
158
159namespace boost {
160
161template <>
162struct hash<ripple::Issue> : std::hash<ripple::Issue>
163{
164 hash() = default;
165
167 // VFALCO NOTE broken in vs2012
168 // using Base::Base; // inherit ctors
169};
170
171template <>
172struct hash<ripple::Book> : std::hash<ripple::Book>
173{
174 hash() = default;
175
177 // VFALCO NOTE broken in vs2012
178 // using Base::Base; // inherit ctors
179};
180
181} // namespace boost
182
183#endif
Specifies an order book.
Definition Book.h:17
Issue in
Definition Book.h:19
Issue out
Definition Book.h:20
std::optional< uint256 > domain
Definition Book.h:21
Book(Issue const &in_, Issue const &out_, std::optional< uint256 > const &domain_)
Definition Book.h:27
Tracks the number of instances of an object.
A currency issued by an account.
Definition Issue.h:14
AccountID account
Definition Issue.h:17
Currency currency
Definition Issue.h:16
hardened_hash<> hasher
Value hashing function.
Definition base_uint.h:151
Seed functor once per construction.
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:180
bool isConsistent(Book const &book)
Definition Book.cpp:10
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:544
Book reversed(Book const &book)
Definition Book.cpp:30
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:628
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:566
STL namespace.
value_type operator()(argument_type const &value) const
Definition Book.h:143
uint256_hasher m_uint256_hasher
Definition Book.h:134
issue_hasher m_issue_hasher
Definition Book.h:133
boost::base_from_member< std::hash< ripple::AccountID >, 1 > issuer_hash_type
Definition Book.h:105
value_type operator()(argument_type const &value) const
Definition Book.h:114
boost::base_from_member< std::hash< ripple::Currency >, 0 > currency_hash_type
Definition Book.h:103