rippled
Loading...
Searching...
No Matches
AmountConversions.h
1#ifndef XRPL_PROTOCOL_AMOUNTCONVERSION_H_INCLUDED
2#define XRPL_PROTOCOL_AMOUNTCONVERSION_H_INCLUDED
3
4#include <xrpl/protocol/IOUAmount.h>
5#include <xrpl/protocol/STAmount.h>
6#include <xrpl/protocol/XRPAmount.h>
7
8#include <type_traits>
9
10namespace xrpl {
11
12inline STAmount
13toSTAmount(IOUAmount const& iou, Issue const& iss)
14{
15 bool const isNeg = iou.signum() < 0;
16 std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
17 return STAmount(iss, umant, iou.exponent(), isNeg, STAmount::unchecked());
18}
19
20inline STAmount
22{
23 return toSTAmount(iou, noIssue());
24}
25
26inline STAmount
28{
29 bool const isNeg = xrp.signum() < 0;
30 std::uint64_t const umant = isNeg ? -xrp.drops() : xrp.drops();
31 return STAmount(umant, isNeg);
32}
33
34inline STAmount
35toSTAmount(XRPAmount const& xrp, Issue const& iss)
36{
37 XRPL_ASSERT(
38 isXRP(iss.account) && isXRP(iss.currency), "xrpl::toSTAmount : is XRP");
39 return toSTAmount(xrp);
40}
41
42template <class T>
43T
44toAmount(STAmount const& amt) = delete;
45
46template <>
49{
50 return amt;
51}
52
53template <>
56{
57 XRPL_ASSERT(
59 "xrpl::toAmount<IOUAmount> : maximum mantissa");
60 bool const isNeg = amt.negative();
61 std::int64_t const sMant =
62 isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
63
64 XRPL_ASSERT(!isXRP(amt), "xrpl::toAmount<IOUAmount> : is not XRP");
65 return IOUAmount(sMant, amt.exponent());
66}
67
68template <>
71{
72 XRPL_ASSERT(
74 "xrpl::toAmount<XRPAmount> : maximum mantissa");
75 bool const isNeg = amt.negative();
76 std::int64_t const sMant =
77 isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
78
79 XRPL_ASSERT(isXRP(amt), "xrpl::toAmount<XRPAmount> : is XRP");
80 return XRPAmount(sMant);
81}
82
83template <class T>
84T
85toAmount(IOUAmount const& amt) = delete;
86
87template <>
90{
91 return amt;
92}
93
94template <class T>
95T
96toAmount(XRPAmount const& amt) = delete;
97
98template <>
101{
102 return amt;
103}
104
105template <typename T>
106T
108 Issue const& issue,
109 Number const& n,
111{
113 if (isXRP(issue))
114 Number::setround(mode);
115
116 if constexpr (std::is_same_v<IOUAmount, T>)
117 return IOUAmount(n);
118 else if constexpr (std::is_same_v<XRPAmount, T>)
119 return XRPAmount(static_cast<std::int64_t>(n));
120 else if constexpr (std::is_same_v<STAmount, T>)
121 {
122 if (isXRP(issue))
123 return STAmount(issue, static_cast<std::int64_t>(n));
124 return STAmount(issue, n.mantissa(), n.exponent());
125 }
126 else
127 {
128 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
129 static_assert(alwaysFalse, "Unsupported type for toAmount");
130 }
131}
132
133template <typename T>
134T
135toMaxAmount(Issue const& issue)
136{
137 if constexpr (std::is_same_v<IOUAmount, T>)
139 else if constexpr (std::is_same_v<XRPAmount, T>)
140 return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
141 else if constexpr (std::is_same_v<STAmount, T>)
142 {
143 if (isXRP(issue))
144 return STAmount(
145 issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
147 }
148 else
149 {
150 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
151 static_assert(alwaysFalse, "Unsupported type for toMaxAmount");
152 }
153}
154
155inline STAmount
157 Issue const& issue,
158 Number const& n,
160{
161 return toAmount<STAmount>(issue, n, mode);
162}
163
164template <typename T>
165Issue
166getIssue(T const& amt)
167{
168 if constexpr (std::is_same_v<IOUAmount, T>)
169 return noIssue();
170 else if constexpr (std::is_same_v<XRPAmount, T>)
171 return xrpIssue();
172 else if constexpr (std::is_same_v<STAmount, T>)
173 return amt.issue();
174 else
175 {
176 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
177 static_assert(alwaysFalse, "Unsupported type for getIssue");
178 }
179}
180
181template <typename T>
182constexpr T
183get(STAmount const& a)
184{
185 if constexpr (std::is_same_v<IOUAmount, T>)
186 return a.iou();
187 else if constexpr (std::is_same_v<XRPAmount, T>)
188 return a.xrp();
189 else if constexpr (std::is_same_v<STAmount, T>)
190 return a;
191 else
192 {
193 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
194 static_assert(alwaysFalse, "Unsupported type for get");
195 }
196}
197
198} // namespace xrpl
199
200#endif
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:27
int exponent() const noexcept
Definition IOUAmount.h:153
std::int64_t mantissa() const noexcept
Definition IOUAmount.h:159
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:147
A currency issued by an account.
Definition Issue.h:14
Currency currency
Definition Issue.h:16
AccountID account
Definition Issue.h:17
static rounding_mode getround()
Definition Number.cpp:28
constexpr rep mantissa() const noexcept
Definition Number.h:209
static rounding_mode setround(rounding_mode mode)
Definition Number.cpp:34
constexpr int exponent() const noexcept
Definition Number.h:215
static constexpr std::uint64_t cMaxValue
Definition STAmount.h:52
std::uint64_t mantissa() const noexcept
Definition STAmount.h:469
IOUAmount iou() const
Definition STAmount.cpp:280
bool negative() const noexcept
Definition STAmount.h:463
static int const cMaxOffset
Definition STAmount.h:47
int exponent() const noexcept
Definition STAmount.h:438
static constexpr std::uint64_t cMaxNativeN
Definition STAmount.h:57
XRPAmount xrp() const
Definition STAmount.cpp:264
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:158
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:151
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:96
bool isXRP(AccountID const &c)
Definition AccountID.h:71
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
T toAmount(STAmount const &amt)=delete
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
T toMaxAmount(Issue const &issue)
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition Issue.h:104
Issue getIssue(T const &amt)
STAmount toAmount< STAmount >(STAmount const &amt)