rippled
Loading...
Searching...
No Matches
MPTAmount.h
1#ifndef XRPL_PROTOCOL_MPTAMOUNT_H_INCLUDED
2#define XRPL_PROTOCOL_MPTAMOUNT_H_INCLUDED
3
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/beast/utility/Zero.h>
8
9#include <boost/multiprecision/cpp_int.hpp>
10#include <boost/operators.hpp>
11
12#include <cstdint>
13#include <string>
14
15namespace ripple {
16
17class MPTAmount : private boost::totally_ordered<MPTAmount>,
18 private boost::additive<MPTAmount>,
19 private boost::equality_comparable<MPTAmount, std::int64_t>,
20 private boost::additive<MPTAmount, std::int64_t>
21{
22public:
24
25protected:
27
28public:
29 MPTAmount() = default;
30 constexpr MPTAmount(MPTAmount const& other) = default;
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 constexpr int
72 signum() const noexcept;
73
78 constexpr value_type
79 value() const;
80
81 static MPTAmount
83};
84
88
89constexpr MPTAmount&
91{
92 value_ = 0;
93 return *this;
94}
95
97constexpr MPTAmount::operator bool() const noexcept
98{
99 return value_ != 0;
100}
101
103constexpr int
104MPTAmount::signum() const noexcept
105{
106 return (value_ < 0) ? -1 : (value_ ? 1 : 0);
107}
108
113constexpr MPTAmount::value_type
115{
116 return value_;
117}
118
119inline std::string
120to_string(MPTAmount const& amount)
121{
122 return std::to_string(amount.value());
123}
124
125inline MPTAmount
127 MPTAmount const& amt,
128 std::uint32_t num,
129 std::uint32_t den,
130 bool roundUp)
131{
132 using namespace boost::multiprecision;
133
134 if (!den)
135 Throw<std::runtime_error>("division by zero");
136
137 int128_t const amt128(amt.value());
138 auto const neg = amt.value() < 0;
139 auto const m = amt128 * num;
140 auto r = m / den;
141 if (m % den)
142 {
143 if (!neg && roundUp)
144 r += 1;
145 if (neg && !roundUp)
146 r -= 1;
147 }
149 Throw<std::overflow_error>("MPT mulRatio overflow");
150 return MPTAmount(r.convert_to<MPTAmount::value_type>());
151}
152
153} // namespace ripple
154
155#endif // XRPL_BASICS_MPTAMOUNT_H_INCLUDED
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:114
bool operator==(MPTAmount const &other) const
Definition MPTAmount.cpp:26
MPTAmount & operator-=(MPTAmount const &other)
Definition MPTAmount.cpp:13
constexpr MPTAmount & operator=(MPTAmount const &other)=default
std::int64_t value_type
Definition MPTAmount.h:23
MPTAmount operator-() const
Definition MPTAmount.cpp:20
bool operator<(MPTAmount const &other) const
Definition MPTAmount.cpp:38
MPTAmount()=default
constexpr int signum() const noexcept
Return the sign of the amount.
Definition MPTAmount.h:104
value_type value_
Definition MPTAmount.h:26
MPTAmount & operator+=(MPTAmount const &other)
Definition MPTAmount.cpp:6
constexpr MPTAmount(MPTAmount const &other)=default
MPTAmount(Number const &x)
Definition MPTAmount.h:35
static MPTAmount minPositiveAmount()
Definition MPTAmount.cpp:44
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:26
T to_string(T... args)