mirror of
https://github.com/XRPLF/rippled.git
synced 2026-10-09 13:18:09 +00:00
254 lines
5.7 KiB
C++
254 lines
5.7 KiB
C++
#pragma once
|
|
|
|
#include <xrpl/basics/CountedObject.h>
|
|
#include <xrpl/basics/base_uint.h>
|
|
#include <xrpl/protocol/AccountID.h>
|
|
#include <xrpl/protocol/Asset.h>
|
|
#include <xrpl/protocol/Issue.h>
|
|
#include <xrpl/protocol/MPTIssue.h>
|
|
#include <xrpl/protocol/UintTypes.h>
|
|
|
|
#include <boost/utility/base_from_member.hpp>
|
|
|
|
#include <compare>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <string>
|
|
|
|
namespace xrpl {
|
|
|
|
/**
|
|
* Specifies an order book.
|
|
* The order book is a pair of Issues called in and out.
|
|
* @see Issue.
|
|
*/
|
|
class Book final : public CountedObject<Book>
|
|
{
|
|
public:
|
|
Asset in;
|
|
Asset out;
|
|
std::optional<UInt256> domain;
|
|
|
|
Book() = default;
|
|
|
|
Book(Asset const& in, Asset const& out, std::optional<UInt256> const& domain)
|
|
: in(in), out(out), domain(domain)
|
|
{
|
|
}
|
|
};
|
|
|
|
bool
|
|
isConsistent(Book const& book);
|
|
|
|
std::string
|
|
to_string(Book const& book);
|
|
|
|
std::ostream&
|
|
operator<<(std::ostream& os, Book const& x);
|
|
|
|
template <class Hasher>
|
|
void
|
|
hash_append(Hasher& h, Book const& b)
|
|
{
|
|
using beast::hash_append;
|
|
hash_append(h, b.in, b.out);
|
|
if (b.domain)
|
|
hash_append(h, *b.domain);
|
|
}
|
|
|
|
Book
|
|
reversed(Book const& book);
|
|
|
|
/**
|
|
* Equality comparison.
|
|
*/
|
|
/** @{ */
|
|
[[nodiscard]] constexpr bool
|
|
operator==(Book const& lhs, Book const& rhs)
|
|
{
|
|
return (lhs.in == rhs.in) && (lhs.out == rhs.out) && (lhs.domain == rhs.domain);
|
|
}
|
|
/** @} */
|
|
|
|
/**
|
|
* Strict weak ordering.
|
|
*/
|
|
/** @{ */
|
|
[[nodiscard]] constexpr std::weak_ordering
|
|
operator<=>(Book const& lhs, Book const& rhs)
|
|
{
|
|
if (auto const c{lhs.in <=> rhs.in}; c != 0) // NOLINT(modernize-use-nullptr)
|
|
return c;
|
|
if (auto const c{lhs.out <=> rhs.out}; c != 0) // NOLINT(modernize-use-nullptr)
|
|
return c;
|
|
|
|
// Manually compare optionals
|
|
if (lhs.domain && rhs.domain)
|
|
return *lhs.domain <=> *rhs.domain; // Compare values if both exist
|
|
if (!lhs.domain && rhs.domain)
|
|
return std::weak_ordering::less; // Empty is considered less
|
|
if (lhs.domain && !rhs.domain)
|
|
return std::weak_ordering::greater; // Non-empty is greater
|
|
|
|
return std::weak_ordering::equivalent; // Both are empty
|
|
}
|
|
/** @} */
|
|
|
|
} // namespace xrpl
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
struct hash<xrpl::Issue> : private boost::base_from_member<std::hash<xrpl::Currency>, 0>,
|
|
private boost::base_from_member<std::hash<xrpl::AccountID>, 1>
|
|
{
|
|
private:
|
|
using CurrencyHashType = boost::base_from_member<std::hash<xrpl::Currency>, 0>;
|
|
using IssuerHashType = boost::base_from_member<std::hash<xrpl::AccountID>, 1>;
|
|
|
|
public:
|
|
hash() = default;
|
|
|
|
using value_type = std::size_t;
|
|
using argument_type = xrpl::Issue;
|
|
|
|
value_type
|
|
operator()(argument_type const& value) const
|
|
{
|
|
value_type result(CurrencyHashType::member(value.currency));
|
|
if (!isXRP(value.currency))
|
|
boost::hash_combine(result, IssuerHashType::member(value.account));
|
|
return result;
|
|
}
|
|
};
|
|
|
|
template <>
|
|
struct hash<xrpl::MPTIssue> : private boost::base_from_member<std::hash<xrpl::MPTID>, 0>
|
|
{
|
|
private:
|
|
using IdHashType = boost::base_from_member<std::hash<xrpl::MPTID>, 0>;
|
|
|
|
public:
|
|
hash() = default;
|
|
|
|
using value_type = std::size_t;
|
|
using argument_type = xrpl::MPTIssue;
|
|
|
|
value_type
|
|
operator()(argument_type const& value) const
|
|
{
|
|
value_type const result(IdHashType::member(value.getMptID()));
|
|
return result;
|
|
}
|
|
};
|
|
|
|
template <>
|
|
struct hash<xrpl::Asset>
|
|
{
|
|
private:
|
|
using value_type = std::size_t;
|
|
using argument_type = xrpl::Asset;
|
|
|
|
using IssueHasher = std::hash<xrpl::Issue>;
|
|
using MptissueHasher = std::hash<xrpl::MPTIssue>;
|
|
|
|
IssueHasher mIssueHasher_;
|
|
MptissueHasher mMptissueHasher_;
|
|
|
|
public:
|
|
hash() = default;
|
|
|
|
value_type
|
|
operator()(argument_type const& asset) const
|
|
{
|
|
return asset.visit(
|
|
[&](xrpl::Issue const& issue) {
|
|
value_type const result(mIssueHasher_(issue));
|
|
return result;
|
|
},
|
|
[&](xrpl::MPTIssue const& issue) {
|
|
value_type const result(mMptissueHasher_(issue));
|
|
return result;
|
|
});
|
|
}
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <>
|
|
struct hash<xrpl::Book>
|
|
{
|
|
private:
|
|
using AssetHasher = std::hash<xrpl::Asset>;
|
|
using UInt256Hasher = xrpl::UInt256::hasher;
|
|
|
|
AssetHasher issueHasher_;
|
|
UInt256Hasher uint256Hasher_;
|
|
|
|
public:
|
|
hash() = default;
|
|
|
|
using value_type = std::size_t;
|
|
using argument_type = xrpl::Book;
|
|
|
|
value_type
|
|
operator()(argument_type const& value) const
|
|
{
|
|
value_type result(issueHasher_(value.in));
|
|
boost::hash_combine(result, issueHasher_(value.out));
|
|
|
|
if (value.domain)
|
|
boost::hash_combine(result, uint256Hasher_(*value.domain));
|
|
|
|
return result;
|
|
}
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
namespace boost {
|
|
|
|
template <>
|
|
struct hash<xrpl::Issue> : std::hash<xrpl::Issue>
|
|
{
|
|
hash() = default;
|
|
|
|
using Base = std::hash<xrpl::Issue>;
|
|
// VFALCO NOTE broken in vs2012
|
|
// using Base::Base; // inherit ctors
|
|
};
|
|
|
|
template <>
|
|
struct hash<xrpl::MPTIssue> : std::hash<xrpl::MPTIssue>
|
|
{
|
|
hash() = default;
|
|
|
|
using Base = std::hash<xrpl::MPTIssue>;
|
|
};
|
|
|
|
template <>
|
|
struct hash<xrpl::Asset> : std::hash<xrpl::Asset>
|
|
{
|
|
hash() = default;
|
|
|
|
using Base = std::hash<xrpl::Asset>;
|
|
};
|
|
|
|
template <>
|
|
struct hash<xrpl::Book> : std::hash<xrpl::Book>
|
|
{
|
|
hash() = default;
|
|
|
|
using Base = std::hash<xrpl::Book>;
|
|
// VFALCO NOTE broken in vs2012
|
|
// using Base::Base; // inherit ctors
|
|
};
|
|
|
|
} // namespace boost
|