Bitcoin ABC  0.26.3
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_CONSENSUS_AMOUNT_H
8 #define BITCOIN_CONSENSUS_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 constexpr 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_CONSENSUS_AMOUNT_H
bool MoneyRange(const Amount nValue)
Definition: amount.h:176
static constexpr Amount SATOSHI
Definition: amount.h:153
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:175
static constexpr Amount COIN
Definition: amount.h:154
#define READWRITE(...)
Definition: serialize.h:180
Definition: amount.h:19
Amount & operator/=(const int64_t n)
Definition: amount.h:118
constexpr Amount(const Amount &other)
Definition: amount.h:27
constexpr Amount operator%(const double b) const =delete
SERIALIZE_METHODS(Amount, obj)
Definition: amount.h:150
constexpr Amount operator%(const int64_t b) const
Definition: amount.h:129
constexpr int64_t operator/(const Amount b) const
Division.
Definition: amount.h:111
int64_t amount
Definition: amount.h:21
constexpr friend bool operator!=(const Amount a, const Amount b)
Definition: amount.h:63
constexpr friend Amount operator*(const int64_t a, const Amount b)
Multiplication.
Definition: amount.h:101
static constexpr Amount zero()
Definition: amount.h:42
constexpr friend bool operator<=(const Amount a, const Amount b)
Definition: amount.h:76
friend std::ostream & operator<<(std::ostream &stream, const Amount &ca)
Definition: amount.h:143
constexpr Amount()
Definition: amount.h:26
constexpr friend Amount operator-(const Amount a, const Amount b)
Definition: amount.h:94
constexpr Amount operator/(const double b) const =delete
Amount & operator-=(const Amount a)
Definition: amount.h:52
constexpr friend Amount operator*(const double a, const Amount b)=delete
Do not implement double ops to get an error with double and ensure casting to integer is explicit.
constexpr friend Amount operator+(const Amount a, const Amount b)
Addition and subtraction.
Definition: amount.h:91
constexpr friend bool operator==(const Amount a, const Amount b)
Equality.
Definition: amount.h:60
constexpr Amount operator-() const
Unary minus.
Definition: amount.h:86
constexpr Amount operator/(const int64_t b) const
Definition: amount.h:114
constexpr friend bool operator<(const Amount a, const Amount b)
Comparison.
Definition: amount.h:70
constexpr friend bool operator>(const Amount a, const Amount b)
Definition: amount.h:73
constexpr Amount operator%(const Amount b) const
Modulus.
Definition: amount.h:126
static constexpr Amount satoshi()
Definition: amount.h:43
std::string ToString() const
Definition: amount.cpp:22
constexpr Amount operator%(const int b) const
Definition: amount.h:132
constexpr friend bool operator>=(const Amount a, const Amount b)
Definition: amount.h:79
constexpr Amount(int64_t _amount)
Definition: amount.h:23
Amount & operator+=(const Amount a)
Implement standard operators.
Definition: amount.h:48
constexpr Amount operator/(const int b) const
Definition: amount.h:117
constexpr friend Amount operator*(const int a, const Amount b)
Definition: amount.h:104
constexpr Amount & operator=(const Amount &other)
Assignement operator.
Definition: amount.h:37
Amount baseunit
Definition: amount.h:157
static const Currency & get()
Definition: amount.cpp:18
std::string ticker
Definition: amount.h:160
uint8_t decimals
Definition: amount.h:159
Amount subunit
Definition: amount.h:158