Changeset View
Changeset View
Standalone View
Standalone View
src/amount.h
Show All 23 Lines | public: | ||||
explicit constexpr Amount(T _camount) : amount(_camount) { | explicit constexpr Amount(T _camount) : amount(_camount) { | ||||
static_assert(std::is_integral<T>(), | static_assert(std::is_integral<T>(), | ||||
"Only integer types can be used as amounts"); | "Only integer types can be used as amounts"); | ||||
} | } | ||||
constexpr Amount(const Amount &_camount) : amount(_camount.amount) {} | constexpr Amount(const Amount &_camount) : amount(_camount.amount) {} | ||||
// Allow access to underlying value for non-monetary operations | // Allow access to underlying value for non-monetary operations | ||||
int64_t GetSatoshis() const { return amount; } | int64_t GetSatoshis() const { return *this / Amount(1); } | ||||
/** | /** | ||||
* Implement standard operators | * Implement standard operators | ||||
*/ | */ | ||||
Amount &operator+=(const Amount a) { | Amount &operator+=(const Amount a) { | ||||
amount += a.amount; | amount += a.amount; | ||||
return *this; | return *this; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | constexpr Amount operator/(const int64_t b) const { | ||||
return Amount(amount / b); | return Amount(amount / b); | ||||
} | } | ||||
constexpr Amount operator/(const int b) const { return Amount(amount / b); } | constexpr Amount operator/(const int b) const { return Amount(amount / b); } | ||||
/** | /** | ||||
* Modulus | * Modulus | ||||
*/ | */ | ||||
constexpr int64_t operator%(const Amount b) const { | constexpr int64_t operator%(const Amount b) const { | ||||
return amount % b.amount; | return Amount(amount % b.amount) / Amount(1); | ||||
} | } | ||||
constexpr Amount operator%(const int64_t b) const { | constexpr Amount operator%(const int64_t b) const { | ||||
return Amount(amount % b); | return Amount(amount % b); | ||||
} | } | ||||
constexpr Amount operator%(const int b) const { return Amount(amount % b); } | constexpr Amount operator%(const int b) const { return Amount(amount % b); } | ||||
/** | /** | ||||
* Do not implement double ops to get an error with double and ensure | * Do not implement double ops to get an error with double and ensure | ||||
Show All 14 Lines | public: | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(amount); | READWRITE(amount); | ||||
} | } | ||||
}; | }; | ||||
static const Amount COIN(100000000); | static const Amount SATOSHI(1); | ||||
static const Amount CENT(1000000); | static const Amount CASH = 100 * SATOSHI; | ||||
static const Amount COIN = 100000000 * SATOSHI; | |||||
static const Amount CENT = COIN / 100; | |||||
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 BCH for various reasons, but | * currently happens to be less than 21,000,000 BCH for various reasons, but | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |