Bitcoin ABC  0.22.13
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 struct Amount {
18 private:
19  int64_t amount;
20 
21  explicit constexpr Amount(int64_t _amount) : amount(_amount) {}
22 
23 public:
24  constexpr Amount() : amount(0) {}
25  constexpr Amount(const Amount &other) : amount(other.amount) {}
26 
30  constexpr Amount &operator=(const Amount &other) {
31  amount = other.amount;
32  return *this;
33  }
34 
35  static constexpr Amount zero() { return Amount(0); }
36  static constexpr Amount satoshi() { return Amount(1); }
37 
41  Amount &operator+=(const Amount a) {
42  amount += a.amount;
43  return *this;
44  }
45  Amount &operator-=(const Amount a) {
46  amount -= a.amount;
47  return *this;
48  }
49 
53  friend constexpr bool operator==(const Amount a, const Amount b) {
54  return a.amount == b.amount;
55  }
56  friend constexpr bool operator!=(const Amount a, const Amount b) {
57  return !(a == b);
58  }
59 
63  friend constexpr bool operator<(const Amount a, const Amount b) {
64  return a.amount < b.amount;
65  }
66  friend constexpr bool operator>(const Amount a, const Amount b) {
67  return b < a;
68  }
69  friend constexpr bool operator<=(const Amount a, const Amount b) {
70  return !(a > b);
71  }
72  friend constexpr bool operator>=(const Amount a, const Amount b) {
73  return !(a < b);
74  }
75 
79  constexpr Amount operator-() const { return Amount(-amount); }
80 
84  friend constexpr Amount operator+(const Amount a, const Amount b) {
85  return Amount(a.amount + b.amount);
86  }
87  friend constexpr Amount operator-(const Amount a, const Amount b) {
88  return a + -b;
89  }
90 
94  friend constexpr Amount operator*(const int64_t a, const Amount b) {
95  return Amount(a * b.amount);
96  }
97  friend constexpr Amount operator*(const int a, const Amount b) {
98  return Amount(a * b.amount);
99  }
100 
104  constexpr int64_t operator/(const Amount b) const {
105  return amount / b.amount;
106  }
107  constexpr Amount operator/(const int64_t b) const {
108  return Amount(amount / b);
109  }
110  constexpr Amount operator/(const int b) const { return Amount(amount / b); }
111  Amount &operator/=(const int64_t n) {
112  amount /= n;
113  return *this;
114  }
115 
119  constexpr Amount operator%(const Amount b) const {
120  return Amount(amount % b.amount);
121  }
122  constexpr Amount operator%(const int64_t b) const {
123  return Amount(amount % b);
124  }
125  constexpr Amount operator%(const int b) const { return Amount(amount % b); }
126 
131  friend constexpr Amount operator*(const double a, const Amount b) = delete;
132  constexpr Amount operator/(const double b) const = delete;
133  constexpr Amount operator%(const double b) const = delete;
134 
135  // ostream support
136  friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) {
137  return stream << ca.amount;
138  }
139 
140  std::string ToString() const;
141 
142  // serialization support
144 
145  template <typename Stream, typename Operation>
146  inline void SerializationOp(Stream &s, Operation ser_action) {
147  READWRITE(amount);
148  }
149 };
150 
151 static constexpr Amount SATOSHI = Amount::satoshi();
152 static constexpr Amount CASH = 100 * SATOSHI;
153 static constexpr Amount COIN = 100000000 * SATOSHI;
154 
165 static const Amount MAX_MONEY = 21000000 * COIN;
166 inline bool MoneyRange(const Amount nValue) {
167  return nValue >= Amount::zero() && nValue <= MAX_MONEY;
168 }
169 
170 #endif // BITCOIN_AMOUNT_H
constexpr Amount operator/(const int b) const
Definition: amount.h:110
friend constexpr bool operator!=(const Amount a, const Amount b)
Definition: amount.h:56
static constexpr Amount zero()
Definition: amount.h:35
std::string ToString() const
Definition: amount.cpp:13
static constexpr Amount CASH
Definition: amount.h:152
constexpr Amount operator-() const
Unary minus.
Definition: amount.h:79
constexpr Amount(const Amount &other)
Definition: amount.h:25
Definition: amount.h:17
static constexpr Amount SATOSHI
Definition: amount.h:151
static const Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:165
constexpr Amount operator%(const int b) const
Definition: amount.h:125
friend std::ostream & operator<<(std::ostream &stream, const Amount &ca)
Definition: amount.h:136
constexpr Amount & operator=(const Amount &other)
Assignement operator.
Definition: amount.h:30
friend constexpr Amount operator*(const int a, const Amount b)
Definition: amount.h:97
Amount & operator+=(const Amount a)
Implement standard operators.
Definition: amount.h:41
constexpr Amount operator/(const int64_t b) const
Definition: amount.h:107
friend constexpr bool operator>=(const Amount a, const Amount b)
Definition: amount.h:72
Amount & operator-=(const Amount a)
Definition: amount.h:45
Amount & operator/=(const int64_t n)
Definition: amount.h:111
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
friend constexpr bool operator<=(const Amount a, const Amount b)
Definition: amount.h:69
void SerializationOp(Stream &s, Operation ser_action)
Definition: amount.h:146
static constexpr Amount COIN
Definition: amount.h:153
friend constexpr bool operator>(const Amount a, const Amount b)
Definition: amount.h:66
static constexpr Amount satoshi()
Definition: amount.h:36
constexpr Amount operator%(const int64_t b) const
Definition: amount.h:122
friend constexpr Amount operator*(const int64_t a, const Amount b)
Multiplication.
Definition: amount.h:94
constexpr Amount operator%(const Amount b) const
Modulus.
Definition: amount.h:119
constexpr Amount(int64_t _amount)
Definition: amount.h:21
constexpr Amount()
Definition: amount.h:24
int64_t amount
Definition: amount.h:19
friend constexpr bool operator<(const Amount a, const Amount b)
Comparison.
Definition: amount.h:63
friend constexpr bool operator==(const Amount a, const Amount b)
Equality.
Definition: amount.h:53
friend constexpr Amount operator-(const Amount a, const Amount b)
Definition: amount.h:87
constexpr int64_t operator/(const Amount b) const
Division.
Definition: amount.h:104
#define READWRITE(...)
Definition: serialize.h:191
friend constexpr Amount operator+(const Amount a, const Amount b)
Addition and subtraction.
Definition: amount.h:84
ADD_SERIALIZE_METHODS
Definition: amount.h:143