Bitcoin ABC  0.24.7
P2P Digital Currency
amount.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_AMOUNT_H
8 #define BITCOIN_AMOUNT_H
9 
10 #include <serialize.h>
11 
12 #include <cstdlib>
13 #include <ostream>
14 #include <string>
15 #include <type_traits>
16 
17 class UniValue;
18 
19 struct Amount {
20 private:
21  int64_t amount;
22 
23  explicit constexpr Amount(int64_t _amount) : amount(_amount) {}
24 
25 public:
26  constexpr Amount() : amount(0) {}
27  constexpr Amount(const Amount &other) : amount(other.amount) {}
28 
32  operator UniValue() const;
33 
37  constexpr Amount &operator=(const Amount &other) {
38  amount = other.amount;
39  return *this;
40  }
41 
42  static constexpr Amount zero() { return Amount(0); }
43  static constexpr Amount satoshi() { return Amount(1); }
44 
48  Amount &operator+=(const Amount a) {
49  amount += a.amount;
50  return *this;
51  }
52  Amount &operator-=(const Amount a) {
53  amount -= a.amount;
54  return *this;
55  }
56 
60  friend constexpr bool operator==(const Amount a, const Amount b) {
61  return a.amount == b.amount;
62  }
63  friend constexpr bool operator!=(const Amount a, const Amount b) {
64  return !(a == b);
65  }
66 
70  friend constexpr bool operator<(const Amount a, const Amount b) {
71  return a.amount < b.amount;
72  }
73  friend constexpr bool operator>(const Amount a, const Amount b) {
74  return b < a;
75  }
76  friend constexpr bool operator<=(const Amount a, const Amount b) {
77  return !(a > b);
78  }
79  friend constexpr bool operator>=(const Amount a, const Amount b) {
80  return !(a < b);
81  }
82 
86  constexpr Amount operator-() const { return Amount(-amount); }
87 
91  friend constexpr Amount operator+(const Amount a, const Amount b) {
92  return Amount(a.amount + b.amount);
93  }
94  friend constexpr Amount operator-(const Amount a, const Amount b) {
95  return a + -b;
96  }
97 
101  friend constexpr Amount operator*(const int64_t a, const Amount b) {
102  return Amount(a * b.amount);
103  }
104  friend constexpr Amount operator*(const int a, const Amount b) {
105  return Amount(a * b.amount);
106  }
107 
111  constexpr int64_t operator/(const Amount b) const {
112  return amount / b.amount;
113  }
114  constexpr Amount operator/(const int64_t b) const {
115  return Amount(amount / b);
116  }
117  constexpr Amount operator/(const int b) const { return Amount(amount / b); }
118  Amount &operator/=(const int64_t n) {
119  amount /= n;
120  return *this;
121  }
122 
126  constexpr Amount operator%(const Amount b) const {
127  return Amount(amount % b.amount);
128  }
129  constexpr Amount operator%(const int64_t b) const {
130  return Amount(amount % b);
131  }
132  constexpr Amount operator%(const int b) const { return Amount(amount % b); }
133 
138  friend constexpr Amount operator*(const double a, const Amount b) = delete;
139  constexpr Amount operator/(const double b) const = delete;
140  constexpr Amount operator%(const double b) const = delete;
141 
142  // ostream support
143  friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) {
144  return stream << ca.amount;
145  }
146 
147  std::string ToString() const;
148 
149  // serialization support
150  SERIALIZE_METHODS(Amount, obj) { READWRITE(obj.amount); }
151 };
152 
153 static constexpr Amount SATOSHI = Amount::satoshi();
154 static constexpr Amount COIN = 100000000 * SATOSHI;
155 
156 struct Currency {
159  uint8_t decimals;
160  std::string ticker;
161 
162  static const Currency &get();
163 };
164 
175 static const Amount MAX_MONEY = 21000000 * COIN;
176 inline bool MoneyRange(const Amount nValue) {
177  return nValue >= Amount::zero() && nValue <= MAX_MONEY;
178 }
179 
180 #endif // BITCOIN_AMOUNT_H
Amount::operator<=
constexpr friend bool operator<=(const Amount a, const Amount b)
Definition: amount.h:76
Amount::operator==
constexpr friend bool operator==(const Amount a, const Amount b)
Equality.
Definition: amount.h:60
MoneyRange
bool MoneyRange(const Amount nValue)
Definition: amount.h:176
Amount::ToString
std::string ToString() const
Definition: amount.cpp:23
Amount::operator%
constexpr Amount operator%(const int b) const
Definition: amount.h:132
Currency::baseunit
Amount baseunit
Definition: amount.h:157
Amount::operator%
constexpr Amount operator%(const Amount b) const
Modulus.
Definition: amount.h:126
Amount::operator*
constexpr friend Amount operator*(const int a, const Amount b)
Definition: amount.h:104
Amount::operator/
constexpr Amount operator/(const int b) const
Definition: amount.h:117
Amount::operator/=
Amount & operator/=(const int64_t n)
Definition: amount.h:118
COIN
static constexpr Amount COIN
Definition: amount.h:154
UniValue
Definition: univalue.h:23
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
Amount::operator*
constexpr friend Amount operator*(const int64_t a, const Amount b)
Multiplication.
Definition: amount.h:101
Currency::decimals
uint8_t decimals
Definition: amount.h:159
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
Amount::Amount
constexpr Amount()
Definition: amount.h:26
Amount::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Amount &ca)
Definition: amount.h:143
Amount::operator+=
Amount & operator+=(const Amount a)
Implement standard operators.
Definition: amount.h:48
Amount::operator+
constexpr friend Amount operator+(const Amount a, const Amount b)
Addition and subtraction.
Definition: amount.h:91
Amount::SERIALIZE_METHODS
SERIALIZE_METHODS(Amount, obj)
Definition: amount.h:150
Currency::get
static const Currency & get()
Definition: amount.cpp:19
Amount::operator-=
Amount & operator-=(const Amount a)
Definition: amount.h:52
Amount::amount
int64_t amount
Definition: amount.h:21
Amount::operator=
constexpr Amount & operator=(const Amount &other)
Assignement operator.
Definition: amount.h:37
Amount::operator<
constexpr friend bool operator<(const Amount a, const Amount b)
Comparison.
Definition: amount.h:70
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
Amount
Definition: amount.h:19
Amount::operator-
constexpr friend Amount operator-(const Amount a, const Amount b)
Definition: amount.h:94
Currency::ticker
std::string ticker
Definition: amount.h:160
Amount::operator%
constexpr Amount operator%(const int64_t b) const
Definition: amount.h:129
Amount::operator-
constexpr Amount operator-() const
Unary minus.
Definition: amount.h:86
Amount::operator/
constexpr int64_t operator/(const Amount b) const
Division.
Definition: amount.h:111
Amount::operator>
constexpr friend bool operator>(const Amount a, const Amount b)
Definition: amount.h:73
serialize.h
Amount::operator>=
constexpr friend bool operator>=(const Amount a, const Amount b)
Definition: amount.h:79
Currency
Definition: amount.h:156
Amount::Amount
constexpr Amount(const Amount &other)
Definition: amount.h:27
MAX_MONEY
static const Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:175
Amount::Amount
constexpr Amount(int64_t _amount)
Definition: amount.h:23
Currency::subunit
Amount subunit
Definition: amount.h:158
Amount::satoshi
static constexpr Amount satoshi()
Definition: amount.h:43
Amount::operator/
constexpr Amount operator/(const int64_t b) const
Definition: amount.h:114
Amount::operator!=
constexpr friend bool operator!=(const Amount a, const Amount b)
Definition: amount.h:63