Bitcoin ABC  0.28.12
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() noexcept : amount(0) {}
30  operator UniValue() const;
31 
32  static constexpr Amount zero() noexcept { return Amount(0); }
33  static constexpr Amount satoshi() noexcept { return Amount(1); }
34 
38  Amount &operator+=(const Amount a) {
39  amount += a.amount;
40  return *this;
41  }
42  Amount &operator-=(const Amount a) {
43  amount -= a.amount;
44  return *this;
45  }
46 
50  friend constexpr bool operator==(const Amount a, const Amount b) {
51  return a.amount == b.amount;
52  }
53  friend constexpr bool operator!=(const Amount a, const Amount b) {
54  return !(a == b);
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 b < a;
65  }
66  friend constexpr bool operator<=(const Amount a, const Amount b) {
67  return !(a > b);
68  }
69  friend constexpr bool operator>=(const Amount a, const Amount b) {
70  return !(a < b);
71  }
72 
76  constexpr Amount operator-() const { return Amount(-amount); }
77 
81  friend constexpr Amount operator+(const Amount a, const Amount b) {
82  return Amount(a.amount + b.amount);
83  }
84  friend constexpr Amount operator-(const Amount a, const Amount b) {
85  return a + -b;
86  }
87 
91  friend constexpr Amount operator*(const int64_t a, const Amount b) {
92  return Amount(a * b.amount);
93  }
94  friend constexpr Amount operator*(const int a, const Amount b) {
95  return Amount(a * b.amount);
96  }
97 
101  constexpr int64_t operator/(const Amount b) const {
102  return amount / b.amount;
103  }
104  constexpr Amount operator/(const int64_t b) const {
105  return Amount(amount / b);
106  }
107  constexpr Amount operator/(const int b) const { return Amount(amount / b); }
108  Amount &operator/=(const int64_t n) {
109  amount /= n;
110  return *this;
111  }
112 
116  constexpr Amount operator%(const Amount b) const {
117  return Amount(amount % b.amount);
118  }
119  constexpr Amount operator%(const int64_t b) const {
120  return Amount(amount % b);
121  }
122  constexpr Amount operator%(const int b) const { return Amount(amount % b); }
123 
128  friend constexpr Amount operator*(const double a, const Amount b) = delete;
129  constexpr Amount operator/(const double b) const = delete;
130  constexpr Amount operator%(const double b) const = delete;
131 
132  // ostream support
133  friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) {
134  return stream << ca.amount;
135  }
136 
137  std::string ToString() const;
138 
139  // serialization support
140  SERIALIZE_METHODS(Amount, obj) { READWRITE(obj.amount); }
141 };
142 
143 static constexpr Amount SATOSHI = Amount::satoshi();
144 static constexpr Amount COIN = 100000000 * SATOSHI;
145 
146 struct Currency {
149  uint8_t decimals;
150  std::string ticker;
151 
152  static const Currency &get();
153 };
154 
165 static constexpr 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_CONSENSUS_AMOUNT_H
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:165
static constexpr Amount COIN
Definition: amount.h:144
#define READWRITE(...)
Definition: serialize.h:180
Definition: amount.h:19
Amount & operator/=(const int64_t n)
Definition: amount.h:108
constexpr Amount operator%(const double b) const =delete
SERIALIZE_METHODS(Amount, obj)
Definition: amount.h:140
static constexpr Amount zero() noexcept
Definition: amount.h:32
constexpr Amount operator%(const int64_t b) const
Definition: amount.h:119
constexpr int64_t operator/(const Amount b) const
Division.
Definition: amount.h:101
int64_t amount
Definition: amount.h:21
constexpr friend bool operator!=(const Amount a, const Amount b)
Definition: amount.h:53
constexpr friend Amount operator*(const int64_t a, const Amount b)
Multiplication.
Definition: amount.h:91
constexpr friend bool operator<=(const Amount a, const Amount b)
Definition: amount.h:66
friend std::ostream & operator<<(std::ostream &stream, const Amount &ca)
Definition: amount.h:133
constexpr friend Amount operator-(const Amount a, const Amount b)
Definition: amount.h:84
constexpr Amount operator/(const double b) const =delete
Amount & operator-=(const Amount a)
Definition: amount.h:42
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:81
constexpr friend bool operator==(const Amount a, const Amount b)
Equality.
Definition: amount.h:50
constexpr Amount operator-() const
Unary minus.
Definition: amount.h:76
constexpr Amount operator/(const int64_t b) const
Definition: amount.h:104
constexpr friend bool operator<(const Amount a, const Amount b)
Comparison.
Definition: amount.h:60
constexpr friend bool operator>(const Amount a, const Amount b)
Definition: amount.h:63
constexpr Amount operator%(const Amount b) const
Modulus.
Definition: amount.h:116
constexpr Amount() noexcept
Definition: amount.h:26
std::string ToString() const
Definition: amount.cpp:22
constexpr Amount operator%(const int b) const
Definition: amount.h:122
constexpr friend bool operator>=(const Amount a, const Amount b)
Definition: amount.h:69
constexpr Amount(int64_t _amount)
Definition: amount.h:23
Amount & operator+=(const Amount a)
Implement standard operators.
Definition: amount.h:38
static constexpr Amount satoshi() noexcept
Definition: amount.h:33
constexpr Amount operator/(const int b) const
Definition: amount.h:107
constexpr friend Amount operator*(const int a, const Amount b)
Definition: amount.h:94
Amount baseunit
Definition: amount.h:147
static const Currency & get()
Definition: amount.cpp:18
std::string ticker
Definition: amount.h:150
uint8_t decimals
Definition: amount.h:149
Amount subunit
Definition: amount.h:148