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 <ostream>
13#include <stdexcept>
14#include <string>
15
16namespace xrpl {
17
18class MPTAmount : private boost::totally_ordered<MPTAmount>,
19 private boost::additive<MPTAmount>,
20 private boost::equality_comparable<MPTAmount, std::int64_t>,
21 private boost::additive<MPTAmount, std::int64_t>
22{
23public:
25
26protected:
28
29public:
30 MPTAmount() = default;
31 constexpr MPTAmount(MPTAmount const& other) = default;
32 constexpr MPTAmount(beast::Zero);
33 constexpr MPTAmount&
34 operator=(MPTAmount const& other) = default;
35
36 // Round to nearest, even on tie.
37 explicit MPTAmount(Number const& x) : MPTAmount(static_cast<value_type>(x))
38 {
39 }
40
41 constexpr explicit MPTAmount(value_type value);
42
44
46 operator+=(MPTAmount const& other);
47
49 operator-=(MPTAmount const& other);
50
52 operator-() const;
53
54 bool
55 operator==(MPTAmount const& other) const;
56
57 bool
58 operator==(value_type other) const;
59
60 bool
61 operator<(MPTAmount const& other) const;
62
66 explicit constexpr
67 operator bool() const noexcept;
68
69 operator Number() const noexcept
70 {
71 return value();
72 }
73
77 [[nodiscard]] constexpr int
78 signum() const noexcept;
79
85 [[nodiscard]] constexpr value_type
86 value() const;
87
88 static MPTAmount
90};
91
95
97{
98 *this = beast::kZero;
99}
100
101constexpr MPTAmount&
103{
104 value_ = 0;
105 return *this;
106}
107
111constexpr MPTAmount::
112operator bool() const noexcept
113{
114 return value_ != 0;
115}
116
120constexpr int
121MPTAmount::signum() const noexcept
122{
123 if (value_ < 0)
124 return -1;
125 return (value_ != 0) ? 1 : 0;
126}
127
133constexpr MPTAmount::value_type
135{
136 return value_;
137}
138
139// Output MPTAmount as just the value.
140template <class Char, class Traits>
143{
144 return os << q.value();
145}
146
147inline std::string
148to_string(MPTAmount const& amount)
149{
150 return std::to_string(amount.value());
151}
152
153inline MPTAmount
154mulRatio(MPTAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp)
155{
156 using namespace boost::multiprecision;
157
158 if (den == 0u)
159 Throw<std::runtime_error>("division by zero");
160
161 int128_t const amt128(amt.value());
162 auto const neg = amt.value() < 0;
163 auto const m = amt128 * num;
164 auto r = m / den;
165 if (m % den)
166 {
167 if (!neg && roundUp)
168 r += 1;
169 if (neg && !roundUp)
170 r -= 1;
171 }
173 Throw<std::overflow_error>("MPT mulRatio overflow");
174 return MPTAmount(r.convert_to<MPTAmount::value_type>());
175}
176
177} // namespace xrpl
std::int64_t value_type
Definition MPTAmount.h:24
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:121
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:134
MPTAmount(Number const &x)
Definition MPTAmount.h:37
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:27
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:666
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
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)