rippled
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 <ripple/basics/IOUAmount.h>
24 #include <ripple/basics/XRPAmount.h>
25 #include <ripple/protocol/STAmount.h>
26 
27 namespace ripple {
28 
29 inline STAmount
30 toSTAmount(IOUAmount const& iou, Issue const& iss)
31 {
32  bool const isNeg = iou.signum() < 0;
33  std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa();
34  return STAmount(
35  iss,
36  umant,
37  iou.exponent(),
38  /*native*/ false,
39  isNeg,
41 }
42 
43 inline STAmount
44 toSTAmount(IOUAmount const& iou)
45 {
46  return toSTAmount(iou, noIssue());
47 }
48 
49 inline STAmount
50 toSTAmount(XRPAmount const& xrp)
51 {
52  bool const isNeg = xrp.signum() < 0;
53  std::uint64_t const umant = isNeg ? -xrp.drops() : xrp.drops();
54  return STAmount(umant, isNeg);
55 }
56 
57 inline STAmount
58 toSTAmount(XRPAmount const& xrp, Issue const& iss)
59 {
60  assert(isXRP(iss.account) && isXRP(iss.currency));
61  return toSTAmount(xrp);
62 }
63 
64 template <class T>
65 T
66 toAmount(STAmount const& amt) = delete;
67 
68 template <>
69 inline STAmount
71 {
72  return amt;
73 }
74 
75 template <>
76 inline IOUAmount
78 {
80  bool const isNeg = amt.negative();
81  std::int64_t const sMant =
82  isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
83 
84  assert(!isXRP(amt));
85  return IOUAmount(sMant, amt.exponent());
86 }
87 
88 template <>
89 inline XRPAmount
91 {
93  bool const isNeg = amt.negative();
94  std::int64_t const sMant =
95  isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
96 
97  assert(isXRP(amt));
98  return XRPAmount(sMant);
99 }
100 
101 template <class T>
102 T
103 toAmount(IOUAmount const& amt) = delete;
104 
105 template <>
106 inline IOUAmount
108 {
109  return amt;
110 }
111 
112 template <class T>
113 T
114 toAmount(XRPAmount const& amt) = delete;
115 
116 template <>
117 inline XRPAmount
119 {
120  return amt;
121 }
122 
123 template <typename T>
124 T
126  Issue const& issue,
127  Number const& n,
129 {
131  if (isXRP(issue))
132  Number::setround(mode);
133  if constexpr (std::is_same_v<IOUAmount, T>)
134  return IOUAmount(n);
135  if constexpr (std::is_same_v<XRPAmount, T>)
136  return XRPAmount(static_cast<std::int64_t>(n));
137  if constexpr (std::is_same_v<STAmount, T>)
138  {
139  if (isXRP(issue))
140  return STAmount(issue, static_cast<std::int64_t>(n));
141  return STAmount(issue, n.mantissa(), n.exponent());
142  }
143 }
144 
145 inline STAmount
147  Issue const& issue,
148  Number const& n,
150 {
151  return toAmount<STAmount>(issue, n, mode);
152 }
153 
154 template <typename T>
155 Issue
156 getIssue(T const& amt)
157 {
158  if constexpr (std::is_same_v<IOUAmount, T>)
159  return noIssue();
160  if constexpr (std::is_same_v<XRPAmount, T>)
161  return xrpIssue();
162  if constexpr (std::is_same_v<STAmount, T>)
163  return amt.issue();
164 }
165 
166 template <typename T>
167 constexpr T
168 get(STAmount const& a)
169 {
170  if constexpr (std::is_same_v<IOUAmount, T>)
171  return a.iou();
172  if constexpr (std::is_same_v<XRPAmount, T>)
173  return a.xrp();
174  if constexpr (std::is_same_v<STAmount, T>)
175  return a;
176 }
177 
178 } // namespace ripple
179 
180 #endif
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:163
ripple::Number::getround
static rounding_mode getround()
Definition: Number.cpp:41
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
ripple::STAmount::mantissa
std::uint64_t mantissa() const noexcept
Definition: STAmount.h:341
ripple::XRPAmount::drops
constexpr value_type drops() const
Returns the number of drops.
Definition: XRPAmount.h:172
ripple::saveNumberRoundMode
Definition: Number.h:365
ripple::Issue::currency
Currency currency
Definition: Issue.h:38
ripple::Number::exponent
constexpr int exponent() const noexcept
Definition: Number.h:213
ripple::noIssue
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition: Issue.h:113
ripple::toAmount
T toAmount(STAmount const &amt)=delete
ripple::IOUAmount
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:43
ripple::STAmount::iou
IOUAmount iou() const
Definition: STAmount.cpp:349
ripple::Number::rounding_mode
rounding_mode
Definition: Number.h:161
ripple::toAmount< IOUAmount >
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Definition: AmountConversions.h:77
ripple::STAmount::xrp
XRPAmount xrp() const
Definition: STAmount.cpp:334
ripple::IOUAmount::signum
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:157
ripple::STAmount::exponent
int exponent() const noexcept
Definition: STAmount.h:323
ripple::toAmount< XRPAmount >
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
Definition: AmountConversions.h:90
ripple::getIssue
Issue getIssue(T const &amt)
Definition: AmountConversions.h:156
ripple::Number
Definition: Number.h:36
ripple::Number::setround
static rounding_mode setround(rounding_mode mode)
Definition: Number.cpp:47
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:30
ripple::STAmount
Definition: STAmount.h:45
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:89
std::uint64_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::IOUAmount::mantissa
std::int64_t mantissa() const noexcept
Definition: IOUAmount.h:169
ripple::STAmount::unchecked
Definition: STAmount.h:80
ripple::STAmount::negative
bool negative() const noexcept
Definition: STAmount.h:335
ripple::xrpIssue
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:105
std::numeric_limits
ripple::Number::mantissa
constexpr rep mantissa() const noexcept
Definition: Number.h:207
ripple::Issue::account
AccountID account
Definition: Issue.h:39
ripple::toAmount< STAmount >
STAmount toAmount< STAmount >(STAmount const &amt)
Definition: AmountConversions.h:70
ripple::XRPAmount::signum
constexpr int signum() const noexcept
Return the sign of the amount.
Definition: XRPAmount.h:165
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:118
ripple::XRPAmount
Definition: XRPAmount.h:46