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/protocol/Issue.h>
25
26#include <boost/utility/base_from_member.hpp>
27
28namespace ripple {
29
34class Book final : public CountedObject<Book>
35{
36public:
39
41 {
42 }
43
44 Book(Issue const& in_, Issue const& out_) : in(in_), out(out_)
45 {
46 }
47};
48
49bool
50isConsistent(Book const& book);
51
53to_string(Book const& book);
54
56operator<<(std::ostream& os, Book const& x);
57
58template <class Hasher>
59void
60hash_append(Hasher& h, Book const& b)
61{
63 hash_append(h, b.in, b.out);
64}
65
66Book
67reversed(Book const& book);
68
71[[nodiscard]] inline constexpr bool
72operator==(Book const& lhs, Book const& rhs)
73{
74 return (lhs.in == rhs.in) && (lhs.out == rhs.out);
75}
80[[nodiscard]] inline constexpr std::weak_ordering
81operator<=>(Book const& lhs, Book const& rhs)
82{
83 if (auto const c{lhs.in <=> rhs.in}; c != 0)
84 return c;
85 return lhs.out <=> rhs.out;
86}
89} // namespace ripple
90
91//------------------------------------------------------------------------------
92
93namespace std {
94
95template <>
96struct hash<ripple::Issue>
97 : private boost::base_from_member<std::hash<ripple::Currency>, 0>,
98 private boost::base_from_member<std::hash<ripple::AccountID>, 1>
99{
100private:
102 boost::base_from_member<std::hash<ripple::Currency>, 0>;
104 boost::base_from_member<std::hash<ripple::AccountID>, 1>;
105
106public:
107 explicit hash() = default;
108
111
113 operator()(argument_type const& value) const
114 {
115 value_type result(currency_hash_type::member(value.currency));
116 if (!isXRP(value.currency))
117 boost::hash_combine(
118 result, issuer_hash_type::member(value.account));
119 return result;
120 }
121};
122
123//------------------------------------------------------------------------------
124
125template <>
126struct hash<ripple::Book>
127{
128private:
130
132
133public:
134 explicit hash() = default;
135
138
140 operator()(argument_type const& value) const
141 {
142 value_type result(m_hasher(value.in));
143 boost::hash_combine(result, m_hasher(value.out));
144 return result;
145 }
146};
147
148} // namespace std
149
150//------------------------------------------------------------------------------
151
152namespace boost {
153
154template <>
155struct hash<ripple::Issue> : std::hash<ripple::Issue>
156{
157 explicit hash() = default;
158
160 // VFALCO NOTE broken in vs2012
161 // using Base::Base; // inherit ctors
162};
163
164template <>
165struct hash<ripple::Book> : std::hash<ripple::Book>
166{
167 explicit hash() = default;
168
170 // VFALCO NOTE broken in vs2012
171 // using Base::Base; // inherit ctors
172};
173
174} // namespace boost
175
176#endif
Specifies an order book.
Definition: Book.h:35
Book()
Definition: Book.h:40
Issue in
Definition: Book.h:37
Issue out
Definition: Book.h:38
Book(Issue const &in_, Issue const &out_)
Definition: Book.h:44
Tracks the number of instances of an object.
A currency issued by an account.
Definition: Issue.h:36
AccountID account
Definition: Issue.h:39
Currency currency
Definition: Issue.h:38
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.
Definition: hash_append.h:237
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:637
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:140
boost::base_from_member< std::hash< ripple::AccountID >, 1 > issuer_hash_type
Definition: Book.h:104
value_type operator()(argument_type const &value) const
Definition: Book.h:113
boost::base_from_member< std::hash< ripple::Currency >, 0 > currency_hash_type
Definition: Book.h:102