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(
56 amt.mantissa() < std::numeric_limits<std::int64_t>::max(), "xrpl::toAmount<IOUAmount> : maximum mantissa");
57 bool const isNeg = amt.negative();
58 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
59
60 XRPL_ASSERT(!isXRP(amt), "xrpl::toAmount<IOUAmount> : is not XRP");
61 return IOUAmount(sMant, amt.exponent());
62}
63
64template <>
67{
68 XRPL_ASSERT(
69 amt.mantissa() < std::numeric_limits<std::int64_t>::max(), "xrpl::toAmount<XRPAmount> : maximum mantissa");
70 bool const isNeg = amt.negative();
71 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
72
73 XRPL_ASSERT(isXRP(amt), "xrpl::toAmount<XRPAmount> : is XRP");
74 return XRPAmount(sMant);
75}
76
77template <class T>
78T
79toAmount(IOUAmount const& amt) = delete;
80
81template <>
84{
85 return amt;
86}
87
88template <class T>
89T
90toAmount(XRPAmount const& amt) = delete;
91
92template <>
95{
96 return amt;
97}
98
99template <typename T>
100T
102{
104 if (isXRP(issue))
105 Number::setround(mode);
106
107 if constexpr (std::is_same_v<IOUAmount, T>)
108 return IOUAmount(n);
109 else if constexpr (std::is_same_v<XRPAmount, T>)
110 return XRPAmount(static_cast<std::int64_t>(n));
111 else if constexpr (std::is_same_v<STAmount, T>)
112 {
113 if (isXRP(issue))
114 return STAmount(issue, static_cast<std::int64_t>(n));
115 return STAmount(issue, n);
116 }
117 else
118 {
119 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
120 static_assert(alwaysFalse, "Unsupported type for toAmount");
121 }
122}
123
124template <typename T>
125T
126toMaxAmount(Issue const& issue)
127{
128 if constexpr (std::is_same_v<IOUAmount, T>)
130 else if constexpr (std::is_same_v<XRPAmount, T>)
131 return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
132 else if constexpr (std::is_same_v<STAmount, T>)
133 {
134 if (isXRP(issue))
135 return STAmount(issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
137 }
138 else
139 {
140 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
141 static_assert(alwaysFalse, "Unsupported type for toMaxAmount");
142 }
143}
144
145inline STAmount
147{
148 return toAmount<STAmount>(issue, n, mode);
149}
150
151template <typename T>
152Issue
153getIssue(T const& amt)
154{
155 if constexpr (std::is_same_v<IOUAmount, T>)
156 return noIssue();
157 else if constexpr (std::is_same_v<XRPAmount, T>)
158 return xrpIssue();
159 else if constexpr (std::is_same_v<STAmount, T>)
160 return amt.issue();
161 else
162 {
163 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
164 static_assert(alwaysFalse, "Unsupported type for getIssue");
165 }
166}
167
168template <typename T>
169constexpr T
170get(STAmount const& a)
171{
172 if constexpr (std::is_same_v<IOUAmount, T>)
173 return a.iou();
174 else if constexpr (std::is_same_v<XRPAmount, T>)
175 return a.xrp();
176 else if constexpr (std::is_same_v<STAmount, T>)
177 return a;
178 else
179 {
180 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
181 static_assert(alwaysFalse, "Unsupported type for get");
182 }
183}
184
185} // namespace xrpl
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:25
mantissa_type mantissa() const noexcept
Definition IOUAmount.h:161
exponent_type exponent() const noexcept
Definition IOUAmount.h:155
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:149
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:435
IOUAmount iou() const
Definition STAmount.cpp:264
bool negative() const noexcept
Definition STAmount.h:429
static int const cMaxOffset
Definition STAmount.h:46
int exponent() const noexcept
Definition STAmount.h:404
static constexpr std::uint64_t cMaxNativeN
Definition STAmount.h:56
XRPAmount xrp() const
Definition STAmount.cpp:249
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)