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/basics/safe_cast.h>
6#include <xrpl/beast/utility/Zero.h>
7
8#include <boost/multiprecision/cpp_int.hpp>
9#include <boost/operators.hpp>
10
11#include <cstdint>
12#include <string>
13
14namespace xrpl {
15
16class MPTAmount : private boost::totally_ordered<MPTAmount>,
17 private boost::additive<MPTAmount>,
18 private boost::equality_comparable<MPTAmount, std::int64_t>,
19 private boost::additive<MPTAmount, std::int64_t>
20{
21public:
23
24protected:
26
27public:
28 MPTAmount() = default;
29 constexpr MPTAmount(MPTAmount const& other) = default;
30 constexpr MPTAmount(beast::Zero);
31 constexpr MPTAmount&
32 operator=(MPTAmount const& other) = default;
33
34 // Round to nearest, even on tie.
35 explicit MPTAmount(Number const& x) : MPTAmount(static_cast<value_type>(x))
36 {
37 }
38
39 constexpr explicit MPTAmount(value_type value);
40
42
44 operator+=(MPTAmount const& other);
45
47 operator-=(MPTAmount const& other);
48
50 operator-() const;
51
52 bool
53 operator==(MPTAmount const& other) const;
54
55 bool
56 operator==(value_type other) const;
57
58 bool
59 operator<(MPTAmount const& other) const;
60
62 explicit constexpr
63 operator bool() const noexcept;
64
65 operator Number() const noexcept
66 {
67 return value();
68 }
69
71 [[nodiscard]] constexpr int
72 signum() const noexcept;
73
78 [[nodiscard]] constexpr value_type
79 value() const;
80
81 static MPTAmount
83};
84
88
90{
91 *this = beast::kZero;
92}
93
94constexpr MPTAmount&
96{
97 value_ = 0;
98 return *this;
99}
100
102constexpr MPTAmount::
103operator bool() const noexcept
104{
105 return value_ != 0;
106}
107
109constexpr int
110MPTAmount::signum() const noexcept
111{
112 if (value_ < 0)
113 return -1;
114 return (value_ != 0) ? 1 : 0;
115}
116
121constexpr MPTAmount::value_type
123{
124 return value_;
125}
126
127// Output MPTAmount as just the value.
128template <class Char, class Traits>
131{
132 return os << q.value();
133}
134
135inline std::string
136to_string(MPTAmount const& amount)
137{
138 return std::to_string(amount.value());
139}
140
141inline MPTAmount
142mulRatio(MPTAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp)
143{
144 using namespace boost::multiprecision;
145
146 if (den == 0u)
147 Throw<std::runtime_error>("division by zero");
148
149 int128_t const amt128(amt.value());
150 auto const neg = amt.value() < 0;
151 auto const m = amt128 * num;
152 auto r = m / den;
153 if (m % den)
154 {
155 if (!neg && roundUp)
156 r += 1;
157 if (neg && !roundUp)
158 r -= 1;
159 }
161 Throw<std::overflow_error>("MPT mulRatio overflow");
162 return MPTAmount(r.convert_to<MPTAmount::value_type>());
163}
164
165} // namespace xrpl
std::int64_t value_type
Definition MPTAmount.h:22
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:110
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:122
MPTAmount(Number const &x)
Definition MPTAmount.h:35
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:25
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:306
T max(T... args)
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:648
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition IOUAmount.cpp:93
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:25
T to_string(T... args)