Refactor the "scaleNumber" conversion functions

- Rename to "fromNumber".
- Instead of returning a pair or tuple, returns a constructed object
  (IOUAmount, STAmount).
This commit is contained in:
Ed Hennis
2025-12-01 19:59:19 -05:00
parent 748888533e
commit 639d6a953c
4 changed files with 22 additions and 33 deletions

View File

@@ -40,13 +40,8 @@ private:
void
normalize();
IOUAmount(std::pair<mantissa_type, exponent_type> parts)
: IOUAmount(parts.first, parts.second)
{
}
static std::pair<mantissa_type, exponent_type>
scaleNumber(Number const& number);
static IOUAmount
fromNumber(Number const& number);
public:
IOUAmount() = default;

View File

@@ -138,7 +138,7 @@ public:
template <AssetType A>
STAmount(A const& asset, Number const& number)
: STAmount(asset, scaleNumber(asset, number))
: STAmount(fromNumber(asset, number))
{
}
@@ -283,20 +283,8 @@ public:
private:
template <AssetType A>
STAmount(
A const& asset,
std::tuple<mantissa_type, exponent_type, bool> parts)
: STAmount(
asset,
std::get<mantissa_type>(parts),
std::get<exponent_type>(parts),
std::get<bool>(parts))
{
}
template <AssetType A>
static std::tuple<mantissa_type, exponent_type, bool>
scaleNumber(A const& asset, Number const& number);
static STAmount
fromNumber(A const& asset, Number const& number);
static std::unique_ptr<STAmount>
construct(SerialIter&, SField const& name);
@@ -568,28 +556,29 @@ STAmount::operator=(XRPAmount const& amount)
}
template <AssetType A>
inline std::tuple<STAmount::mantissa_type, STAmount::exponent_type, bool>
STAmount::scaleNumber(A const& asset, Number const& number)
inline STAmount
STAmount::fromNumber(A const& asset, Number const& number)
{
bool const negative = number.mantissa() < 0;
Number const working{negative ? -number : number};
if (asset.integral())
{
return std::make_tuple(std::int64_t(working), 0, negative);
std::uint64_t const intValue = static_cast<std::int64_t>(working);
return STAmount{asset, intValue, 0, negative};
}
else
{
auto const [mantissa, exponent] =
working.normalizeToRange(cMinValue, cMaxValue);
return std::make_tuple(mantissa, exponent, negative);
return STAmount{asset, mantissa, exponent, negative};
}
}
inline STAmount&
STAmount::operator=(Number const& number)
{
std::tie(mValue, mOffset, mIsNegative) = scaleNumber(mAsset, number);
*this = fromNumber(mAsset, number);
canonicalize();
return *this;
}