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 <cstdint>
9#include <ostream>
10#include <stdexcept>
11#include <string>
12#include <type_traits>
13#include <variant>
14
15namespace xrpl {
16
17/* Represent STPathElement's asset, which can be Currency or MPTID.
18 */
20{
21private:
23
24public:
25 PathAsset() = default;
26 // Enables comparing Asset and PathAsset
27 PathAsset(Asset const& asset);
28 PathAsset(Currency const& currency) : easset_(currency)
29 {
30 }
31 PathAsset(MPTID const& mpt) : easset_(mpt)
32 {
33 }
34
35 template <ValidPathAsset T>
36 [[nodiscard]] constexpr bool
37 holds() const;
38
39 [[nodiscard]] constexpr bool
40 isXRP() const;
41
42 template <ValidPathAsset T>
43 T const&
44 get() const;
45
46 [[nodiscard]] constexpr std::variant<Currency, MPTID> const&
47 value() const;
48
49 // Custom, generic visit implementation
50 template <typename... Visitors>
51 constexpr auto
52 visit(Visitors&&... visitors) const -> decltype(auto)
53 {
54 // Simple delegation to the reusable utility, passing the internal
55 // variant data.
56 return detail::visit(easset_, std::forward<Visitors>(visitors)...);
57 }
58
59 friend constexpr bool
60 operator==(PathAsset const& lhs, PathAsset const& rhs);
61};
62
63template <ValidPathAsset PA>
65
66template <ValidPathAsset PA>
68
69inline PathAsset::PathAsset(Asset const& asset)
70{
71 asset.visit(
72 [&](Issue const& issue) { easset_ = issue.currency; },
73 [&](MPTIssue const& issue) { easset_ = issue.getMptID(); });
74}
75
76template <ValidPathAsset T>
77constexpr bool
82
83template <ValidPathAsset T>
84[[nodiscard]] [[nodiscard]] T const&
86{
87 if (!holds<T>())
88 Throw<std::runtime_error>("PathAsset doesn't hold requested asset.");
89 return std::get<T>(easset_);
90}
91
92constexpr std::variant<Currency, MPTID> const&
94{
95 return easset_;
96}
97
98constexpr bool
100{
101 return visit(
102 [&](Currency const& currency) { return xrpl::isXRP(currency); },
103 [](MPTID const&) { return false; });
104}
105
106constexpr bool
107operator==(PathAsset const& lhs, PathAsset const& rhs)
108{
109 return std::visit(
110 []<ValidPathAsset TLhs, ValidPathAsset TRhs>(TLhs const& lhs, TRhs const& rhs) {
111 if constexpr (std::is_same_v<TLhs, TRhs>)
112 {
113 return lhs == rhs;
114 }
115 else
116 {
117 return false;
118 }
119 },
120 lhs.value(),
121 rhs.value());
122}
123
124template <typename Hasher>
125void
126hash_append(Hasher& h, PathAsset const& pathAsset) noexcept
127{
128 using beast::hash_append;
129 using Variant = std::remove_cvref_t<decltype(pathAsset.value())>;
130
131 static_assert(
133 "PathAsset's discriminant must fit in a byte, leaving 0xFF reserved.");
134
135 // std::visit is not noexcept: it throws bad_variant_access when the variant
136 // is valueless_by_exception.
137 if (pathAsset.value().valueless_by_exception()) [[unlikely]]
138 {
139 hash_append(h, static_cast<std::uint8_t>(0xFFu));
140 return;
141 }
142
143 hash_append(h, static_cast<std::uint8_t>(pathAsset.value().index()));
145 [&]<ValidPathAsset T>(T const& e) noexcept {
146 static_assert(
147 noexcept(hash_append(h, e)),
148 "Every PathAsset alternative must be nothrow-hashable.");
149 hash_append(h, e);
150 },
151 pathAsset.value());
152}
153
154inline bool
155isXRP(PathAsset const& asset)
156{
157 return asset.isXRP();
158}
159
161to_string(PathAsset const& asset);
162
164operator<<(std::ostream& os, PathAsset const& x);
165
166} // 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:99
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition PathAsset.h:52
constexpr bool holds() const
Definition PathAsset.h:78
PathAsset(MPTID const &mpt)
Definition PathAsset.h:31
constexpr std::variant< Currency, MPTID > const & value() const
Definition PathAsset.h:93
PathAsset()=default
T const & get() const
std::variant< Currency, MPTID > easset_
Definition PathAsset.h:22
friend constexpr bool operator==(PathAsset const &lhs, PathAsset const &rhs)
Definition PathAsset.h:107
PathAsset(Currency const &currency)
Definition PathAsset.h:28
T forward(T... args)
T holds_alternative(T... args)
T is_same_v
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
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:612
constexpr bool kIsCurrencyV
Definition PathAsset.h:64
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:672
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:657
constexpr bool kIsMptidV
Definition PathAsset.h:67
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 variant_size_v
T visit(T... args)