xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
MPTAmount.h
1
#pragma once
2
3
#include <xrpl/basics/Number.h>
4
#include <xrpl/basics/contract.h>
5
#include <xrpl/beast/utility/Zero.h>
6
7
#include <boost/multiprecision/cpp_int.hpp>
8
#include <boost/operators.hpp>
9
10
#include <
cstdint
>
11
#include <
limits
>
12
#include <
optional
>
13
#include <
ostream
>
14
#include <
stdexcept
>
15
#include <
string
>
16
17
namespace
xrpl
{
18
19
class
MPTAmount
:
private
boost::totally_ordered<MPTAmount>,
20
private
boost::additive<MPTAmount>,
21
private
boost::equality_comparable<MPTAmount, std::int64_t>,
22
private
boost::additive<MPTAmount, std::int64_t>
23
{
24
public
:
25
using
value_type
=
std::int64_t
;
26
27
protected
:
28
value_type
value_
{};
29
30
public
:
31
MPTAmount
() =
default
;
32
constexpr
MPTAmount
(
MPTAmount
const
& other) =
default
;
33
constexpr
MPTAmount
(
beast::Zero
);
34
constexpr
MPTAmount
&
35
operator=
(
MPTAmount
const
& other) =
default
;
36
37
// Round to nearest, even on tie.
38
explicit
MPTAmount
(
Number
const
& x) :
MPTAmount
(static_cast<
value_type
>(x))
39
{
40
}
41
42
constexpr
explicit
MPTAmount
(
value_type
value);
43
44
constexpr
MPTAmount
&
operator=
(
beast::Zero
);
45
46
MPTAmount
&
47
operator+=
(
MPTAmount
const
& other);
48
49
MPTAmount
&
50
operator-=
(
MPTAmount
const
& other);
51
52
MPTAmount
53
operator-
()
const
;
54
55
bool
56
operator==
(
MPTAmount
const
& other)
const
;
57
58
bool
59
operator==
(
value_type
other)
const
;
60
61
bool
62
operator<
(
MPTAmount
const
& other)
const
;
63
67
explicit
constexpr
68
operator
bool() const noexcept;
69
70
operator
Number
() const noexcept
71
{
72
return
value
();
73
}
74
78
[[nodiscard]]
constexpr
int
79
signum
() const noexcept;
80
86
[[nodiscard]] constexpr
value_type
87
value() const;
88
89
static
MPTAmount
90
minPositiveAmount
();
91
};
92
93
constexpr
MPTAmount
::
MPTAmount
(
value_type
value
) :
value_
(
value
)
94
{
95
}
96
97
constexpr
MPTAmount::MPTAmount
(
beast::Zero
)
98
{
99
*
this
=
beast::kZero
;
100
}
101
102
constexpr
MPTAmount
&
103
MPTAmount::operator=
(
beast::Zero
)
104
{
105
value_
= 0;
106
return
*
this
;
107
}
108
112
constexpr
MPTAmount::
113
operator bool() const noexcept
114
{
115
return
value_
!= 0;
116
}
117
121
constexpr
int
122
MPTAmount::signum
() const noexcept
123
{
124
if
(
value_
< 0)
125
return
-1;
126
return
(
value_
!= 0) ? 1 : 0;
127
}
128
134
constexpr
MPTAmount::value_type
135
MPTAmount::value
()
const
136
{
137
return
value_
;
138
}
139
140
// Output MPTAmount as just the value.
141
template
<
class
Char,
class
Traits>
142
std::basic_ostream<Char, Traits>
&
143
operator<<
(
std::basic_ostream<Char, Traits>
& os,
MPTAmount
const
& q)
144
{
145
return
os << q.
value
();
146
}
147
148
inline
std::string
149
to_string
(
MPTAmount
const
& amount)
150
{
151
return
std::to_string
(amount.
value
());
152
}
153
154
inline
MPTAmount
155
mulRatio
(
MPTAmount
const
& amt,
std::uint32_t
num,
std::uint32_t
den,
bool
roundUp)
156
{
157
using namespace
boost::multiprecision;
158
159
if
(den == 0u)
160
Throw<std::runtime_error>
(
"division by zero"
);
161
162
int128_t
const
amt128(amt.
value
());
163
auto
const
neg = amt.
value
() < 0;
164
auto
const
m = amt128 * num;
165
auto
r = m / den;
166
if
(m % den)
167
{
168
if
(!neg && roundUp)
169
r += 1;
170
if
(neg && !roundUp)
171
r -= 1;
172
}
173
if
(r >
std::numeric_limits<MPTAmount::value_type>::max
())
174
Throw<std::overflow_error>
(
"MPT mulRatio overflow"
);
175
return
MPTAmount
(r.convert_to<
MPTAmount::value_type
>());
176
}
177
178
inline
std::optional<MPTAmount>
179
tryMulRatio
(
MPTAmount
const
& amt,
std::uint32_t
num,
std::uint32_t
den,
bool
roundUp)
180
{
181
try
182
{
183
return
mulRatio
(amt, num, den, roundUp);
184
}
185
catch
(
std::overflow_error
const
&)
186
{
187
return
std::nullopt;
188
}
189
}
190
191
}
// namespace xrpl
std::basic_ostream
std::string
xrpl::MPTAmount
Definition
MPTAmount.h:23
xrpl::MPTAmount::value_type
std::int64_t value_type
Definition
MPTAmount.h:25
xrpl::MPTAmount::operator==
bool operator==(MPTAmount const &other) const
Definition
MPTAmount.cpp:26
xrpl::MPTAmount::minPositiveAmount
static MPTAmount minPositiveAmount()
Definition
MPTAmount.cpp:44
xrpl::MPTAmount::operator-=
MPTAmount & operator-=(MPTAmount const &other)
Definition
MPTAmount.cpp:13
xrpl::MPTAmount::operator-
MPTAmount operator-() const
Definition
MPTAmount.cpp:20
xrpl::MPTAmount::signum
constexpr int signum() const noexcept
Return the sign of the amount.
Definition
MPTAmount.h:122
xrpl::MPTAmount::value
constexpr value_type value() const
Returns the underlying value.
Definition
MPTAmount.h:135
xrpl::MPTAmount::MPTAmount
MPTAmount(Number const &x)
Definition
MPTAmount.h:38
xrpl::MPTAmount::operator<
bool operator<(MPTAmount const &other) const
Definition
MPTAmount.cpp:38
xrpl::MPTAmount::operator+=
MPTAmount & operator+=(MPTAmount const &other)
Definition
MPTAmount.cpp:6
xrpl::MPTAmount::MPTAmount
constexpr MPTAmount(MPTAmount const &other)=default
xrpl::MPTAmount::value_
value_type value_
Definition
MPTAmount.h:28
xrpl::MPTAmount::operator=
constexpr MPTAmount & operator=(MPTAmount const &other)=default
xrpl::MPTAmount::MPTAmount
MPTAmount()=default
xrpl::Number
Number is a floating point type that can represent a wide range of values.
Definition
Number.h:351
cstdint
std::int64_t
limits
std::numeric_limits::max
T max(T... args)
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::operator<<
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition
base_uint.h:672
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::tryMulRatio
std::optional< MPTAmount > tryMulRatio(MPTAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition
MPTAmount.h:179
xrpl::mulRatio
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition
libxrpl/protocol/IOUAmount.cpp:93
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
optional
ostream
std::overflow_error
stdexcept
string
beast::Zero
Zero allows classes to offer efficient comparisons to zero.
Definition
Zero.h:26
std::to_string
T to_string(T... args)
Generated by
1.17.0