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