rippled
Loading...
Searching...
No Matches
AmountConversions.h
1#pragma once
2
3#include <xrpl/protocol/IOUAmount.h>
4#include <xrpl/protocol/STAmount.h>
5#include <xrpl/protocol/XRPAmount.h>
6
7#include <type_traits>
8
9namespace xrpl {
10
11inline STAmount
12toSTAmount(IOUAmount const& iou, Issue const& iss)
13{
14 bool const isNeg = iou.signum() < 0;
15 std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
16 return STAmount(iss, umant, iou.exponent(), isNeg, STAmount::unchecked());
17}
18
19inline STAmount
21{
22 return toSTAmount(iou, noIssue());
23}
24
25inline STAmount
27{
28 bool const isNeg = xrp.signum() < 0;
29 std::uint64_t const umant = isNeg ? -xrp.drops() : xrp.drops();
30 return STAmount(umant, isNeg);
31}
32
33inline STAmount
34toSTAmount(XRPAmount const& xrp, Issue const& iss)
35{
36 XRPL_ASSERT(isXRP(iss.account) && isXRP(iss.currency), "xrpl::toSTAmount : is XRP");
37 return toSTAmount(xrp);
38}
39
40template <class T>
41T
42toAmount(STAmount const& amt) = delete;
43
44template <>
47{
48 return amt;
49}
50
51template <>
54{
55 XRPL_ASSERT(
57 "xrpl::toAmount<IOUAmount> : maximum mantissa");
58 bool const isNeg = amt.negative();
59 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
60
61 XRPL_ASSERT(!isXRP(amt), "xrpl::toAmount<IOUAmount> : is not XRP");
62 return IOUAmount(sMant, amt.exponent());
63}
64
65template <>
68{
69 XRPL_ASSERT(
71 "xrpl::toAmount<XRPAmount> : maximum mantissa");
72 bool const isNeg = amt.negative();
73 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
74
75 XRPL_ASSERT(isXRP(amt), "xrpl::toAmount<XRPAmount> : is XRP");
76 return XRPAmount(sMant);
77}
78
79template <class T>
80T
81toAmount(IOUAmount const& amt) = delete;
82
83template <>
86{
87 return amt;
88}
89
90template <class T>
91T
92toAmount(XRPAmount const& amt) = delete;
93
94template <>
97{
98 return amt;
99}
100
101template <typename T>
102T
104{
106 if (isXRP(issue))
107 Number::setround(mode);
108
109 if constexpr (std::is_same_v<IOUAmount, T>)
110 return IOUAmount(n);
111 else if constexpr (std::is_same_v<XRPAmount, T>)
112 return XRPAmount(static_cast<std::int64_t>(n));
113 else if constexpr (std::is_same_v<STAmount, T>)
114 {
115 if (isXRP(issue))
116 return STAmount(issue, static_cast<std::int64_t>(n));
117 return STAmount(issue, n);
118 }
119 else
120 {
121 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
122 static_assert(alwaysFalse, "Unsupported type for toAmount");
123 }
124}
125
126template <typename T>
127T
128toMaxAmount(Issue const& issue)
129{
130 if constexpr (std::is_same_v<IOUAmount, T>)
132 else if constexpr (std::is_same_v<XRPAmount, T>)
133 return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
134 else if constexpr (std::is_same_v<STAmount, T>)
135 {
136 if (isXRP(issue))
137 return STAmount(issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
139 }
140 else
141 {
142 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
143 static_assert(alwaysFalse, "Unsupported type for toMaxAmount");
144 }
145}
146
147inline STAmount
149{
150 return toAmount<STAmount>(issue, n, mode);
151}
152
153template <typename T>
154Issue
155getIssue(T const& amt)
156{
157 if constexpr (std::is_same_v<IOUAmount, T>)
158 return noIssue();
159 else if constexpr (std::is_same_v<XRPAmount, T>)
160 return xrpIssue();
161 else if constexpr (std::is_same_v<STAmount, T>)
162 return amt.issue();
163 else
164 {
165 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
166 static_assert(alwaysFalse, "Unsupported type for getIssue");
167 }
168}
169
170template <typename T>
171constexpr T
172get(STAmount const& a)
173{
174 if constexpr (std::is_same_v<IOUAmount, T>)
175 return a.iou();
176 else if constexpr (std::is_same_v<XRPAmount, T>)
177 return a.xrp();
178 else if constexpr (std::is_same_v<STAmount, T>)
179 return a;
180 else
181 {
182 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
183 static_assert(alwaysFalse, "Unsupported type for get");
184 }
185}
186
187} // namespace xrpl
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:25
mantissa_type mantissa() const noexcept
Definition IOUAmount.h:164
exponent_type exponent() const noexcept
Definition IOUAmount.h:158
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:152
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
static rounding_mode getround()
Definition Number.cpp:33
static rounding_mode setround(rounding_mode mode)
Definition Number.cpp:39
static constexpr std::uint64_t cMaxValue
Definition STAmount.h:51
std::uint64_t mantissa() const noexcept
Definition STAmount.h:451
IOUAmount iou() const
Definition STAmount.cpp:276
bool negative() const noexcept
Definition STAmount.h:445
static int const cMaxOffset
Definition STAmount.h:46
int exponent() const noexcept
Definition STAmount.h:420
static constexpr std::uint64_t cMaxNativeN
Definition STAmount.h:56
XRPAmount xrp() const
Definition STAmount.cpp:261
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:157
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:150
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:97
bool isXRP(AccountID const &c)
Definition AccountID.h:70
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:105
Issue getIssue(T const &amt)
STAmount toAmount< STAmount >(STAmount const &amt)