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)
67 {
68  static_assert(sizeof(T) == -1, "Must use specialized function");
69  return T(0);
70 }
71 
72 template <>
73 inline STAmount
75 {
76  return amt;
77 }
78 
79 template <>
80 inline IOUAmount
82 {
84  bool const isNeg = amt.negative();
85  std::int64_t const sMant =
86  isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
87 
88  assert(!isXRP(amt));
89  return IOUAmount(sMant, amt.exponent());
90 }
91 
92 template <>
93 inline XRPAmount
95 {
97  bool const isNeg = amt.negative();
98  std::int64_t const sMant =
99  isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
100 
101  assert(isXRP(amt));
102  return XRPAmount(sMant);
103 }
104 
105 } // namespace ripple
106 
107 #endif
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:122
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::STAmount::mantissa
std::uint64_t mantissa() const noexcept
Definition: STAmount.h:192
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:105
ripple::IOUAmount
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:41
ripple::toAmount< IOUAmount >
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Definition: AmountConversions.h:81
ripple::IOUAmount::signum
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:116
ripple::STAmount::exponent
int exponent() const noexcept
Definition: STAmount.h:177
ripple::toAmount< XRPAmount >
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
Definition: AmountConversions.h:94
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:30
ripple::STAmount
Definition: STAmount.h:42
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:128
ripple::STAmount::unchecked
Definition: STAmount.h:77
ripple::STAmount::negative
bool negative() const noexcept
Definition: STAmount.h:187
ripple::toAmount
T toAmount(STAmount const &amt)
Definition: AmountConversions.h:66
std::numeric_limits
ripple::Issue::account
AccountID account
Definition: Issue.h:38
ripple::toAmount< STAmount >
STAmount toAmount< STAmount >(STAmount const &amt)
Definition: AmountConversions.h:74
ripple::XRPAmount::signum
constexpr int signum() const noexcept
Return the sign of the amount.
Definition: XRPAmount.h:165
ripple::XRPAmount
Definition: XRPAmount.h:46