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 ripple {
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),
39 "ripple::toSTAmount : is XRP");
40 return toSTAmount(xrp);
41}
42
43template <class T>
44T
45toAmount(STAmount const& amt) = delete;
46
47template <>
50{
51 return amt;
52}
53
54template <>
57{
58 XRPL_ASSERT(
60 "ripple::toAmount<IOUAmount> : maximum mantissa");
61 bool const isNeg = amt.negative();
62 std::int64_t const sMant =
63 isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
64
65 XRPL_ASSERT(!isXRP(amt), "ripple::toAmount<IOUAmount> : is not XRP");
66 return IOUAmount(sMant, amt.exponent());
67}
68
69template <>
72{
73 XRPL_ASSERT(
75 "ripple::toAmount<XRPAmount> : maximum mantissa");
76 bool const isNeg = amt.negative();
77 std::int64_t const sMant =
78 isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
79
80 XRPL_ASSERT(isXRP(amt), "ripple::toAmount<XRPAmount> : is XRP");
81 return XRPAmount(sMant);
82}
83
84template <class T>
85T
86toAmount(IOUAmount const& amt) = delete;
87
88template <>
91{
92 return amt;
93}
94
95template <class T>
96T
97toAmount(XRPAmount const& amt) = delete;
98
99template <>
102{
103 return amt;
104}
105
106template <typename T>
107T
109 Issue const& issue,
110 Number const& n,
112{
114 if (isXRP(issue))
115 Number::setround(mode);
116
117 if constexpr (std::is_same_v<IOUAmount, T>)
118 return IOUAmount(n);
119 else if constexpr (std::is_same_v<XRPAmount, T>)
120 return XRPAmount(static_cast<std::int64_t>(n));
121 else if constexpr (std::is_same_v<STAmount, T>)
122 {
123 if (isXRP(issue))
124 return STAmount(issue, static_cast<std::int64_t>(n));
125 return STAmount(issue, n.mantissa(), n.exponent());
126 }
127 else
128 {
129 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
130 static_assert(alwaysFalse, "Unsupported type for toAmount");
131 }
132}
133
134template <typename T>
135T
136toMaxAmount(Issue const& issue)
137{
138 if constexpr (std::is_same_v<IOUAmount, T>)
140 else if constexpr (std::is_same_v<XRPAmount, T>)
141 return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
142 else if constexpr (std::is_same_v<STAmount, T>)
143 {
144 if (isXRP(issue))
145 return STAmount(
146 issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
148 }
149 else
150 {
151 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
152 static_assert(alwaysFalse, "Unsupported type for toMaxAmount");
153 }
154}
155
156inline STAmount
158 Issue const& issue,
159 Number const& n,
161{
162 return toAmount<STAmount>(issue, n, mode);
163}
164
165template <typename T>
166Issue
167getIssue(T const& amt)
168{
169 if constexpr (std::is_same_v<IOUAmount, T>)
170 return noIssue();
171 else if constexpr (std::is_same_v<XRPAmount, T>)
172 return xrpIssue();
173 else if constexpr (std::is_same_v<STAmount, T>)
174 return amt.issue();
175 else
176 {
177 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
178 static_assert(alwaysFalse, "Unsupported type for getIssue");
179 }
180}
181
182template <typename T>
183constexpr T
184get(STAmount const& a)
185{
186 if constexpr (std::is_same_v<IOUAmount, T>)
187 return a.iou();
188 else if constexpr (std::is_same_v<XRPAmount, T>)
189 return a.xrp();
190 else if constexpr (std::is_same_v<STAmount, T>)
191 return a;
192 else
193 {
194 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
195 static_assert(alwaysFalse, "Unsupported type for get");
196 }
197}
198
199} // namespace ripple
200
201#endif
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:27
int exponent() const noexcept
Definition IOUAmount.h:153
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:147
std::int64_t mantissa() const noexcept
Definition IOUAmount.h:159
A currency issued by an account.
Definition Issue.h:14
AccountID account
Definition Issue.h:17
Currency currency
Definition Issue.h:16
constexpr int exponent() const noexcept
Definition Number.h:217
static rounding_mode getround()
Definition Number.cpp:28
static rounding_mode setround(rounding_mode mode)
Definition Number.cpp:34
constexpr rep mantissa() const noexcept
Definition Number.h:211
IOUAmount iou() const
Definition STAmount.cpp:280
static std::uint64_t const cMaxNativeN
Definition STAmount.h:55
int exponent() const noexcept
Definition STAmount.h:433
static int const cMaxOffset
Definition STAmount.h:47
XRPAmount xrp() const
Definition STAmount.cpp:264
static std::uint64_t const cMaxValue
Definition STAmount.h:51
bool negative() const noexcept
Definition STAmount.h:452
std::uint64_t mantissa() const noexcept
Definition STAmount.h:458
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:151
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:158
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
T toAmount(STAmount const &amt)=delete
bool isXRP(AccountID const &c)
Definition AccountID.h:71
STAmount toAmount< STAmount >(STAmount const &amt)
Issue getIssue(T const &amt)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition Issue.h:104
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
T toMaxAmount(Issue const &issue)
XRPAmount toAmount< XRPAmount >(STAmount const &amt)