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
30 STAmount
31 toSTAmount (IOUAmount const& iou, Issue const& iss)
32 {
33  bool const isNeg = iou.signum() < 0;
34  std::uint64_t const umant = isNeg ? - iou.mantissa () : iou.mantissa ();
35  return STAmount (iss, umant, iou.exponent (), /*native*/ false, isNeg,
37 }
38 
39 inline
40 STAmount
41 toSTAmount (IOUAmount const& iou)
42 {
43  return toSTAmount (iou, noIssue ());
44 }
45 
46 inline
47 STAmount
48 toSTAmount (XRPAmount const& xrp)
49 {
50  bool const isNeg = xrp.signum() < 0;
51  std::uint64_t const umant = isNeg ? - xrp.drops () : xrp.drops ();
52  return STAmount (umant, isNeg);
53 }
54 
55 inline
56 STAmount
57 toSTAmount (XRPAmount const& xrp, Issue const& iss)
58 {
59  assert (isXRP(iss.account) && isXRP(iss.currency));
60  return toSTAmount (xrp);
61 }
62 
63 template <class T>
64 T
65 toAmount (STAmount const& amt)
66 {
67  static_assert(sizeof(T) == -1, "Must use specialized function");
68  return T(0);
69 }
70 
71 template <>
72 inline
73 STAmount
75 {
76  return amt;
77 }
78 
79 template <>
80 inline
81 IOUAmount
83 {
85  bool const isNeg = amt.negative ();
86  std::int64_t const sMant =
87  isNeg ? - std::int64_t (amt.mantissa ()) : amt.mantissa ();
88 
89  assert (! isXRP (amt));
90  return IOUAmount (sMant, amt.exponent ());
91 }
92 
93 template <>
94 inline
95 XRPAmount
97 {
99  bool const isNeg = amt.negative ();
100  std::int64_t const sMant =
101  isNeg ? - std::int64_t (amt.mantissa ()) : amt.mantissa ();
102 
103  assert (isXRP (amt));
104  return XRPAmount (sMant);
105 }
106 
107 
108 }
109 
110 #endif
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:126
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::STAmount::mantissa
std::uint64_t mantissa() const noexcept
Definition: STAmount.h:155
ripple::XRPAmount::drops
constexpr value_type drops() const
Returns the number of drops.
Definition: XRPAmount.h:185
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:104
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:82
ripple::IOUAmount::signum
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:120
ripple::STAmount::exponent
int exponent() const noexcept
Definition: STAmount.h:152
ripple::toAmount< XRPAmount >
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
Definition: AmountConversions.h:96
ripple::toSTAmount
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
Definition: AmountConversions.h:31
ripple::STAmount
Definition: STAmount.h:42
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:121
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:132
ripple::STAmount::unchecked
Definition: STAmount.h:78
ripple::STAmount::negative
bool negative() const noexcept
Definition: STAmount.h:154
ripple::toAmount
T toAmount(STAmount const &amt)
Definition: AmountConversions.h:65
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:177
ripple::XRPAmount
Definition: XRPAmount.h:46