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 } // namespace ripple
124 
125 #endif
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:163
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
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::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::noIssue
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition: Issue.h:103
ripple::toAmount
T toAmount(STAmount const &amt)=delete
ripple::IOUAmount
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:43
ripple::toAmount< IOUAmount >
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Definition: AmountConversions.h:77
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::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
std::numeric_limits
ripple::Issue::account
AccountID account
Definition: Issue.h:38
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::XRPAmount
Definition: XRPAmount.h:46