From 02fd3e2f7afd3f2cfc32fe281703053e9a59b6eb Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 27 May 2012 18:29:55 -0700 Subject: [PATCH] Signed get/set functions for native currency values. --- src/Amount.cpp | 28 +++++++++++++++++++++++++--- src/SerializedTypes.h | 4 +++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Amount.cpp b/src/Amount.cpp index 6c19711d3d..1af8c4f5c9 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -272,6 +272,28 @@ STAmount* STAmount::construct(SerializerIterator& sit, const char *name) return new STAmount(name, currency, value, offset, isNegative); } +int64 STAmount::getSNValue() const +{ // signed native value + if (!mIsNative) throw std::runtime_error("not native"); + if (mIsNegative) return -mValue; + return mValue; +} + +void STAmount::setSNValue(int64 v) +{ + if (!mIsNative) throw std::runtime_error("not native"); + if (v > 0) + { + mIsNegative = false; + mValue = v; + } + else + { + mIsNegative = true; + mValue = -v; + } +} + std::string STAmount::getRaw() const { // show raw internal form if (mValue == 0) return "0"; @@ -771,7 +793,7 @@ STAmount getNeeded(const STAmount& offerOut, const STAmount& offerIn, const STAm return (ret > offerIn) ? offerIn : ret; } -static uint64_t muldiv(uint64_t a, uint64_t b, uint64_t c) +static uint64 muldiv(uint64 a, uint64 b, uint64 c) { // computes (a*b)/c rounding up - supports values up to 10^18 if (c == 0) throw std::runtime_error("underflow"); if ((a == 0) || (b == 0)) return 0; @@ -784,12 +806,12 @@ static uint64_t muldiv(uint64_t a, uint64_t b, uint64_t c) return v.getulong(); } -uint64 convertToDisplayAmount(const STAmount& internalAmount, uint64_t totalNow, uint64_t totalInit) +uint64 convertToDisplayAmount(const STAmount& internalAmount, uint64 totalNow, uint64 totalInit) { // Convert an internal ledger/account quantity of native currency to a display amount return muldiv(internalAmount.getNValue(), totalInit, totalNow); } -STAmount convertToInternalAmount(uint64_t displayAmount, uint64_t totalNow, uint64_t totalInit, +STAmount convertToInternalAmount(uint64 displayAmount, uint64 totalNow, uint64 totalInit, const char *name) { // Convert a display/request currency amount to an internal amount return STAmount(name, muldiv(displayAmount, totalNow, totalInit)); diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 317ff0312d..faa717c48c 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -257,7 +257,9 @@ public: uint64 getMantissa() const { return mValue; } uint64 getNValue() const { if (!mIsNative) throw std::runtime_error("not native"); return mValue; } - void setNValue(uint64_t v) { if (!mIsNative) throw std::runtime_error("not native"); mValue = v; } + void setNValue(uint64 v) { if (!mIsNative) throw std::runtime_error("not native"); mValue = v; } + int64 getSNValue() const; + void setSNValue(int64); std::string getCurrencyHuman();