Changeset View
Changeset View
Standalone View
Standalone View
src/amount.h
Show All 12 Lines | |||||
#include <string> | #include <string> | ||||
#include <type_traits> | #include <type_traits> | ||||
struct Amount { | struct Amount { | ||||
private: | private: | ||||
int64_t amount; | int64_t amount; | ||||
public: | public: | ||||
Amount() : amount(0) {} | constexpr Amount() : amount(0) {} | ||||
template <typename T> Amount(T _camount) : amount(_camount) { | template <typename T> 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"); | ||||
} | } | ||||
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 amount; } | ||||
/* | /* | ||||
* 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; | ||||
} | } | ||||
Amount &operator-=(const Amount a) { | Amount &operator-=(const Amount a) { | ||||
amount -= a.amount; | amount -= a.amount; | ||||
return *this; | return *this; | ||||
} | } | ||||
friend bool operator<(const Amount a, const Amount b) { | friend constexpr bool operator<(const Amount a, const Amount b) { | ||||
return a.amount < b.amount; | return a.amount < b.amount; | ||||
} | } | ||||
friend bool operator==(const Amount a, const Amount b) { | friend constexpr bool operator==(const Amount a, const Amount b) { | ||||
return a.amount == b.amount; | return a.amount == b.amount; | ||||
} | } | ||||
friend bool operator>(const Amount a, const Amount b) { | friend constexpr bool operator>(const Amount a, const Amount b) { | ||||
return b.amount < a.amount; | return b.amount < a.amount; | ||||
} | } | ||||
friend bool operator!=(const Amount a, const Amount b) { | friend constexpr bool operator!=(const Amount a, const Amount b) { | ||||
return !(a.amount == b.amount); | return !(a.amount == b.amount); | ||||
} | } | ||||
friend bool operator<=(const Amount a, const Amount b) { | friend constexpr bool operator<=(const Amount a, const Amount b) { | ||||
return !(a.amount > b.amount); | return !(a.amount > b.amount); | ||||
} | } | ||||
friend bool operator>=(const Amount a, const Amount b) { | friend constexpr bool operator>=(const Amount a, const Amount b) { | ||||
return !(a.amount < b.amount); | return !(a.amount < b.amount); | ||||
} | } | ||||
friend Amount operator+(const Amount a, const Amount b) { | friend constexpr Amount operator+(const Amount a, const Amount b) { | ||||
return Amount(a.amount + b.amount); | return Amount(a.amount + b.amount); | ||||
} | } | ||||
friend Amount operator-(const Amount a, const Amount b) { | friend constexpr Amount operator-(const Amount a, const Amount b) { | ||||
return Amount(a.amount - b.amount); | return Amount(a.amount - b.amount); | ||||
} | } | ||||
// Implemented for allowing COIN as a base unit. | // Implemented for allowing COIN as a base unit. | ||||
friend Amount operator*(const int64_t a, const Amount b) { | friend constexpr Amount operator*(const int64_t a, const Amount b) { | ||||
return Amount(a * b.amount); | return Amount(a * b.amount); | ||||
} | } | ||||
friend Amount operator*(const int a, const Amount b) { | friend constexpr Amount operator*(const int a, const Amount b) { | ||||
return Amount(a * b.amount); | return Amount(a * b.amount); | ||||
} | } | ||||
// DO NOT IMPLEMENT | // DO NOT IMPLEMENT | ||||
friend Amount operator*(const double a, const Amount b) = delete; | friend constexpr Amount operator*(const double a, const Amount b) = delete; | ||||
int64_t operator/(const Amount b) const { return amount / b.amount; } | constexpr int64_t operator/(const Amount b) const { | ||||
Amount operator/(const int64_t b) const { return Amount(amount / b); } | return amount / b.amount; | ||||
Amount operator/(const int b) const { return Amount(amount / b); } | } | ||||
constexpr Amount operator/(const int64_t b) const { | |||||
return Amount(amount / b); | |||||
} | |||||
constexpr Amount operator/(const int b) const { return Amount(amount / b); } | |||||
// DO NOT IMPLEMENT | // DO NOT IMPLEMENT | ||||
Amount operator/(const double b) const = delete; | constexpr Amount operator/(const double b) const = delete; | ||||
// ostream support | // ostream support | ||||
friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) { | friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) { | ||||
return stream << ca.amount; | return stream << ca.amount; | ||||
} | } | ||||
std::string ToString() const; | std::string ToString() const; | ||||
// serialization support | // serialization support | ||||
▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines |