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#include <xrpl/json/json_value.h>
28
29#include <boost/multiprecision/cpp_int.hpp>
30#include <boost/operators.hpp>
31
32#include <cstdint>
33#include <optional>
34#include <string>
35#include <type_traits>
36
37namespace ripple {
38
39class MPTAmount : private boost::totally_ordered<MPTAmount>,
40 private boost::additive<MPTAmount>,
41 private boost::equality_comparable<MPTAmount, std::int64_t>,
42 private boost::additive<MPTAmount, std::int64_t>
43{
44public:
46
47protected:
49
50public:
51 MPTAmount() = default;
52 constexpr MPTAmount(MPTAmount const& other) = default;
53 constexpr MPTAmount&
54 operator=(MPTAmount const& other) = default;
55
56 // Round to nearest, even on tie.
57 explicit MPTAmount(Number const& x) : MPTAmount(static_cast<value_type>(x))
58 {
59 }
60
61 constexpr explicit MPTAmount(value_type value);
62
64
66 operator+=(MPTAmount const& other);
67
69 operator-=(MPTAmount const& other);
70
72 operator-() const;
73
74 bool
75 operator==(MPTAmount const& other) const;
76
77 bool
78 operator==(value_type other) const;
79
80 bool
81 operator<(MPTAmount const& other) const;
82
84 explicit constexpr
85 operator bool() const noexcept;
86
87 operator Number() const noexcept
88 {
89 return value();
90 }
91
93 constexpr int
94 signum() const noexcept;
95
100 constexpr value_type
101 value() const;
102
103 static MPTAmount
105};
106
108{
109}
110
111constexpr MPTAmount&
113{
114 value_ = 0;
115 return *this;
116}
117
119constexpr MPTAmount::operator bool() const noexcept
120{
121 return value_ != 0;
122}
123
125constexpr int
126MPTAmount::signum() const noexcept
127{
128 return (value_ < 0) ? -1 : (value_ ? 1 : 0);
129}
130
135constexpr MPTAmount::value_type
137{
138 return value_;
139}
140
141inline std::string
142to_string(MPTAmount const& amount)
143{
144 return std::to_string(amount.value());
145}
146
147inline MPTAmount
149 MPTAmount const& amt,
150 std::uint32_t num,
151 std::uint32_t den,
152 bool roundUp)
153{
154 using namespace boost::multiprecision;
155
156 if (!den)
157 Throw<std::runtime_error>("division by zero");
158
159 int128_t const amt128(amt.value());
160 auto const neg = amt.value() < 0;
161 auto const m = amt128 * num;
162 auto r = m / den;
163 if (m % den)
164 {
165 if (!neg && roundUp)
166 r += 1;
167 if (neg && !roundUp)
168 r -= 1;
169 }
171 Throw<std::overflow_error>("MPT mulRatio overflow");
172 return MPTAmount(r.convert_to<MPTAmount::value_type>());
173}
174
175} // namespace ripple
176
177#endif // RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED
constexpr value_type value() const
Returns the underlying value.
Definition: MPTAmount.h:136
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:45
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:126
value_type value_
Definition: MPTAmount.h:48
MPTAmount & operator+=(MPTAmount const &other)
Definition: MPTAmount.cpp:25
constexpr MPTAmount(MPTAmount const &other)=default
MPTAmount(Number const &x)
Definition: MPTAmount.h:57
static MPTAmount minPositiveAmount()
Definition: MPTAmount.cpp:63
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition: IOUAmount.cpp:182
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
Zero allows classes to offer efficient comparisons to zero.
Definition: Zero.h:43
T to_string(T... args)