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 
69 int
70 compare(Book const& lhs, Book const& rhs);
71 
74 bool
75 operator==(Book const& lhs, Book const& rhs);
76 bool
77 operator!=(Book const& lhs, Book const& rhs);
82 bool
83 operator<(Book const& lhs, Book const& rhs);
84 bool
85 operator>(Book const& lhs, Book const& rhs);
86 bool
87 operator>=(Book const& lhs, Book const& rhs);
88 bool
89 operator<=(Book const& lhs, Book const& rhs);
92 } // namespace ripple
93 
94 //------------------------------------------------------------------------------
95 
96 namespace std {
97 
98 template <>
99 struct hash<ripple::Issue>
100  : private boost::base_from_member<std::hash<ripple::Currency>, 0>,
101  private boost::base_from_member<std::hash<ripple::AccountID>, 1>
102 {
103 private:
104  using currency_hash_type =
105  boost::base_from_member<std::hash<ripple::Currency>, 0>;
106  using issuer_hash_type =
107  boost::base_from_member<std::hash<ripple::AccountID>, 1>;
108 
109 public:
110  explicit hash() = default;
111 
114 
115  value_type
116  operator()(argument_type const& value) const
117  {
118  value_type result(currency_hash_type::member(value.currency));
119  if (!isXRP(value.currency))
120  boost::hash_combine(
121  result, issuer_hash_type::member(value.account));
122  return result;
123  }
124 };
125 
126 //------------------------------------------------------------------------------
127 
128 template <>
129 struct hash<ripple::Book>
130 {
131 private:
133 
135 
136 public:
137  explicit hash() = default;
138 
141 
142  value_type
143  operator()(argument_type const& value) const
144  {
145  value_type result(m_hasher(value.in));
146  boost::hash_combine(result, m_hasher(value.out));
147  return result;
148  }
149 };
150 
151 } // namespace std
152 
153 //------------------------------------------------------------------------------
154 
155 namespace boost {
156 
157 template <>
158 struct hash<ripple::Issue> : std::hash<ripple::Issue>
159 {
160  explicit hash() = default;
161 
163  // VFALCO NOTE broken in vs2012
164  // using Base::Base; // inherit ctors
165 };
166 
167 template <>
168 struct hash<ripple::Book> : std::hash<ripple::Book>
169 {
170  explicit hash() = default;
171 
173  // VFALCO NOTE broken in vs2012
174  // using Base::Base; // inherit ctors
175 };
176 
177 } // namespace boost
178 
179 #endif
ripple::operator>
bool operator>(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:584
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:107
ripple::isConsistent
bool isConsistent(Book const &book)
Definition: Book.cpp:25
ripple::Book::out
Issue out
Definition: Book.h:37
ripple::operator>=
bool operator>=(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:591
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:161
ripple::operator<=
bool operator<=(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:577
std::hash< ripple::Book >
Definition: Book.h:129
ripple::operator<
bool operator<(CanonicalTXSet::Key const &lhs, CanonicalTXSet::Key const &rhs)
Definition: CanonicalTXSet.cpp:25
std::ostream
STL class.
ripple::compare
int compare(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:553
ripple::reversed
Book reversed(Book const &book)
Definition: Book.cpp:45
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:171
std::hash< ripple::Book >::m_hasher
hasher m_hasher
Definition: Book.h:134
std::hash< ripple::Book >::operator()
value_type operator()(argument_type const &value) const
Definition: Book.h:143
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:105
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:99
std::hash< ripple::Issue >::operator()
value_type operator()(argument_type const &value) const
Definition: Book.h:116
ripple::Book::in
Issue in
Definition: Book.h:36
ripple::Issue::account
AccountID account
Definition: Issue.h:38
std::hash