rippled
Loading...
Searching...
No Matches
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 <test/jtx/Account.h>
21#include <test/jtx/amount.h>
22
23#include <xrpl/basics/safe_cast.h>
24
25#include <iomanip>
26
27namespace ripple {
28namespace test {
29namespace 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
50PrettyAmount::operator AnyAmount() const
51{
52 return {amount_};
53}
54
55template <typename T>
56static std::string
57to_places(T const 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
73operator<<(std::ostream& os, PrettyAmount const& amount)
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 if (amount.value().holds<Issue>())
95 {
96 os << amount.value().getText() << "/"
97 << to_string(amount.value().issue().currency) << "(" << amount.name()
98 << ")";
99 }
100 else
101 {
102 auto const& mptIssue = amount.value().asset().get<MPTIssue>();
103 os << amount.value().getText() << "/" << to_string(mptIssue) << "("
104 << amount.name() << ")";
105 }
106 return os;
107}
108
109//------------------------------------------------------------------------------
110
111XRP_t const XRP{};
112
115{
116 return {STAmount(issue(), 1, -81), account.name()};
117}
118
121{
122 return {
123 STAmount(issue(), safe_cast<std::uint64_t>(m.n), -81), account.name()};
124}
125
127operator<<(std::ostream& os, IOU const& iou)
128{
129 os << to_string(iou.issue().currency) << "(" << iou.account.name() << ")";
130 return os;
131}
132
133any_t const any{};
134
135} // namespace jtx
136} // namespace test
137} // namespace ripple
constexpr TIss const & get() const
A currency issued by an account.
Definition Issue.h:33
Currency currency
Definition Issue.h:35
constexpr bool holds() const noexcept
Definition STAmount.h:465
Asset const & asset() const
Definition STAmount.h:483
std::string getText() const override
Definition STAmount.cpp:706
bool negative() const noexcept
Definition STAmount.h:471
Issue const & issue() const
Definition STAmount.h:496
std::uint64_t mantissa() const noexcept
Definition STAmount.h:477
bool native() const noexcept
Definition STAmount.h:458
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:177
std::string const & name() const
Return the name.
Definition Account.h:87
Converts to IOU Issue or STAmount.
PrettyAmount operator()(T v) const
T fixed(T... args)
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition amount.cpp:73
constexpr XRPAmount dropsPerXRP
any_t const any
Returns an amount representing "any issuer".
Definition amount.cpp:133
static std::string to_places(T const d, std::uint8_t places)
Definition amount.cpp:57
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:111
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
T setprecision(T... args)
T str(T... args)
Amount specifier with an option for any issuer.
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
std::string const & name() const