diff --git a/src/ripple/protocol/Asset.h b/src/ripple/protocol/Asset.h index 9fc99fa5ad4..8740d39b377 100644 --- a/src/ripple/protocol/Asset.h +++ b/src/ripple/protocol/Asset.h @@ -58,23 +58,23 @@ class Asset return *this; } - asset_type const& + asset_type constexpr const& asset() const { return asset_; } - bool + bool constexpr isCFT() const { return std::holds_alternative(asset_); } - bool + bool constexpr isCurrency() const { return std::holds_alternative(asset_); } - bool + bool constexpr isXRP() const { return isCurrency() && ripple::isXRP(std::get(asset_)); @@ -113,14 +113,20 @@ class Asset return std::get_if(asset_); } - template - requires(std::is_same_v || std::is_same_v) operator T - const &() const + operator Currency const&() const { - assert(std::holds_alternative(asset_)); - if (!std::holds_alternative(asset_)) - Throw("Invalid Asset cast"); - return std::get(asset_); + assert(std::holds_alternative(asset_)); + if (!std::holds_alternative(asset_)) + Throw("Invalid Currency cast"); + return std::get(asset_); + } + + operator CFT const&() const + { + assert(std::holds_alternative(asset_)); + if (!std::holds_alternative(asset_)) + Throw("Invalid CFT cast"); + return std::get(asset_); } friend bool @@ -169,6 +175,23 @@ class Asset { return !(a1 < a2); } + friend inline constexpr std::weak_ordering + operator<=>(Asset const& lhs, Asset const& rhs) + { + assert(lhs.isCurrency() == rhs.isCurrency()); + if (lhs.isCurrency() != rhs.isCurrency()) + Throw("Invalid Asset comparison"); + if (lhs.isCurrency()) + return std::get(lhs.asset_) <=> + std::get(rhs.asset_); + if (auto const c{ + std::get(lhs.asset_).second <=> + std::get(rhs.asset_).second}; + c != 0) + return c; + return std::get(lhs.asset_).first <=> + std::get(rhs.asset_).first; + } friend std::string to_string(Asset const& a) { diff --git a/src/ripple/protocol/Book.h b/src/ripple/protocol/Book.h index 5c3681ba21d..546321ab2c0 100644 --- a/src/ripple/protocol/Book.h +++ b/src/ripple/protocol/Book.h @@ -76,7 +76,7 @@ operator==(Book const& lhs, Book const& rhs) /** Strict weak ordering. */ /** @{ */ -[[nodiscard]] inline std::weak_ordering +[[nodiscard]] inline constexpr std::weak_ordering operator<=>(Book const& lhs, Book const& rhs) { if (auto const c{lhs.in <=> rhs.in}; c != 0) diff --git a/src/ripple/protocol/Issue.h b/src/ripple/protocol/Issue.h index c5e4282fea6..fd2b60de74d 100644 --- a/src/ripple/protocol/Issue.h +++ b/src/ripple/protocol/Issue.h @@ -104,7 +104,7 @@ class Issue return *this; } - Asset const& + Asset constexpr const& asset() const { return asset_; @@ -185,7 +185,7 @@ operator==(Issue const& lhs, Issue const& rhs) /** Strict weak ordering. */ /** @{ */ -[[nodiscard]] inline std::weak_ordering +[[nodiscard]] inline constexpr std::weak_ordering operator<=>(Issue const& lhs, Issue const& rhs) { if (auto const c{lhs.asset().asset() <=> rhs.asset().asset()}; c != 0)