xrpld
Loading...
Searching...
No Matches
Book.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Asset.h>
7#include <xrpl/protocol/Issue.h>
8#include <xrpl/protocol/MPTIssue.h>
9#include <xrpl/protocol/UintTypes.h>
10
11#include <boost/utility/base_from_member.hpp>
12
13#include <compare>
14#include <cstddef>
15#include <functional>
16#include <optional>
17#include <ostream>
18#include <string>
19
20namespace xrpl {
21
27class Book final : public CountedObject<Book>
28{
29public:
33
34 Book() = default;
35
37 : in(in), out(out), domain(domain)
38 {
39 }
40};
41
42bool
43isConsistent(Book const& book);
44
46to_string(Book const& book);
47
49operator<<(std::ostream& os, Book const& x);
50
51template <class Hasher>
52void
53hash_append(Hasher& h, Book const& b)
54{
56 hash_append(h, b.in, b.out);
57 if (b.domain)
58 hash_append(h, *(b.domain));
59}
60
61Book
62reversed(Book const& book);
63
68[[nodiscard]] constexpr bool
69operator==(Book const& lhs, Book const& rhs)
70{
71 return (lhs.in == rhs.in) && (lhs.out == rhs.out) && (lhs.domain == rhs.domain);
72}
73
74
79[[nodiscard]] constexpr std::weak_ordering
80operator<=>(Book const& lhs, Book const& rhs)
81{
82 if (auto const c{lhs.in <=> rhs.in}; c != 0) // NOLINT(modernize-use-nullptr)
83 return c;
84 if (auto const c{lhs.out <=> rhs.out}; c != 0) // NOLINT(modernize-use-nullptr)
85 return c;
86
87 // Manually compare optionals
88 if (lhs.domain && rhs.domain)
89 return *lhs.domain <=> *rhs.domain; // Compare values if both exist
90 if (!lhs.domain && rhs.domain)
91 return std::weak_ordering::less; // Empty is considered less
92 if (lhs.domain && !rhs.domain)
93 return std::weak_ordering::greater; // Non-empty is greater
94
95 return std::weak_ordering::equivalent; // Both are empty
96}
97
98
99} // namespace xrpl
100
101//------------------------------------------------------------------------------
102
103namespace std {
104
105template <>
106struct hash<xrpl::Issue> : private boost::base_from_member<std::hash<xrpl::Currency>, 0>,
107 private boost::base_from_member<std::hash<xrpl::AccountID>, 1>
108{
109private:
110 using currency_hash_type = boost::base_from_member<std::hash<xrpl::Currency>, 0>;
111 using issuer_hash_type = boost::base_from_member<std::hash<xrpl::AccountID>, 1>;
112
113public:
114 hash() = default;
115
118
120 operator()(argument_type const& value) const
121 {
122 value_type result(currency_hash_type::member(value.currency));
123 if (!isXRP(value.currency))
124 boost::hash_combine(result, issuer_hash_type::member(value.account));
125 return result;
126 }
127};
128
129template <>
130struct hash<xrpl::MPTIssue> : private boost::base_from_member<std::hash<xrpl::MPTID>, 0>
131{
132private:
133 using id_hash_type = boost::base_from_member<std::hash<xrpl::MPTID>, 0>;
134
135public:
136 explicit hash() = default;
137
140
142 operator()(argument_type const& value) const
143 {
144 value_type const result(id_hash_type::member(value.getMptID()));
145 return result;
146 }
147};
148
149template <>
150struct hash<xrpl::Asset>
151{
152private:
155
158
161
162public:
163 explicit hash() = default;
164
166 operator()(argument_type const& asset) const
167 {
168 return asset.visit(
169 [&](xrpl::Issue const& issue) {
170 value_type const result(mIssueHasher_(issue));
171 return result;
172 },
173 [&](xrpl::MPTIssue const& issue) {
174 value_type const result(mMptissueHasher_(issue));
175 return result;
176 });
177 }
178};
179
180//------------------------------------------------------------------------------
181
182template <>
183struct hash<xrpl::Book>
184{
185private:
188
191
192public:
193 hash() = default;
194
197
199 operator()(argument_type const& value) const
200 {
201 value_type result(issueHasher_(value.in));
202 boost::hash_combine(result, issueHasher_(value.out));
203
204 if (value.domain)
205 boost::hash_combine(result, uint256Hasher_(*value.domain));
206
207 return result;
208 }
209};
210
211} // namespace std
212
213//------------------------------------------------------------------------------
214
215namespace boost {
216
217template <>
218struct hash<xrpl::Issue> : std::hash<xrpl::Issue>
219{
220 hash() = default;
221
223 // VFALCO NOTE broken in vs2012
224 // using Base::Base; // inherit ctors
225};
226
227template <>
228struct hash<xrpl::MPTIssue> : std::hash<xrpl::MPTIssue>
229{
230 explicit hash() = default;
231
233};
234
235template <>
236struct hash<xrpl::Asset> : std::hash<xrpl::Asset>
237{
238 explicit hash() = default;
239
241};
242
243template <>
244struct hash<xrpl::Book> : std::hash<xrpl::Book>
245{
246 hash() = default;
247
249 // VFALCO NOTE broken in vs2012
250 // using Base::Base; // inherit ctors
251};
252
253} // namespace boost
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:117
HardenedHash<> hasher
Definition base_uint.h:163
Specifies an order book.
Definition Book.h:28
Asset in
Definition Book.h:30
Asset out
Definition Book.h:31
Book(Asset const &in, Asset const &out, std::optional< uint256 > const &domain)
Definition Book.h:36
std::optional< uint256 > domain
Definition Book.h:32
Book()=default
A currency issued by an account.
Definition Issue.h:18
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:606
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:666
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
constexpr std::strong_ordering operator<=>(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:585
Book reversed(Book const &book)
Definition Book.cpp:30
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:194
bool isConsistent(Asset const &asset)
Definition Asset.h:323
std::hash< xrpl::Asset > Base
Definition Book.h:240
std::hash< xrpl::Book > Base
Definition Book.h:248
std::hash< xrpl::Issue > Base
Definition Book.h:222
std::hash< xrpl::MPTIssue > Base
Definition Book.h:232
value_type operator()(argument_type const &asset) const
Definition Book.h:166
std::hash< xrpl::Issue > issue_hasher
Definition Book.h:156
mptissue_hasher mMptissueHasher_
Definition Book.h:160
xrpl::Asset argument_type
Definition Book.h:154
std::hash< xrpl::MPTIssue > mptissue_hasher
Definition Book.h:157
std::size_t value_type
Definition Book.h:153
issue_hasher mIssueHasher_
Definition Book.h:159
std::hash< xrpl::Asset > asset_hasher
Definition Book.h:186
xrpl::uint256::hasher uint256_hasher
Definition Book.h:187
std::size_t value_type
Definition Book.h:195
xrpl::Book argument_type
Definition Book.h:196
value_type operator()(argument_type const &value) const
Definition Book.h:199
uint256_hasher uint256Hasher_
Definition Book.h:190
asset_hasher issueHasher_
Definition Book.h:189
xrpl::Issue argument_type
Definition Book.h:117
value_type operator()(argument_type const &value) const
Definition Book.h:120
boost::base_from_member< std::hash< xrpl::Currency >, 0 > currency_hash_type
Definition Book.h:110
std::size_t value_type
Definition Book.h:116
boost::base_from_member< std::hash< xrpl::AccountID >, 1 > issuer_hash_type
Definition Book.h:111
xrpl::MPTIssue argument_type
Definition Book.h:139
value_type operator()(argument_type const &value) const
Definition Book.h:142
boost::base_from_member< std::hash< xrpl::MPTID >, 0 > id_hash_type
Definition Book.h:133
std::size_t value_type
Definition Book.h:138