#pragma once #include #include namespace xrpl { /* Represent STPathElement's asset, which can be Currency or MPTID. */ class PathAsset { private: std::variant easset_; public: PathAsset() = default; // Enables comparing Asset and PathAsset PathAsset(Asset const& asset); PathAsset(Currency const& currency) : easset_(currency) { } PathAsset(MPTID const& mpt) : easset_(mpt) { } template constexpr bool holds() const; constexpr bool isXRP() const; template T const& get() const; constexpr std::variant const& value() const; // Custom, generic visit implementation template constexpr auto visit(Visitors&&... visitors) const -> decltype(auto) { // Simple delegation to the reusable utility, passing the internal // variant data. return detail::visit(easset_, std::forward(visitors)...); } friend constexpr bool operator==(PathAsset const& lhs, PathAsset const& rhs); }; template constexpr bool is_currency_v = std::is_same_v; template constexpr bool is_mptid_v = std::is_same_v; inline PathAsset::PathAsset(Asset const& asset) { asset.visit( [&](Issue const& issue) { easset_ = issue.currency; }, [&](MPTIssue const& issue) { easset_ = issue.getMptID(); }); } template constexpr bool PathAsset::holds() const { return std::holds_alternative(easset_); } template T const& PathAsset::get() const { if (!holds()) Throw("PathAsset doesn't hold requested asset."); return std::get(easset_); } constexpr std::variant const& PathAsset::value() const { return easset_; } constexpr bool PathAsset::isXRP() const { return visit( [&](Currency const& currency) { return xrpl::isXRP(currency); }, [](MPTID const&) { return false; }); } constexpr bool operator==(PathAsset const& lhs, PathAsset const& rhs) { return std::visit( [](TLhs const& lhs_, TRhs const& rhs_) { if constexpr (std::is_same_v) { return lhs_ == rhs_; } else { return false; } }, lhs.value(), rhs.value()); } template void hash_append(Hasher& h, PathAsset const& pathAsset) { std::visit([&](T const& e) { hash_append(h, e); }, pathAsset.value()); } inline bool isXRP(PathAsset const& asset) { return asset.isXRP(); } std::string to_string(PathAsset const& asset); std::ostream& operator<<(std::ostream& os, PathAsset const& x); } // namespace xrpl