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::operator bool() const noexcept
97{
98 return value_ != 0;
99}
100
102constexpr int
103MPTAmount::signum() const noexcept
104{
105 return (value_ < 0) ? -1 : (value_ ? 1 : 0);
106}
107
112constexpr MPTAmount::value_type
114{
115 return value_;
116}
117
118inline std::string
119to_string(MPTAmount const& amount)
120{
121 return std::to_string(amount.value());
122}
123
124inline MPTAmount
125mulRatio(MPTAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp)
126{
127 using namespace boost::multiprecision;
128
129 if (!den)
130 Throw<std::runtime_error>("division by zero");
131
132 int128_t const amt128(amt.value());
133 auto const neg = amt.value() < 0;
134 auto const m = amt128 * num;
135 auto r = m / den;
136 if (m % den)
137 {
138 if (!neg && roundUp)
139 r += 1;
140 if (neg && !roundUp)
141 r -= 1;
142 }
144 Throw<std::overflow_error>("MPT mulRatio overflow");
145 return MPTAmount(r.convert_to<MPTAmount::value_type>());
146}
147
148} // 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:103
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:113
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:597
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)