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