xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
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
15
namespace
xrpl
{
16
17
/* Represent STPathElement's asset, which can be Currency or MPTID.
18
*/
19
class
PathAsset
20
{
21
private
:
22
std::variant<Currency, MPTID>
easset_
;
23
24
public
:
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
<Val
id
PathAsset T>
36
[[nodiscard]]
constexpr
bool
37
holds
()
const
;
38
39
[[nodiscard]]
constexpr
bool
40
isXRP
()
const
;
41
42
template
<Val
id
PathAsset 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
63
template
<Val
id
PathAsset PA>
64
constexpr
bool
kIsCurrencyV
=
std::is_same_v<PA, Currency>
;
65
66
template
<Val
id
PathAsset PA>
67
constexpr
bool
kIsMptidV
=
std::is_same_v<PA, MPTID>
;
68
69
inline
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
76
template
<Val
id
PathAsset T>
77
constexpr
bool
78
PathAsset::holds
()
const
79
{
80
return
std::holds_alternative<T>
(
easset_
);
81
}
82
83
template
<Val
id
PathAsset T>
84
[[nodiscard]] [[nodiscard]] T
const
&
85
PathAsset::get
()
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
92
constexpr
std::variant<Currency, MPTID>
const
&
93
PathAsset::value
()
const
94
{
95
return
easset_
;
96
}
97
98
constexpr
bool
99
PathAsset::isXRP
()
const
100
{
101
return
visit
(
102
[&](
Currency
const
& currency) {
return
xrpl::isXRP
(currency); },
103
[](
MPTID
const
&) {
return
false
; });
104
}
105
106
constexpr
bool
107
operator==
(
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
124
template
<
typename
Hasher>
125
void
126
hash_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
(
132
std::variant_size_v<Variant>
< 0xFFu,
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()));
144
std::visit
(
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
154
inline
bool
155
isXRP
(
PathAsset
const
& asset)
156
{
157
return
asset.
isXRP
();
158
}
159
160
std::string
161
to_string
(PathAsset
const
& asset);
162
163
std::ostream
&
164
operator<<
(
std::ostream
& os, PathAsset
const
& x);
165
166
}
// namespace xrpl
std::ostream
std::string
xrpl::Asset
Definition
Asset.h:52
xrpl::Asset::visit
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition
Asset.h:117
xrpl::Issue
A currency issued by an account.
Definition
Issue.h:18
xrpl::Issue::currency
Currency currency
Definition
Issue.h:20
xrpl::MPTIssue
Definition
MPTIssue.h:23
xrpl::PathAsset
Definition
PathAsset.h:20
xrpl::PathAsset::isXRP
constexpr bool isXRP() const
Definition
PathAsset.h:99
xrpl::PathAsset::visit
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition
PathAsset.h:52
xrpl::PathAsset::holds
constexpr bool holds() const
Definition
PathAsset.h:78
xrpl::PathAsset::PathAsset
PathAsset(MPTID const &mpt)
Definition
PathAsset.h:31
xrpl::PathAsset::value
constexpr std::variant< Currency, MPTID > const & value() const
Definition
PathAsset.h:93
xrpl::PathAsset::PathAsset
PathAsset()=default
xrpl::PathAsset::get
T const & get() const
xrpl::PathAsset::easset_
std::variant< Currency, MPTID > easset_
Definition
PathAsset.h:22
xrpl::PathAsset::operator==
friend constexpr bool operator==(PathAsset const &lhs, PathAsset const &rhs)
Definition
PathAsset.h:107
xrpl::PathAsset::PathAsset
PathAsset(Currency const ¤cy)
Definition
PathAsset.h:28
xrpl::ValidPathAsset
Definition
Concepts.h:29
cstdint
std::forward
T forward(T... args)
std::holds_alternative
T holds_alternative(T... args)
std::uint8_t
std::is_same_v
T is_same_v
beast::hash_append
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition
hash_append.h:206
xrpl::detail::visit
constexpr auto visit(Variant &&v, Visitors &&... visitors) -> decltype(auto)
Definition
Concepts.h:74
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::operator==
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition
base_uint.h:612
xrpl::kIsCurrencyV
constexpr bool kIsCurrencyV
Definition
PathAsset.h:64
xrpl::isXRP
bool isXRP(AccountID const &c)
Definition
AccountID.h:84
xrpl::Currency
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition
UintTypes.h:42
xrpl::operator<<
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition
base_uint.h:672
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::kIsMptidV
constexpr bool kIsMptidV
Definition
PathAsset.h:67
xrpl::MPTID
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
xrpl::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition
Slice.h:194
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
ostream
std::remove_cvref_t
stdexcept
string
type_traits
variant
std::variant_size_v
T variant_size_v
std::visit
T visit(T... args)
Generated by
1.17.0