rippled
Loading...
Searching...
No Matches
AmountSpec.h
1#pragma once
2
3#include <xrpl/protocol/IOUAmount.h>
4#include <xrpl/protocol/STAmount.h>
5#include <xrpl/protocol/XRPAmount.h>
6
7#include <optional>
8
9namespace xrpl {
10
12{
13 explicit AmountSpec() = default;
14
15 bool native;
16 union
17 {
20 };
23
25 operator<<(std::ostream& stream, AmountSpec const& amt)
26 {
27 if (amt.native)
28 stream << to_string(amt.xrp);
29 else
30 stream << to_string(amt.iou);
31 if (amt.currency)
32 stream << "/(" << *amt.currency << ")";
33 if (amt.issuer)
34 stream << "/" << *amt.issuer << "";
35 return stream;
36 }
37};
38
40{
41#ifndef NDEBUG
42 bool native = false;
43#endif
44
45 union
46 {
49 };
50
51 EitherAmount() = default;
52
53 explicit EitherAmount(IOUAmount const& a) : iou(a)
54 {
55 }
56
57#if defined(__GNUC__) && !defined(__clang__)
58#pragma GCC diagnostic push
59 // ignore warning about half of iou amount being uninitialized
60#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
61#endif
62 explicit EitherAmount(XRPAmount const& a) : xrp(a)
63 {
64#ifndef NDEBUG
65 native = true;
66#endif
67 }
68#if defined(__GNUC__) && !defined(__clang__)
69#pragma GCC diagnostic pop
70#endif
71
72 explicit EitherAmount(AmountSpec const& a)
73 {
74#ifndef NDEBUG
75 native = a.native;
76#endif
77 if (a.native)
78 xrp = a.xrp;
79 else
80 iou = a.iou;
81 }
82
83#ifndef NDEBUG
85 operator<<(std::ostream& stream, EitherAmount const& amt)
86 {
87 if (amt.native)
88 stream << to_string(amt.xrp);
89 else
90 stream << to_string(amt.iou);
91 return stream;
92 }
93#endif
94};
95
96template <class T>
97T&
99{
100 static_assert(sizeof(T) == -1, "Must used specialized function");
101 return T(0);
102}
103
104template <>
107{
108 XRPL_ASSERT(!amt.native, "xrpl::get<IOUAmount>(EitherAmount&) : is not XRP");
109 return amt.iou;
110}
111
112template <>
115{
116 XRPL_ASSERT(amt.native, "xrpl::get<XRPAmount>(EitherAmount&) : is XRP");
117 return amt.xrp;
118}
119
120template <class T>
121T const&
122get(EitherAmount const& amt)
123{
124 static_assert(sizeof(T) == -1, "Must used specialized function");
125 return T(0);
126}
127
128template <>
129inline IOUAmount const&
131{
132 XRPL_ASSERT(!amt.native, "xrpl::get<IOUAmount>(EitherAmount const&) : is not XRP");
133 return amt.iou;
134}
135
136template <>
137inline XRPAmount const&
139{
140 XRPL_ASSERT(amt.native, "xrpl::get<XRPAmount>(EitherAmount const&) : is XRP");
141 return amt.xrp;
142}
143
144inline AmountSpec
146{
147 XRPL_ASSERT(
149 "xrpl::toAmountSpec(STAmount const&) : maximum mantissa");
150 bool const isNeg = amt.negative();
151 std::int64_t const sMant = isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();
152 AmountSpec result;
153
154 result.native = isXRP(amt);
155 if (result.native)
156 {
157 result.xrp = XRPAmount(sMant);
158 }
159 else
160 {
161 result.iou = IOUAmount(sMant, amt.exponent());
162 result.issuer = amt.issue().account;
163 result.currency = amt.issue().currency;
164 }
165
166 return result;
167}
168
169inline EitherAmount
171{
172 if (isXRP(amt))
173 return EitherAmount{amt.xrp()};
174 return EitherAmount{amt.iou()};
175}
176
177inline AmountSpec
179{
180 AmountSpec r;
181 r.native = (!c || isXRP(*c));
182 r.currency = c;
183 XRPL_ASSERT(
184 ea.native == r.native,
185 "xrpl::toAmountSpec(EitherAmount const&&, std::optional<Currency>) : "
186 "matching native");
187 if (r.native)
188 {
189 r.xrp = ea.xrp;
190 }
191 else
192 {
193 r.iou = ea.iou;
194 }
195 return r;
196}
197
198} // namespace xrpl
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:25
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
Issue const & issue() const
Definition STAmount.h:454
std::uint64_t mantissa() const noexcept
Definition STAmount.h:435
IOUAmount iou() const
Definition STAmount.cpp:264
bool negative() const noexcept
Definition STAmount.h:429
int exponent() const noexcept
Definition STAmount.h:404
XRPAmount xrp() const
Definition STAmount.cpp:249
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
IOUAmount & get< IOUAmount >(EitherAmount &amt)
Definition AmountSpec.h:106
bool isXRP(AccountID const &c)
Definition AccountID.h:70
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
EitherAmount toEitherAmount(STAmount const &amt)
Definition AmountSpec.h:170
XRPAmount & get< XRPAmount >(EitherAmount &amt)
Definition AmountSpec.h:114
AmountSpec toAmountSpec(STAmount const &amt)
Definition AmountSpec.h:145
IOUAmount iou
Definition AmountSpec.h:19
AmountSpec()=default
XRPAmount xrp
Definition AmountSpec.h:18
std::optional< AccountID > issuer
Definition AmountSpec.h:21
friend std::ostream & operator<<(std::ostream &stream, AmountSpec const &amt)
Definition AmountSpec.h:25
std::optional< Currency > currency
Definition AmountSpec.h:22
EitherAmount(XRPAmount const &a)
Definition AmountSpec.h:62
EitherAmount()=default
friend std::ostream & operator<<(std::ostream &stream, EitherAmount const &amt)
Definition AmountSpec.h:85
EitherAmount(AmountSpec const &a)
Definition AmountSpec.h:72
EitherAmount(IOUAmount const &a)
Definition AmountSpec.h:53