xrpld
Loading...
Searching...
No Matches
PathAsset.h
1#pragma once
2
3#include <xrpl/protocol/Asset.h>
4#include <xrpl/protocol/Concepts.h>
5
6namespace xrpl {
7
8/* Represent STPathElement's asset, which can be Currency or MPTID.
9 */
11{
12private:
14
15public:
16 PathAsset() = default;
17 // Enables comparing Asset and PathAsset
18 PathAsset(Asset const& asset);
19 PathAsset(Currency const& currency) : easset_(currency)
20 {
21 }
22 PathAsset(MPTID const& mpt) : easset_(mpt)
23 {
24 }
25
26 template <ValidPathAsset T>
27 [[nodiscard]] constexpr bool
28 holds() const;
29
30 [[nodiscard]] constexpr bool
31 isXRP() const;
32
33 template <ValidPathAsset T>
34 T const&
35 get() const;
36
37 [[nodiscard]] constexpr std::variant<Currency, MPTID> const&
38 value() const;
39
40 // Custom, generic visit implementation
41 template <typename... Visitors>
42 constexpr auto
43 visit(Visitors&&... visitors) const -> decltype(auto)
44 {
45 // Simple delegation to the reusable utility, passing the internal
46 // variant data.
47 return detail::visit(easset_, std::forward<Visitors>(visitors)...);
48 }
49
50 friend constexpr bool
51 operator==(PathAsset const& lhs, PathAsset const& rhs);
52};
53
54template <ValidPathAsset PA>
56
57template <ValidPathAsset PA>
59
60inline PathAsset::PathAsset(Asset const& asset)
61{
62 asset.visit(
63 [&](Issue const& issue) { easset_ = issue.currency; },
64 [&](MPTIssue const& issue) { easset_ = issue.getMptID(); });
65}
66
67template <ValidPathAsset T>
68constexpr bool
73
74template <ValidPathAsset T>
75[[nodiscard]] [[nodiscard]] T const&
77{
78 if (!holds<T>())
79 Throw<std::runtime_error>("PathAsset doesn't hold requested asset.");
80 return std::get<T>(easset_);
81}
82
83constexpr std::variant<Currency, MPTID> const&
85{
86 return easset_;
87}
88
89constexpr bool
91{
92 return visit(
93 [&](Currency const& currency) { return xrpl::isXRP(currency); },
94 [](MPTID const&) { return false; });
95}
96
97constexpr bool
98operator==(PathAsset const& lhs, PathAsset const& rhs)
99{
100 return std::visit(
101 []<ValidPathAsset TLhs, ValidPathAsset TRhs>(TLhs const& lhs, TRhs const& rhs) {
102 if constexpr (std::is_same_v<TLhs, TRhs>)
103 {
104 return lhs == rhs;
105 }
106 else
107 {
108 return false;
109 }
110 },
111 lhs.value(),
112 rhs.value());
113}
114
115template <typename Hasher>
116void
117hash_append(Hasher& h, PathAsset const& pathAsset)
118{
119 std::visit([&]<ValidPathAsset T>(T const& e) { hash_append(h, e); }, pathAsset.value());
120}
121
122inline bool
123isXRP(PathAsset const& asset)
124{
125 return asset.isXRP();
126}
127
129to_string(PathAsset const& asset);
130
132operator<<(std::ostream& os, PathAsset const& x);
133
134} // namespace xrpl
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:107
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
constexpr bool isXRP() const
Definition PathAsset.h:90
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition PathAsset.h:43
constexpr bool holds() const
Definition PathAsset.h:69
PathAsset(MPTID const &mpt)
Definition PathAsset.h:22
constexpr std::variant< Currency, MPTID > const & value() const
Definition PathAsset.h:84
PathAsset()=default
T const & get() const
std::variant< Currency, MPTID > easset_
Definition PathAsset.h:13
friend constexpr bool operator==(PathAsset const &lhs, PathAsset const &rhs)
Definition PathAsset.h:98
PathAsset(Currency const &currency)
Definition PathAsset.h:19
T forward(T... args)
T holds_alternative(T... args)
T is_same_v
constexpr auto visit(Variant &&v, Visitors &&... visitors) -> decltype(auto)
Definition Concepts.h:74
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:588
constexpr bool kIsCurrencyV
Definition PathAsset.h:55
bool isXRP(AccountID const &c)
Definition AccountID.h:70
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:648
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
constexpr bool kIsMptidV
Definition PathAsset.h:58
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:44
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:175
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
T visit(T... args)