rippled
Loading...
Searching...
No Matches
AmountConversions.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_AMOUNTCONVERSION_H_INCLUDED
21#define RIPPLE_PROTOCOL_AMOUNTCONVERSION_H_INCLUDED
22
23#include <xrpl/protocol/IOUAmount.h>
24#include <xrpl/protocol/STAmount.h>
25#include <xrpl/protocol/XRPAmount.h>
26
27#include <type_traits>
28
29namespace ripple {
30
31inline STAmount
32toSTAmount(IOUAmount const& iou, Issue const& iss)
33{
34 bool const isNeg = iou.signum() < 0;
35 std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
36 return STAmount(iss, umant, iou.exponent(), isNeg, STAmount::unchecked());
37}
38
39inline STAmount
41{
42 return toSTAmount(iou, noIssue());
43}
44
45inline STAmount
47{
48 bool const isNeg = xrp.signum() < 0;
49 std::uint64_t const umant = isNeg ? -xrp.drops() : xrp.drops();
50 return STAmount(umant, isNeg);
51}
52
53inline STAmount
54toSTAmount(XRPAmount const& xrp, Issue const& iss)
55{
56 XRPL_ASSERT(
57 isXRP(iss.account) && isXRP(iss.currency),
58 "ripple::toSTAmount : is XRP");
59 return toSTAmount(xrp);
60}
61
62template <class T>
63T
64toAmount(STAmount const& amt) = delete;
65
66template <>
69{
70 return amt;
71}
72
73template <>
76{
77 XRPL_ASSERT(
79 "ripple::toAmount<IOUAmount> : maximum mantissa");
80 bool const isNeg = amt.negative();
81 std::int64_t const sMant =
82 isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
83
84 XRPL_ASSERT(!isXRP(amt), "ripple::toAmount<IOUAmount> : is not XRP");
85 return IOUAmount(sMant, amt.exponent());
86}
87
88template <>
91{
92 XRPL_ASSERT(
94 "ripple::toAmount<XRPAmount> : maximum mantissa");
95 bool const isNeg = amt.negative();
96 std::int64_t const sMant =
97 isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
98
99 XRPL_ASSERT(isXRP(amt), "ripple::toAmount<XRPAmount> : is XRP");
100 return XRPAmount(sMant);
101}
102
103template <class T>
104T
105toAmount(IOUAmount const& amt) = delete;
106
107template <>
110{
111 return amt;
112}
113
114template <class T>
115T
116toAmount(XRPAmount const& amt) = delete;
117
118template <>
121{
122 return amt;
123}
124
125template <typename T>
126T
128 Issue const& issue,
129 Number const& n,
131{
133 if (isXRP(issue))
134 Number::setround(mode);
135
136 if constexpr (std::is_same_v<IOUAmount, T>)
137 return IOUAmount(n);
138 else if constexpr (std::is_same_v<XRPAmount, T>)
139 return XRPAmount(static_cast<std::int64_t>(n));
140 else if constexpr (std::is_same_v<STAmount, T>)
141 {
142 if (isXRP(issue))
143 return STAmount(issue, static_cast<std::int64_t>(n));
144 return STAmount(issue, n.mantissa(), n.exponent());
145 }
146 else
147 {
148 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
149 static_assert(alwaysFalse, "Unsupported type for toAmount");
150 }
151}
152
153template <typename T>
154T
155toMaxAmount(Issue const& issue)
156{
157 if constexpr (std::is_same_v<IOUAmount, T>)
159 else if constexpr (std::is_same_v<XRPAmount, T>)
160 return XRPAmount(static_cast<std::int64_t>(STAmount::cMaxNativeN));
161 else if constexpr (std::is_same_v<STAmount, T>)
162 {
163 if (isXRP(issue))
164 return STAmount(
165 issue, static_cast<std::int64_t>(STAmount::cMaxNativeN));
167 }
168 else
169 {
170 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
171 static_assert(alwaysFalse, "Unsupported type for toMaxAmount");
172 }
173}
174
175inline STAmount
177 Issue const& issue,
178 Number const& n,
180{
181 return toAmount<STAmount>(issue, n, mode);
182}
183
184template <typename T>
185Issue
186getIssue(T const& amt)
187{
188 if constexpr (std::is_same_v<IOUAmount, T>)
189 return noIssue();
190 else if constexpr (std::is_same_v<XRPAmount, T>)
191 return xrpIssue();
192 else if constexpr (std::is_same_v<STAmount, T>)
193 return amt.issue();
194 else
195 {
196 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
197 static_assert(alwaysFalse, "Unsupported type for getIssue");
198 }
199}
200
201template <typename T>
202constexpr T
203get(STAmount const& a)
204{
205 if constexpr (std::is_same_v<IOUAmount, T>)
206 return a.iou();
207 else if constexpr (std::is_same_v<XRPAmount, T>)
208 return a.xrp();
209 else if constexpr (std::is_same_v<STAmount, T>)
210 return a;
211 else
212 {
213 constexpr bool alwaysFalse = !std::is_same_v<T, T>;
214 static_assert(alwaysFalse, "Unsupported type for get");
215 }
216}
217
218} // namespace ripple
219
220#endif
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:46
int exponent() const noexcept
Definition IOUAmount.h:172
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:166
std::int64_t mantissa() const noexcept
Definition IOUAmount.h:178
A currency issued by an account.
Definition Issue.h:33
AccountID account
Definition Issue.h:36
Currency currency
Definition Issue.h:35
constexpr int exponent() const noexcept
Definition Number.h:236
static rounding_mode getround()
Definition Number.cpp:47
static rounding_mode setround(rounding_mode mode)
Definition Number.cpp:53
constexpr rep mantissa() const noexcept
Definition Number.h:230
IOUAmount iou() const
Definition STAmount.cpp:299
static std::uint64_t const cMaxNativeN
Definition STAmount.h:74
int exponent() const noexcept
Definition STAmount.h:452
static int const cMaxOffset
Definition STAmount.h:66
XRPAmount xrp() const
Definition STAmount.cpp:283
static std::uint64_t const cMaxValue
Definition STAmount.h:70
bool negative() const noexcept
Definition STAmount.h:471
std::uint64_t mantissa() const noexcept
Definition STAmount.h:477
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:170
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:177
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:115
T toAmount(STAmount const &amt)=delete
bool isXRP(AccountID const &c)
Definition AccountID.h:90
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:123
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)