rippled
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&
31 operator=(MPTAmount const& other) = default;
32
33 // Round to nearest, even on tie.
34 explicit MPTAmount(Number const& x) : MPTAmount(static_cast<value_type>(x))
35 {
36 }
37
38 constexpr explicit MPTAmount(value_type value);
39
41
43 operator+=(MPTAmount const& other);
44
46 operator-=(MPTAmount const& other);
47
49 operator-() const;
50
51 bool
52 operator==(MPTAmount const& other) const;
53
54 bool
55 operator==(value_type other) const;
56
57 bool
58 operator<(MPTAmount const& other) const;
59
61 explicit constexpr
62 operator bool() const noexcept;
63
64 operator Number() const noexcept
65 {
66 return value();
67 }
68
70 constexpr int
71 signum() const noexcept;
72
77 constexpr value_type
78 value() const;
79
80 static MPTAmount
82};
83
87
88constexpr MPTAmount&
90{
91 value_ = 0;
92 return *this;
93}
94
96constexpr MPTAmount::
97operator 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
126mulRatio(MPTAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp)
127{
128 using namespace boost::multiprecision;
129
130 if (!den)
131 Throw<std::runtime_error>("division by zero");
132
133 int128_t const amt128(amt.value());
134 auto const neg = amt.value() < 0;
135 auto const m = amt128 * num;
136 auto r = m / den;
137 if (m % den)
138 {
139 if (!neg && roundUp)
140 r += 1;
141 if (neg && !roundUp)
142 r -= 1;
143 }
145 Throw<std::overflow_error>("MPT mulRatio overflow");
146 return MPTAmount(r.convert_to<MPTAmount::value_type>());
147}
148
149} // 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:104
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:114
MPTAmount(Number const &x)
Definition MPTAmount.h:34
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:207
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:25
T to_string(T... args)