diff --git a/src/amount.h b/src/amount.h --- a/src/amount.h +++ b/src/amount.h @@ -11,13 +11,13 @@ #include #include #include -#include struct Amount { private: int64_t amount; public: + Amount() : amount(0) {} Amount(int _camount) : amount(_camount) {} Amount(int64_t _camount) : amount(_camount) {} Amount(const Amount &_camount) : amount(_camount.amount) {} @@ -63,11 +63,19 @@ return Amount(a.amount - b.amount); } // Implemented for allowing COIN as a base unit. - template ::value), - T>::type = 0> - friend Amount operator*(const T a, const Amount b) { + friend Amount operator*(const int64_t a, const Amount b) { return Amount(a * b.amount); } + friend Amount operator*(const int a, const Amount b) { + return Amount(a * b.amount); + } + // DO NOT IMPLEMENT + friend Amount operator*(const double a, const Amount b) = delete; + int64_t operator/(const Amount b) const { return amount / b.amount; } + Amount operator/(const int64_t b) const { return Amount(amount / b); } + Amount operator/(const int b) const { return Amount(amount / b); } + // DO NOT IMPLEMENT + Amount operator/(const double b) const = delete; // ostream support friend std::ostream &operator<<(std::ostream &stream, const Amount &ca) { diff --git a/src/test/amount_tests.cpp b/src/test/amount_tests.cpp --- a/src/test/amount_tests.cpp +++ b/src/test/amount_tests.cpp @@ -7,23 +7,6 @@ #include -// Snippit to allow compile time introspection for multiplication operator -// Used to ensure that certain operations are not implemented on Amount -namespace Check { -class No { -public: - bool b[2]; -}; -template No operator*(const T &, const Arg &); - -bool Check(...); -No &Check(const No &); - -template struct MultiplicationExists { - enum { value = (sizeof(Check(*(T *)(0) * *(Arg *)(0))) != sizeof(No)) }; -}; -} // namespace Check - BOOST_FIXTURE_TEST_SUITE(amount_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(CAmountTests) { @@ -64,12 +47,10 @@ BOOST_CHECK_EQUAL(10 * Amount(10), Amount(100)); BOOST_CHECK_EQUAL(-1 * Amount(1), Amount(-1)); - // The C preprocessor will now allow passing a template type into a macro - // directly, so we must define these aliases. - using impl_int = Check::MultiplicationExists; - using impl_float = Check::MultiplicationExists; - BOOST_CHECK(impl_int::value); - BOOST_CHECK(!impl_float::value); + BOOST_CHECK_EQUAL(Amount(10) / 3, Amount(3)); + BOOST_CHECK_EQUAL(10 * COIN / COIN, 10.0); + BOOST_CHECK_EQUAL(Amount(10) / -3, Amount(-3)); + BOOST_CHECK_EQUAL(-10 * COIN / (-1 * COIN), 10.0); BOOST_CHECK_EQUAL(Amount(100).GetSatoshis() / 10, 10); // This should probably be Banker's rounding,