rippled
Loading...
Searching...
No Matches
IOUAmount.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 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_BASICS_IOUAMOUNT_H_INCLUDED
21#define RIPPLE_BASICS_IOUAMOUNT_H_INCLUDED
22
23#include <xrpl/basics/LocalValue.h>
24#include <xrpl/basics/Number.h>
25#include <xrpl/beast/utility/Zero.h>
26
27#include <boost/operators.hpp>
28
29#include <cstdint>
30#include <string>
31#include <utility>
32
33namespace ripple {
34
45class IOUAmount : private boost::totally_ordered<IOUAmount>,
46 private boost::additive<IOUAmount>
47{
48private:
51
58 void
59 normalize();
60
61public:
62 IOUAmount() = default;
63 explicit IOUAmount(Number const& other);
66
68
69 operator Number() const;
70
72 operator+=(IOUAmount const& other);
73
75 operator-=(IOUAmount const& other);
76
78 operator-() const;
79
80 bool
81 operator==(IOUAmount const& other) const;
82
83 bool
84 operator<(IOUAmount const& other) const;
85
87 explicit
88 operator bool() const noexcept;
89
91 int
92 signum() const noexcept;
93
94 int
95 exponent() const noexcept;
96
97 std::int64_t
98 mantissa() const noexcept;
99
100 static IOUAmount
102};
103
105{
106 *this = beast::zero;
107}
108
111{
112 normalize();
113}
114
115inline IOUAmount&
117{
118 // The -100 is used to allow 0 to sort less than small positive values
119 // which will have a large negative exponent.
120 mantissa_ = 0;
121 exponent_ = -100;
122 return *this;
123}
124
125inline IOUAmount::operator Number() const
126{
127 return Number{mantissa_, exponent_};
128}
129
130inline IOUAmount&
132{
133 *this += -other;
134 return *this;
135}
136
137inline IOUAmount
139{
140 return {-mantissa_, exponent_};
141}
142
143inline bool
145{
146 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
147}
148
149inline bool
150IOUAmount::operator<(IOUAmount const& other) const
151{
152 return Number{*this} < Number{other};
153}
154
155inline IOUAmount::operator bool() const noexcept
156{
157 return mantissa_ != 0;
158}
159
160inline int
161IOUAmount::signum() const noexcept
162{
163 return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
164}
165
166inline int
167IOUAmount::exponent() const noexcept
168{
169 return exponent_;
170}
171
172inline std::int64_t
173IOUAmount::mantissa() const noexcept
174{
175 return mantissa_;
176}
177
179to_string(IOUAmount const& amount);
180
181/* Return num*amt/den
182 This function keeps more precision than computing
183 num*amt, storing the result in an IOUAmount, then
184 dividing by den.
185*/
188 IOUAmount const& amt,
189 std::uint32_t num,
190 std::uint32_t den,
191 bool roundUp);
192
193// Since many uses of the number class do not have access to a ledger,
194// getSTNumberSwitchover needs to be globally accessible.
195
196bool
198
199void
201
206{
207 bool saved_;
208
209public:
211 {
213 }
214
215 NumberSO(NumberSO const&) = delete;
216 NumberSO&
217 operator=(NumberSO const&) = delete;
218
220 {
222 }
223};
224
225} // namespace ripple
226
227#endif
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:47
IOUAmount operator-() const
Definition: IOUAmount.h:138
int exponent() const noexcept
Definition: IOUAmount.h:167
std::int64_t mantissa_
Definition: IOUAmount.h:49
bool operator<(IOUAmount const &other) const
Definition: IOUAmount.h:150
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:161
bool operator==(IOUAmount const &other) const
Definition: IOUAmount.h:144
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition: IOUAmount.cpp:75
std::int64_t mantissa() const noexcept
Definition: IOUAmount.h:173
IOUAmount & operator=(beast::Zero)
Definition: IOUAmount.h:116
IOUAmount & operator+=(IOUAmount const &other)
Definition: IOUAmount.cpp:138
IOUAmount()=default
static IOUAmount minPositiveAmount()
Definition: IOUAmount.cpp:69
IOUAmount & operator-=(IOUAmount const &other)
Definition: IOUAmount.h:131
RAII class to set and restore the Number switchover.
Definition: IOUAmount.h:206
NumberSO & operator=(NumberSO const &)=delete
NumberSO(NumberSO const &)=delete
NumberSO(bool v)
Definition: IOUAmount.h:219
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
void setSTNumberSwitchover(bool v)
Definition: IOUAmount.cpp:56
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition: IOUAmount.cpp:190
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
bool getSTNumberSwitchover()
Definition: IOUAmount.cpp:50
STL namespace.
Zero allows classes to offer efficient comparisons to zero.
Definition: Zero.h:43