rippled
amount.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2015 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 #include <ripple/basics/safe_cast.h>
21 #include <cassert>
22 #include <cmath>
23 #include <iomanip>
24 #include <test/jtx/Account.h>
25 #include <test/jtx/amount.h>
26 
27 namespace ripple {
28 namespace test {
29 namespace jtx {
30 
31 #if 0
34  AnyAmount const& amount)
35 {
36  if (amount.is_any)
37  {
38  os << amount.value.getText() << "/" <<
39  to_string(amount.value.issue().currency) <<
40  "*";
41  return os;
42  }
43  os << amount.value.getText() << "/" <<
44  to_string(amount.value.issue().currency) <<
45  "(" << amount.name() << ")";
46  return os;
47 }
48 #endif
49 
50 PrettyAmount::operator AnyAmount() const
51 {
52  return {amount_};
53 }
54 
55 template <typename T>
56 static std::string
57 to_places(const T d, std::uint8_t places)
58 {
59  assert(places <= std::numeric_limits<T>::digits10);
60 
62  oss << std::setprecision(places) << std::fixed << d;
63 
64  std::string out = oss.str();
65  out.erase(out.find_last_not_of('0') + 1, std::string::npos);
66  if (out.back() == '.')
67  out.pop_back();
68 
69  return out;
70 }
71 
74 {
75  if (amount.value().native())
76  {
77  // measure in hundredths
78  auto const c = dropsPerXRP.drops() / 100;
79  auto const n = amount.value().mantissa();
80  if (n < c)
81  {
82  if (amount.value().negative())
83  os << "-" << n << " drops";
84  else
85  os << n << " drops";
86  return os;
87  }
88  auto const d = double(n) / dropsPerXRP.drops();
89  if (amount.value().negative())
90  os << "-";
91 
92  os << to_places(d, 6) << " XRP";
93  }
94  else
95  {
96  os << amount.value().getText() << "/"
97  << to_string(amount.value().issue().currency) << "(" << amount.name()
98  << ")";
99  }
100  return os;
101 }
102 
103 //------------------------------------------------------------------------------
104 
105 XRP_t const XRP{};
106 
108 {
109  return {STAmount(issue(), 1, -81), account.name()};
110 }
111 
114 {
115  return {
116  STAmount(issue(), safe_cast<std::uint64_t>(m.n), -81), account.name()};
117 }
118 
120 operator<<(std::ostream& os, IOU const& iou)
121 {
122  os << to_string(iou.issue().currency) << "(" << iou.account.name() << ")";
123  return os;
124 }
125 
126 any_t const any{};
127 
128 } // namespace jtx
129 } // namespace test
130 } // namespace ripple
ripple::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition: Account.h:82
std::setprecision
T setprecision(T... args)
ripple::test::jtx::epsilon_t
Definition: amount.h:269
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
ripple::test::jtx::dropsPerXRP
constexpr XRPAmount dropsPerXRP
Definition: amount.h:67
std::string
STL class.
ripple::test::jtx::any_t
Definition: amount.h:354
ripple::test::jtx::operator<<
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition: amount.cpp:73
ripple::test::jtx::to_places
static std::string to_places(const T d, std::uint8_t places)
Definition: amount.cpp:57
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:347
ripple::test::jtx::detail::epsilon_multiple::n
std::size_t n
Definition: amount.h:263
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::STAmount::getText
std::string getText() const override
Definition: STAmount.cpp:571
ripple::test::jtx::AnyAmount
Amount specifier with an option for any issuer.
Definition: amount.h:361
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::test::jtx::detail::epsilon_multiple
Definition: amount.h:261
ripple::test::jtx::IOU::issue
Issue issue() const
Definition: amount.h:303
ripple::test::jtx::IOU::account
Account account
Definition: amount.h:294
cmath
ripple::test::jtx::XRP_t
Definition: amount.h:168
ripple::test::jtx::IOU::operator()
PrettyAmount operator()(T v) const
Definition: amount.h:323
ripple::QualityDirection::out
@ out
ripple::test::jtx::any
const any_t any
Returns an amount representing "any issuer".
Definition: amount.cpp:126
std::ostream
STL class.
ripple::STAmount
Definition: STAmount.h:45
std::uint8_t
std::ostringstream
STL class.
ripple::STAmount::native
bool native() const noexcept
Definition: STAmount.h:329
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::PrettyAmount::name
std::string const & name() const
Definition: amount.h:118
ripple::test::jtx::IOU
Converts to IOU Issue or STAmount.
Definition: amount.h:291
iomanip
cassert
ripple::STAmount::negative
bool negative() const noexcept
Definition: STAmount.h:335
std::fixed
T fixed(T... args)
std::ostringstream::str
T str(T... args)
ripple::test::jtx::PrettyAmount::value
STAmount const & value() const
Definition: amount.h:124
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
std::numeric_limits
ripple::test::jtx::PrettyAmount
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
Definition: amount.h:73