rippled
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/Issue.h>
6
7#include <boost/utility/base_from_member.hpp>
8
9namespace xrpl {
10
15class Book final : public CountedObject<Book>
16{
17public:
21
23 {
24 }
25
26 Book(Issue const& in_, Issue const& out_, std::optional<uint256> const& domain_)
27 : in(in_), out(out_), domain(domain_)
28 {
29 }
30};
31
32bool
33isConsistent(Book const& book);
34
36to_string(Book const& book);
37
39operator<<(std::ostream& os, Book const& x);
40
41template <class Hasher>
42void
43hash_append(Hasher& h, Book const& b)
44{
46 hash_append(h, b.in, b.out);
47 if (b.domain)
48 hash_append(h, *(b.domain));
49}
50
51Book
52reversed(Book const& book);
53
56[[nodiscard]] inline constexpr bool
57operator==(Book const& lhs, Book const& rhs)
58{
59 return (lhs.in == rhs.in) && (lhs.out == rhs.out) && (lhs.domain == rhs.domain);
60}
65[[nodiscard]] inline constexpr std::weak_ordering
66operator<=>(Book const& lhs, Book const& rhs)
67{
68 if (auto const c{lhs.in <=> rhs.in}; c != 0)
69 return c;
70 if (auto const c{lhs.out <=> rhs.out}; c != 0)
71 return c;
72
73 // Manually compare optionals
74 if (lhs.domain && rhs.domain)
75 return *lhs.domain <=> *rhs.domain; // Compare values if both exist
76 if (!lhs.domain && rhs.domain)
77 return std::weak_ordering::less; // Empty is considered less
78 if (lhs.domain && !rhs.domain)
79 return std::weak_ordering::greater; // Non-empty is greater
80
81 return std::weak_ordering::equivalent; // Both are empty
82}
85} // namespace xrpl
86
87//------------------------------------------------------------------------------
88
89namespace std {
90
91template <>
92struct hash<xrpl::Issue> : private boost::base_from_member<std::hash<xrpl::Currency>, 0>,
93 private boost::base_from_member<std::hash<xrpl::AccountID>, 1>
94{
95private:
96 using currency_hash_type = boost::base_from_member<std::hash<xrpl::Currency>, 0>;
97 using issuer_hash_type = boost::base_from_member<std::hash<xrpl::AccountID>, 1>;
98
99public:
100 hash() = default;
101
104
106 operator()(argument_type const& value) const
107 {
108 value_type result(currency_hash_type::member(value.currency));
109 if (!isXRP(value.currency))
110 boost::hash_combine(result, issuer_hash_type::member(value.account));
111 return result;
112 }
113};
114
115//------------------------------------------------------------------------------
116
117template <>
118struct hash<xrpl::Book>
119{
120private:
123
126
127public:
128 hash() = default;
129
132
134 operator()(argument_type const& value) const
135 {
136 value_type result(m_issue_hasher(value.in));
137 boost::hash_combine(result, m_issue_hasher(value.out));
138
139 if (value.domain)
140 boost::hash_combine(result, m_uint256_hasher(*value.domain));
141
142 return result;
143 }
144};
145
146} // namespace std
147
148//------------------------------------------------------------------------------
149
150namespace boost {
151
152template <>
153struct hash<xrpl::Issue> : std::hash<xrpl::Issue>
154{
155 hash() = default;
156
158 // VFALCO NOTE broken in vs2012
159 // using Base::Base; // inherit ctors
160};
161
162template <>
163struct hash<xrpl::Book> : std::hash<xrpl::Book>
164{
165 hash() = default;
166
168 // VFALCO NOTE broken in vs2012
169 // using Base::Base; // inherit ctors
170};
171
172} // namespace boost
Specifies an order book.
Definition Book.h:16
Book()
Definition Book.h:22
Book(Issue const &in_, Issue const &out_, std::optional< uint256 > const &domain_)
Definition Book.h:26
Issue in
Definition Book.h:18
std::optional< uint256 > domain
Definition Book.h:20
Issue out
Definition Book.h:19
Tracks the number of instances of an object.
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
hardened_hash<> hasher
Value hashing function.
Definition base_uint.h:146
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.
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isConsistent(Book const &book)
Definition Book.cpp:10
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:612
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:552
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:531
Book reversed(Book const &book)
Definition Book.cpp:29
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:174
uint256_hasher m_uint256_hasher
Definition Book.h:125
value_type operator()(argument_type const &value) const
Definition Book.h:134
issue_hasher m_issue_hasher
Definition Book.h:124
value_type operator()(argument_type const &value) const
Definition Book.h:106
boost::base_from_member< std::hash< xrpl::Currency >, 0 > currency_hash_type
Definition Book.h:96
boost::base_from_member< std::hash< xrpl::AccountID >, 1 > issuer_hash_type
Definition Book.h:97