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#include <boost/operators.hpp>
27#include <cstdint>
28#include <string>
29#include <utility>
30
31namespace ripple {
32
43class IOUAmount : private boost::totally_ordered<IOUAmount>,
44 private boost::additive<IOUAmount>
45{
46private:
49
56 void
57 normalize();
58
59public:
60 IOUAmount() = default;
61 explicit IOUAmount(Number const& other);
64
66
67 operator Number() const;
68
70 operator+=(IOUAmount const& other);
71
73 operator-=(IOUAmount const& other);
74
76 operator-() const;
77
78 bool
79 operator==(IOUAmount const& other) const;
80
81 bool
82 operator<(IOUAmount const& other) const;
83
85 explicit
86 operator bool() const noexcept;
87
89 int
90 signum() const noexcept;
91
92 int
93 exponent() const noexcept;
94
95 std::int64_t
96 mantissa() const noexcept;
97
98 static IOUAmount
100};
101
103{
104 *this = beast::zero;
105}
106
109{
110 normalize();
111}
112
113inline IOUAmount&
115{
116 // The -100 is used to allow 0 to sort less than small positive values
117 // which will have a large negative exponent.
118 mantissa_ = 0;
119 exponent_ = -100;
120 return *this;
121}
122
123inline IOUAmount::operator Number() const
124{
125 return Number{mantissa_, exponent_};
126}
127
128inline IOUAmount&
130{
131 *this += -other;
132 return *this;
133}
134
135inline IOUAmount
137{
138 return {-mantissa_, exponent_};
139}
140
141inline bool
143{
144 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
145}
146
147inline bool
148IOUAmount::operator<(IOUAmount const& other) const
149{
150 return Number{*this} < Number{other};
151}
152
153inline IOUAmount::operator bool() const noexcept
154{
155 return mantissa_ != 0;
156}
157
158inline int
159IOUAmount::signum() const noexcept
160{
161 return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
162}
163
164inline int
165IOUAmount::exponent() const noexcept
166{
167 return exponent_;
168}
169
170inline std::int64_t
171IOUAmount::mantissa() const noexcept
172{
173 return mantissa_;
174}
175
177to_string(IOUAmount const& amount);
178
179/* Return num*amt/den
180 This function keeps more precision than computing
181 num*amt, storing the result in an IOUAmount, then
182 dividing by den.
183*/
186 IOUAmount const& amt,
187 std::uint32_t num,
188 std::uint32_t den,
189 bool roundUp);
190
191// Since many uses of the number class do not have access to a ledger,
192// getSTNumberSwitchover needs to be globally accessible.
193
194bool
196
197void
199
204{
205 bool saved_;
206
207public:
209 {
211 }
212
213 NumberSO(NumberSO const&) = delete;
214 NumberSO&
215 operator=(NumberSO const&) = delete;
216
218 {
220 }
221};
222
223} // namespace ripple
224
225#endif
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:45
IOUAmount operator-() const
Definition: IOUAmount.h:136
int exponent() const noexcept
Definition: IOUAmount.h:165
std::int64_t mantissa_
Definition: IOUAmount.h:47
bool operator<(IOUAmount const &other) const
Definition: IOUAmount.h:148
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:159
bool operator==(IOUAmount const &other) const
Definition: IOUAmount.h:142
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition: IOUAmount.cpp:67
std::int64_t mantissa() const noexcept
Definition: IOUAmount.h:171
IOUAmount & operator=(beast::Zero)
Definition: IOUAmount.h:114
IOUAmount & operator+=(IOUAmount const &other)
Definition: IOUAmount.cpp:130
IOUAmount()=default
static IOUAmount minPositiveAmount()
Definition: IOUAmount.cpp:61
IOUAmount & operator-=(IOUAmount const &other)
Definition: IOUAmount.h:129
RAII class to set and restore the Number switchover.
Definition: IOUAmount.h:204
NumberSO & operator=(NumberSO const &)=delete
NumberSO(NumberSO const &)=delete
NumberSO(bool v)
Definition: IOUAmount.h:217
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:48
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
bool getSTNumberSwitchover()
Definition: IOUAmount.cpp:42
STL namespace.
Zero allows classes to offer efficient comparisons to zero.
Definition: Zero.h:43