xrpld
Loading...
Searching...
No Matches
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
17namespace xrpl {
18
19class 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{
24public:
26
27protected:
29
30public:
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
45
47 operator+=(MPTAmount const& other);
48
50 operator-=(MPTAmount const& other);
51
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
91};
92
96
98{
99 *this = beast::kZero;
100}
101
102constexpr MPTAmount&
104{
105 value_ = 0;
106 return *this;
107}
108
112constexpr MPTAmount::
113operator bool() const noexcept
114{
115 return value_ != 0;
116}
117
121constexpr int
122MPTAmount::signum() const noexcept
123{
124 if (value_ < 0)
125 return -1;
126 return (value_ != 0) ? 1 : 0;
127}
128
134constexpr MPTAmount::value_type
136{
137 return value_;
138}
139
140// Output MPTAmount as just the value.
141template <class Char, class Traits>
144{
145 return os << q.value();
146}
147
148inline std::string
149to_string(MPTAmount const& amount)
150{
151 return std::to_string(amount.value());
152}
153
154inline MPTAmount
155mulRatio(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 }
174 Throw<std::overflow_error>("MPT mulRatio overflow");
175 return MPTAmount(r.convert_to<MPTAmount::value_type>());
176}
177
179tryMulRatio(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::int64_t value_type
Definition MPTAmount.h:25
bool operator==(MPTAmount const &other) const
Definition MPTAmount.cpp:26
static MPTAmount minPositiveAmount()
Definition MPTAmount.cpp:44
MPTAmount & operator-=(MPTAmount const &other)
Definition MPTAmount.cpp:13
MPTAmount operator-() const
Definition MPTAmount.cpp:20
constexpr int signum() const noexcept
Return the sign of the amount.
Definition MPTAmount.h:122
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:135
MPTAmount(Number const &x)
Definition MPTAmount.h:38
bool operator<(MPTAmount const &other) const
Definition MPTAmount.cpp:38
MPTAmount & operator+=(MPTAmount const &other)
Definition MPTAmount.cpp:6
constexpr MPTAmount(MPTAmount const &other)=default
value_type value_
Definition MPTAmount.h:28
constexpr MPTAmount & operator=(MPTAmount const &other)=default
MPTAmount()=default
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
T max(T... args)
constexpr Zero kZero
Definition Zero.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:672
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:657
std::optional< MPTAmount > tryMulRatio(MPTAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition MPTAmount.h:179
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:26
T to_string(T... args)