Changeset View
Changeset View
Standalone View
Standalone View
src/amount.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_AMOUNT_H | #ifndef BITCOIN_AMOUNT_H | ||||
#define BITCOIN_AMOUNT_H | #define BITCOIN_AMOUNT_H | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include <cstdlib> | #include <cstdlib> | ||||
#include <iostream> | |||||
#include <string> | #include <string> | ||||
#include <type_traits> | |||||
struct Amount { | |||||
private: | |||||
int64_t amount; | |||||
public: | |||||
template <typename T, typename std::enable_if<(std::is_integral<T>::value), | |||||
T>::type = 0> | |||||
Amount(T _camount) : amount(_camount) {} | |||||
Amount(const Amount &_camount) : amount(_camount.amount) {} | |||||
// Allow access to underlying value for non-monetary operations | |||||
int64_t GetSatoshis() const { return amount; } | |||||
/* | |||||
* Implement standard operators | |||||
*/ | |||||
Amount &operator+=(const Amount a) { | |||||
amount += a.amount; | |||||
return *this; | |||||
} | |||||
Amount &operator-=(const Amount a) { | |||||
amount -= a.amount; | |||||
return *this; | |||||
} | |||||
friend bool operator<(const Amount a, const Amount b) { | |||||
return a.amount < b.amount; | |||||
} | |||||
friend bool operator==(const Amount a, const Amount b) { | |||||
return a.amount == b.amount; | |||||
} | |||||
friend bool operator>(const Amount a, const Amount b) { | |||||
return b.amount < a.amount; | |||||
} | |||||
friend bool operator!=(const Amount a, const Amount b) { | |||||
return !(a.amount == b.amount); | |||||
} | |||||
friend bool operator<=(const Amount a, const Amount b) { | |||||
return !(a.amount > b.amount); | |||||
} | |||||
friend bool operator>=(const Amount a, const Amount b) { | |||||
return !(a.amount < b.amount); | |||||
} | |||||
friend Amount operator+(const Amount a, const Amount b) { | |||||
return Amount(a.amount + b.amount); | |||||
} | |||||
friend Amount operator-(const Amount a, const Amount b) { | |||||
return Amount(a.amount - b.amount); | |||||
} | |||||
// Implemented for allowing COIN as a base unit. | |||||
template <typename T, typename std::enable_if<(std::is_integral<T>::value), | |||||
T>::type = 0> | |||||
friend Amount operator*(const T a, const Amount b) { | |||||
return Amount(a * b.amount); | |||||
} | |||||
// ostream support | |||||
friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) { | |||||
return stream << ca.amount; | |||||
} | |||||
std::string ToString() const; | |||||
// serialization support | |||||
ADD_SERIALIZE_METHODS; | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(amount); | |||||
} | |||||
}; | |||||
/** Amount in satoshis (Can be negative) */ | /** Amount in satoshis (Can be negative) */ | ||||
typedef int64_t CAmount; | typedef int64_t CAmount; | ||||
static const CAmount COIN = 100000000; | static const Amount COIN = 100000000; | ||||
static const CAmount CENT = 1000000; | static const Amount CENT = 1000000; | ||||
extern const std::string CURRENCY_UNIT; | extern const std::string CURRENCY_UNIT; | ||||
/** | /** | ||||
* No amount larger than this (in satoshi) is valid. | * No amount larger than this (in satoshi) is valid. | ||||
* | * | ||||
* Note that this constant is *not* the total money supply, which in Bitcoin | * Note that this constant is *not* the total money supply, which in Bitcoin | ||||
* currently happens to be less than 21,000,000 BCC for various reasons, but | * currently happens to be less than 21,000,000 BCC for various reasons, but | ||||
* rather a sanity check. As this sanity check is used by consensus-critical | * rather a sanity check. As this sanity check is used by consensus-critical | ||||
* validation code, the exact value of the MAX_MONEY constant is consensus | * validation code, the exact value of the MAX_MONEY constant is consensus | ||||
* critical; in unusual circumstances like a(nother) overflow bug that allowed | * critical; in unusual circumstances like a(nother) overflow bug that allowed | ||||
* for the creation of coins out of thin air modification could lead to a fork. | * for the creation of coins out of thin air modification could lead to a fork. | ||||
*/ | */ | ||||
static const CAmount MAX_MONEY = 21000000 * COIN; | static const Amount MAX_MONEY = 21000000 * COIN; | ||||
inline bool MoneyRange(const CAmount &nValue) { | inline bool MoneyRange(const CAmount &nValue) { | ||||
return (nValue >= 0 && nValue <= MAX_MONEY); | return (nValue >= 0 && nValue <= MAX_MONEY); | ||||
} | } | ||||
/** | /** | ||||
* Fee rate in satoshis per kilobyte: CAmount / kB | * Fee rate in satoshis per kilobyte: CAmount / kB | ||||
*/ | */ | ||||
class CFeeRate { | class CFeeRate { | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |