mirror of
https://github.com/XRPLF/rippled.git
synced 2026-09-28 07:48:01 +00:00
127 lines
3.7 KiB
C++
127 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include <xrpl/basics/CountedObject.h>
|
|
#include <xrpl/basics/Number.h>
|
|
#include <xrpl/json/json_value.h>
|
|
#include <xrpl/protocol/Asset.h>
|
|
#include <xrpl/protocol/SField.h>
|
|
#include <xrpl/protocol/STBase.h>
|
|
#include <xrpl/protocol/STTakesAsset.h>
|
|
#include <xrpl/protocol/Serializer.h>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <ostream>
|
|
#include <string>
|
|
|
|
namespace xrpl {
|
|
|
|
/**
|
|
* A serializable number.
|
|
*
|
|
* This type is-a `Number`, and can be used everywhere that is accepted.
|
|
* This type simply integrates `Number` with the serialization framework,
|
|
* letting it be used for fields in ledger entries and transactions.
|
|
* It is effectively an `STAmount` sans `Asset`:
|
|
* it can represent a value of any token type (XRP, IOU, or MPT)
|
|
* without paying the storage cost of duplicating asset information
|
|
* that may be deduced from the context.
|
|
*
|
|
* STNumber derives from STTakesAsset, so that it can be associated with the
|
|
* related Asset during transaction processing. Which asset is relevant depends
|
|
* on the object and transaction. As of this writing, only Vault, LoanBroker,
|
|
* and Loan objects use STNumber fields. All of those fields represent amounts
|
|
* of the Vault's Asset, so they should be associated with the Vault's Asset.
|
|
*
|
|
* e.g.
|
|
* associateAsset(*loanSle, asset);
|
|
* associateAsset(*brokerSle, asset);
|
|
* associateAsset(*vaultSle, asset);
|
|
*/
|
|
class STNumber : public STTakesAsset, public CountedObject<STNumber>
|
|
{
|
|
private:
|
|
Number value_;
|
|
|
|
public:
|
|
using value_type = Number;
|
|
|
|
STNumber() = default;
|
|
explicit STNumber(SField const& field, Number const& value = Number());
|
|
STNumber(SerialIter& sit, SField const& field);
|
|
|
|
[[nodiscard]] SerializedTypeID
|
|
getSType() const override;
|
|
[[nodiscard]] std::string
|
|
getText() const override;
|
|
void
|
|
add(Serializer& s) const override;
|
|
|
|
[[nodiscard]] Number const&
|
|
value() const;
|
|
void
|
|
setValue(Number const& v);
|
|
|
|
STNumber&
|
|
operator=(Number const& rhs)
|
|
{
|
|
setValue(rhs);
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] bool
|
|
isEquivalent(STBase const& t) const override;
|
|
[[nodiscard]] bool
|
|
isDefault() const override;
|
|
|
|
void
|
|
associateAsset(Asset const& a) override;
|
|
|
|
// Reconstruct via the (mantissa, exponent) ctor so the returned
|
|
// Number is normalized to the *current* mantissa scale, not the
|
|
// scale that was active when value_ was last set.
|
|
//
|
|
// STNumber instances are held across transaction boundaries (in
|
|
// SLE-cache memory), and the mantissa scale flips per-tx based on
|
|
// featureSingleAssetVault / featureLendingProtocol (see
|
|
// setCurrentTransactionRules in Rules.cpp). A Number normalized at
|
|
// Large scale outside any tx context will fail isnormal() inside a
|
|
// Small-scale tx, tripping the assert in operator+=. mantissa() /
|
|
// exponent() return the canonical (scale-independent) external
|
|
// view; the Normalized{} ctor re-normalizes to current scale.
|
|
//
|
|
// This costs one extra normalize() per implicit conversion to
|
|
// Number — single-digit ns, well below the cost of any caller's
|
|
// arithmetic.
|
|
operator Number() const
|
|
{
|
|
if (value_ == Number{})
|
|
return value_;
|
|
return Number{value_.mantissa(), value_.exponent()};
|
|
}
|
|
|
|
private:
|
|
STBase*
|
|
copy(std::size_t n, void* buf) const override;
|
|
STBase*
|
|
move(std::size_t n, void* buf) override;
|
|
};
|
|
|
|
std::ostream&
|
|
operator<<(std::ostream& out, STNumber const& rhs);
|
|
|
|
struct NumberParts
|
|
{
|
|
std::uint64_t mantissa = 0;
|
|
int exponent = 0;
|
|
bool negative = false;
|
|
};
|
|
|
|
NumberParts
|
|
partsFromString(std::string const& number);
|
|
|
|
STNumber
|
|
numberFromJson(SField const& field, json::Value const& value);
|
|
|
|
} // namespace xrpl
|