rippled
Loading...
Searching...
No Matches
MPTAmount.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_MPTAMOUNT_H_INCLUDED
21#define RIPPLE_PROTOCOL_MPTAMOUNT_H_INCLUDED
22
23#include <xrpl/basics/Number.h>
24#include <xrpl/basics/contract.h>
25#include <xrpl/basics/safe_cast.h>
26#include <xrpl/beast/utility/Zero.h>
27
28#include <boost/multiprecision/cpp_int.hpp>
29#include <boost/operators.hpp>
30
31#include <cstdint>
32#include <string>
33
34namespace ripple {
35
36class MPTAmount : private boost::totally_ordered<MPTAmount>,
37 private boost::additive<MPTAmount>,
38 private boost::equality_comparable<MPTAmount, std::int64_t>,
39 private boost::additive<MPTAmount, std::int64_t>
40{
41public:
43
44protected:
46
47public:
48 MPTAmount() = default;
49 constexpr MPTAmount(MPTAmount const& other) = default;
50 constexpr MPTAmount&
51 operator=(MPTAmount const& other) = default;
52
53 // Round to nearest, even on tie.
54 explicit MPTAmount(Number const& x) : MPTAmount(static_cast<value_type>(x))
55 {
56 }
57
58 constexpr explicit MPTAmount(value_type value);
59
61
63 operator+=(MPTAmount const& other);
64
66 operator-=(MPTAmount const& other);
67
69 operator-() const;
70
71 bool
72 operator==(MPTAmount const& other) const;
73
74 bool
75 operator==(value_type other) const;
76
77 bool
78 operator<(MPTAmount const& other) const;
79
81 explicit constexpr
82 operator bool() const noexcept;
83
84 operator Number() const noexcept
85 {
86 return value();
87 }
88
90 constexpr int
91 signum() const noexcept;
92
97 constexpr value_type
98 value() const;
99
100 static MPTAmount
102};
103
107
108constexpr MPTAmount&
110{
111 value_ = 0;
112 return *this;
113}
114
116constexpr MPTAmount::operator bool() const noexcept
117{
118 return value_ != 0;
119}
120
122constexpr int
123MPTAmount::signum() const noexcept
124{
125 return (value_ < 0) ? -1 : (value_ ? 1 : 0);
126}
127
132constexpr MPTAmount::value_type
134{
135 return value_;
136}
137
138inline std::string
139to_string(MPTAmount const& amount)
140{
141 return std::to_string(amount.value());
142}
143
144inline MPTAmount
146 MPTAmount const& amt,
147 std::uint32_t num,
148 std::uint32_t den,
149 bool roundUp)
150{
151 using namespace boost::multiprecision;
152
153 if (!den)
154 Throw<std::runtime_error>("division by zero");
155
156 int128_t const amt128(amt.value());
157 auto const neg = amt.value() < 0;
158 auto const m = amt128 * num;
159 auto r = m / den;
160 if (m % den)
161 {
162 if (!neg && roundUp)
163 r += 1;
164 if (neg && !roundUp)
165 r -= 1;
166 }
168 Throw<std::overflow_error>("MPT mulRatio overflow");
169 return MPTAmount(r.convert_to<MPTAmount::value_type>());
170}
171
172} // namespace ripple
173
174#endif // RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:133
bool operator==(MPTAmount const &other) const
Definition MPTAmount.cpp:45
MPTAmount & operator-=(MPTAmount const &other)
Definition MPTAmount.cpp:32
constexpr MPTAmount & operator=(MPTAmount const &other)=default
std::int64_t value_type
Definition MPTAmount.h:42
MPTAmount operator-() const
Definition MPTAmount.cpp:39
bool operator<(MPTAmount const &other) const
Definition MPTAmount.cpp:57
MPTAmount()=default
constexpr int signum() const noexcept
Return the sign of the amount.
Definition MPTAmount.h:123
value_type value_
Definition MPTAmount.h:45
MPTAmount & operator+=(MPTAmount const &other)
Definition MPTAmount.cpp:25
constexpr MPTAmount(MPTAmount const &other)=default
MPTAmount(Number const &x)
Definition MPTAmount.h:54
static MPTAmount minPositiveAmount()
Definition MPTAmount.cpp:63
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:630
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:43
T to_string(T... args)