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 friend std::ostream&
103 operator<<(std::ostream& os, IOUAmount const& x)
104 {
105 return os << to_string(x);
106 }
107};
108
110{
111 *this = beast::zero;
112}
113
114inline IOUAmount::IOUAmount(std::int64_t mantissa, int exponent)
115 : mantissa_(mantissa), exponent_(exponent)
116{
117 normalize();
118}
119
120inline IOUAmount&
122{
123 // The -100 is used to allow 0 to sort less than small positive values
124 // which will have a large negative exponent.
125 mantissa_ = 0;
126 exponent_ = -100;
127 return *this;
128}
129
130inline IOUAmount::operator Number() const
131{
132 return Number{mantissa_, exponent_};
133}
134
135inline IOUAmount&
137{
138 *this += -other;
139 return *this;
140}
141
142inline IOUAmount
144{
145 return {-mantissa_, exponent_};
146}
147
148inline bool
150{
151 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
152}
153
154inline bool
155IOUAmount::operator<(IOUAmount const& other) const
156{
157 return Number{*this} < Number{other};
158}
159
160inline IOUAmount::operator bool() const noexcept
161{
162 return mantissa_ != 0;
163}
164
165inline int
166IOUAmount::signum() const noexcept
167{
168 return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
169}
170
171inline int
172IOUAmount::exponent() const noexcept
173{
174 return exponent_;
175}
176
177inline std::int64_t
178IOUAmount::mantissa() const noexcept
179{
180 return mantissa_;
181}
182
184to_string(IOUAmount const& amount);
185
186/* Return num*amt/den
187 This function keeps more precision than computing
188 num*amt, storing the result in an IOUAmount, then
189 dividing by den.
190*/
193 IOUAmount const& amt,
194 std::uint32_t num,
195 std::uint32_t den,
196 bool roundUp);
197
198// Since many uses of the number class do not have access to a ledger,
199// getSTNumberSwitchover needs to be globally accessible.
200
201bool
203
204void
206
211{
212 bool saved_;
213
214public:
219
220 NumberSO(NumberSO const&) = delete;
221 NumberSO&
222 operator=(NumberSO const&) = delete;
223
225 {
227 }
228};
229
230} // namespace ripple
231
232#endif
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:46
IOUAmount operator-() const
Definition IOUAmount.h:143
int exponent() const noexcept
Definition IOUAmount.h:172
std::int64_t mantissa_
Definition IOUAmount.h:48
bool operator<(IOUAmount const &other) const
Definition IOUAmount.h:155
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:166
bool operator==(IOUAmount const &other) const
Definition IOUAmount.h:149
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition IOUAmount.cpp:75
std::int64_t mantissa() const noexcept
Definition IOUAmount.h:178
IOUAmount & operator=(beast::Zero)
Definition IOUAmount.h:121
IOUAmount & operator+=(IOUAmount const &other)
IOUAmount()=default
static IOUAmount minPositiveAmount()
Definition IOUAmount.cpp:69
IOUAmount & operator-=(IOUAmount const &other)
Definition IOUAmount.h:136
RAII class to set and restore the Number switchover.
Definition IOUAmount.h:211
NumberSO & operator=(NumberSO const &)=delete
NumberSO(NumberSO const &)=delete
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void setSTNumberSwitchover(bool v)
Definition IOUAmount.cpp:56
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
bool getSTNumberSwitchover()
Definition IOUAmount.cpp:50
STL namespace.
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:43