rippled
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 <ripple/basics/CountedObject.h>
24 #include <ripple/protocol/Issue.h>
25 #include <boost/utility/base_from_member.hpp>
26 
27 namespace ripple {
28 
33 class Book final : public CountedObject<Book>
34 {
35 public:
38 
39  Book()
40  {
41  }
42 
43  Book(Issue const& in_, Issue const& out_) : in(in_), out(out_)
44  {
45  }
46 };
47 
48 bool
49 isConsistent(Book const& book);
50 
52 to_string(Book const& book);
53 
55 operator<<(std::ostream& os, Book const& x);
56 
57 template <class Hasher>
58 void
59 hash_append(Hasher& h, Book const& b)
60 {
61  using beast::hash_append;
62  hash_append(h, b.in, b.out);
63 }
64 
65 Book
66 reversed(Book const& book);
67 
70 [[nodiscard]] inline constexpr bool
71 operator==(Book const& lhs, Book const& rhs)
72 {
73  return (lhs.in == rhs.in) && (lhs.out == rhs.out);
74 }
79 [[nodiscard]] inline constexpr std::weak_ordering
80 operator<=>(Book const& lhs, Book const& rhs)
81 {
82  if (auto const c{lhs.in <=> rhs.in}; c != 0)
83  return c;
84  return lhs.out <=> rhs.out;
85 }
88 } // namespace ripple
89 
90 //------------------------------------------------------------------------------
91 
92 namespace std {
93 
94 template <>
95 struct hash<ripple::Issue>
96  : private boost::base_from_member<std::hash<ripple::Currency>, 0>,
97  private boost::base_from_member<std::hash<ripple::AccountID>, 1>
98 {
99 private:
100  using currency_hash_type =
101  boost::base_from_member<std::hash<ripple::Currency>, 0>;
102  using issuer_hash_type =
103  boost::base_from_member<std::hash<ripple::AccountID>, 1>;
104 
105 public:
106  explicit hash() = default;
107 
110 
111  value_type
112  operator()(argument_type const& value) const
113  {
114  value_type result(currency_hash_type::member(value.currency));
115  if (!isXRP(value.currency))
116  boost::hash_combine(
117  result, issuer_hash_type::member(value.account));
118  return result;
119  }
120 };
121 
122 //------------------------------------------------------------------------------
123 
124 template <>
125 struct hash<ripple::Book>
126 {
127 private:
129 
131 
132 public:
133  explicit hash() = default;
134 
137 
138  value_type
139  operator()(argument_type const& value) const
140  {
141  value_type result(m_hasher(value.in));
142  boost::hash_combine(result, m_hasher(value.out));
143  return result;
144  }
145 };
146 
147 } // namespace std
148 
149 //------------------------------------------------------------------------------
150 
151 namespace boost {
152 
153 template <>
154 struct hash<ripple::Issue> : std::hash<ripple::Issue>
155 {
156  explicit hash() = default;
157 
159  // VFALCO NOTE broken in vs2012
160  // using Base::Base; // inherit ctors
161 };
162 
163 template <>
164 struct hash<ripple::Book> : std::hash<ripple::Book>
165 {
166  explicit hash() = default;
167 
169  // VFALCO NOTE broken in vs2012
170  // using Base::Base; // inherit ctors
171 };
172 
173 } // namespace boost
174 
175 #endif
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
std::string
STL class.
std::hash< ripple::Issue >::issuer_hash_type
boost::base_from_member< std::hash< ripple::AccountID >, 1 > issuer_hash_type
Definition: Book.h:103
ripple::isConsistent
bool isConsistent(Book const &book)
Definition: Book.cpp:25
ripple::Book::out
Issue out
Definition: Book.h:37
std::weak_ordering
ripple::operator<=>
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:554
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
boost
Definition: IPAddress.h:103
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
std::hash< ripple::Book >
Definition: Book.h:125
std::ostream
STL class.
ripple::reversed
Book reversed(Book const &book)
Definition: Book.cpp:45
std::hash< ripple::Book >::m_hasher
hasher m_hasher
Definition: Book.h:130
std::hash< ripple::Book >::operator()
value_type operator()(argument_type const &value) const
Definition: Book.h:139
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::hash< ripple::Issue >::currency_hash_type
boost::base_from_member< std::hash< ripple::Currency >, 0 > currency_hash_type
Definition: Book.h:101
std
STL namespace.
beast::hash_append
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:236
std::size_t
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::Book
Specifies an order book.
Definition: Book.h:33
ripple::Book::Book
Book()
Definition: Book.h:39
ripple::hash_append
void hash_append(Hasher &h, ValidatorBlobInfo const &blobInfo)
Definition: ValidatorList.h:897
ripple::Book::Book
Book(Issue const &in_, Issue const &out_)
Definition: Book.h:43
std::hash< ripple::Issue >
Definition: Book.h:95
std::hash< ripple::Issue >::operator()
value_type operator()(argument_type const &value) const
Definition: Book.h:112
ripple::Book::in
Issue in
Definition: Book.h:36
ripple::Issue::account
AccountID account
Definition: Issue.h:38
std::hash