rippled
Loading...
Searching...
No Matches
Book.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 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_BOOK_H_INCLUDED
21#define RIPPLE_PROTOCOL_BOOK_H_INCLUDED
22
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/basics/base_uint.h>
25#include <xrpl/protocol/Issue.h>
26
27#include <boost/utility/base_from_member.hpp>
28
29namespace ripple {
30
35class Book final : public CountedObject<Book>
36{
37public:
41
43 {
44 }
45
47 Issue const& in_,
48 Issue const& out_,
49 std::optional<uint256> const& domain_)
50 : in(in_), out(out_), domain(domain_)
51 {
52 }
53};
54
55bool
56isConsistent(Book const& book);
57
59to_string(Book const& book);
60
62operator<<(std::ostream& os, Book const& x);
63
64template <class Hasher>
65void
66hash_append(Hasher& h, Book const& b)
67{
69 hash_append(h, b.in, b.out);
70 if (b.domain)
71 hash_append(h, *(b.domain));
72}
73
74Book
75reversed(Book const& book);
76
79[[nodiscard]] inline constexpr bool
80operator==(Book const& lhs, Book const& rhs)
81{
82 return (lhs.in == rhs.in) && (lhs.out == rhs.out) &&
83 (lhs.domain == rhs.domain);
84}
89[[nodiscard]] inline constexpr std::weak_ordering
90operator<=>(Book const& lhs, Book const& rhs)
91{
92 if (auto const c{lhs.in <=> rhs.in}; c != 0)
93 return c;
94 if (auto const c{lhs.out <=> rhs.out}; c != 0)
95 return c;
96
97 // Manually compare optionals
98 if (lhs.domain && rhs.domain)
99 return *lhs.domain <=> *rhs.domain; // Compare values if both exist
100 if (!lhs.domain && rhs.domain)
101 return std::weak_ordering::less; // Empty is considered less
102 if (lhs.domain && !rhs.domain)
103 return std::weak_ordering::greater; // Non-empty is greater
104
105 return std::weak_ordering::equivalent; // Both are empty
106}
109} // namespace ripple
110
111//------------------------------------------------------------------------------
112
113namespace std {
114
115template <>
116struct hash<ripple::Issue>
117 : private boost::base_from_member<std::hash<ripple::Currency>, 0>,
118 private boost::base_from_member<std::hash<ripple::AccountID>, 1>
119{
120private:
122 boost::base_from_member<std::hash<ripple::Currency>, 0>;
124 boost::base_from_member<std::hash<ripple::AccountID>, 1>;
125
126public:
127 hash() = default;
128
131
133 operator()(argument_type const& value) const
134 {
135 value_type result(currency_hash_type::member(value.currency));
136 if (!isXRP(value.currency))
137 boost::hash_combine(
138 result, issuer_hash_type::member(value.account));
139 return result;
140 }
141};
142
143//------------------------------------------------------------------------------
144
145template <>
146struct hash<ripple::Book>
147{
148private:
151
154
155public:
156 hash() = default;
157
160
162 operator()(argument_type const& value) const
163 {
164 value_type result(m_issue_hasher(value.in));
165 boost::hash_combine(result, m_issue_hasher(value.out));
166
167 if (value.domain)
168 boost::hash_combine(result, m_uint256_hasher(*value.domain));
169
170 return result;
171 }
172};
173
174} // namespace std
175
176//------------------------------------------------------------------------------
177
178namespace boost {
179
180template <>
181struct hash<ripple::Issue> : std::hash<ripple::Issue>
182{
183 hash() = default;
184
186 // VFALCO NOTE broken in vs2012
187 // using Base::Base; // inherit ctors
188};
189
190template <>
191struct hash<ripple::Book> : std::hash<ripple::Book>
192{
193 hash() = default;
194
196 // VFALCO NOTE broken in vs2012
197 // using Base::Base; // inherit ctors
198};
199
200} // namespace boost
201
202#endif
Specifies an order book.
Definition Book.h:36
Issue in
Definition Book.h:38
Issue out
Definition Book.h:39
std::optional< uint256 > domain
Definition Book.h:40
Book(Issue const &in_, Issue const &out_, std::optional< uint256 > const &domain_)
Definition Book.h:46
Tracks the number of instances of an object.
A currency issued by an account.
Definition Issue.h:33
AccountID account
Definition Issue.h:36
Currency currency
Definition Issue.h:35
hardened_hash<> hasher
Value hashing function.
Definition base_uint.h:170
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:25
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:199
bool isConsistent(Book const &book)
Definition Book.cpp:29
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:563
Book reversed(Book const &book)
Definition Book.cpp:49
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:647
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:585
STL namespace.
value_type operator()(argument_type const &value) const
Definition Book.h:162
uint256_hasher m_uint256_hasher
Definition Book.h:153
issue_hasher m_issue_hasher
Definition Book.h:152
boost::base_from_member< std::hash< ripple::AccountID >, 1 > issuer_hash_type
Definition Book.h:124
value_type operator()(argument_type const &value) const
Definition Book.h:133
boost::base_from_member< std::hash< ripple::Currency >, 0 > currency_hash_type
Definition Book.h:122