Support STAmount conversions to XRPAmount and IOUAmount

This commit is contained in:
Nik Bougalis
2015-08-21 10:05:18 -07:00
parent 1e9624270d
commit 94af42da44
4 changed files with 164 additions and 16 deletions

View File

@@ -248,12 +248,72 @@ STAmount::STAmount (Issue const& issue,
{
}
// Legacy support for new-style amounts
STAmount::STAmount (IOUAmount const& amount, Issue const& issue)
: mIssue (issue)
, mOffset (amount.exponent ())
, mIsNative (false)
, mIsNegative (amount < zero)
{
if (mIsNegative)
mValue = static_cast<std::uint64_t> (-amount.mantissa ());
else
mValue = static_cast<std::uint64_t> (amount.mantissa ());
canonicalize ();
}
STAmount::STAmount (XRPAmount const& amount)
: mOffset (0)
, mIsNative (true)
, mIsNegative (amount < zero)
{
if (mIsNegative)
mValue = static_cast<std::uint64_t> (-amount.drops ());
else
mValue = static_cast<std::uint64_t> (amount.drops ());
canonicalize ();
}
std::unique_ptr<STAmount>
STAmount::construct (SerialIter& sit, SField const& name)
{
return std::make_unique<STAmount>(sit, name);
}
//------------------------------------------------------------------------------
//
// Conversion
//
//------------------------------------------------------------------------------
XRPAmount STAmount::xrp () const
{
if (!mIsNative)
throw std::logic_error ("Cannot return non-native STAmount as XRPAmount");
auto drops = static_cast<std::int64_t> (mValue);
if (mIsNegative)
drops = -drops;
return { drops };
}
IOUAmount STAmount::iou () const
{
if (mIsNative)
throw std::logic_error ("Cannot return native STAmount as IOUAmount");
auto mantissa = static_cast<std::int64_t> (mValue);
auto exponent = mOffset;
if (mIsNegative)
mantissa = -mantissa;
return { mantissa, exponent };
}
//------------------------------------------------------------------------------
//
// Operators