3#include <xrpl/basics/safe_cast.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/json/json_forwards.h>
7#include <xrpl/json/json_value.h>
9#include <boost/multiprecision/cpp_int.hpp>
10#include <boost/operators.hpp>
69template <
class Other,
class VU>
80template <
class VU1,
class VU2>
84template <
class UnitTag,
class T>
85class ValueUnit :
private boost::totally_ordered<ValueUnit<UnitTag, T>>,
86 private boost::additive<ValueUnit<UnitTag, T>>,
87 private boost::equality_comparable<ValueUnit<UnitTag, T>, T>,
88 private boost::dividable<ValueUnit<UnitTag, T>, T>,
89 private boost::modable<ValueUnit<UnitTag, T>, T>,
90 private boost::unit_steppable<ValueUnit<UnitTag, T>>
132 template <Compatible<ValueUnit> Other>
227 template <Integral Transparent = value_type>
248 template <Compatible<ValueUnit> Other>
271 operator bool() const noexcept
279 [[nodiscard]]
constexpr int
297 template <
class Other>
298 [[nodiscard]]
constexpr double
301 return static_cast<double>(
value_) / reference.
value();
324 return static_cast<jsontype
>(
value_);
352template <
class Char,
class Traits,
class UnitTag,
class T>
356 return os << q.value();
359template <
class UnitTag,
class T>
366template <
class Source>
375template <
class Source2,
class Source1>
379template <
class Dest,
class Source1,
class Source2>
383template <
class Dest,
class Source1,
class Source2>
394template <
class Source1,
class Source2, unit::muldivable<Source1, Source2> Dest>
399 if (value.value() < 0 || mul.value() < 0 || div.value() < 0)
403 XRPL_ASSERT(value.value() >= 0,
"xrpl::unit::mulDivU : minimum value input");
404 XRPL_ASSERT(mul.value() >= 0,
"xrpl::unit::mulDivU : minimum mul input");
405 XRPL_ASSERT(div.value() > 0,
"xrpl::unit::mulDivU : minimum div input");
409 using desttype = Dest::value_type;
415 if (mul.value() == div.value())
417 if (value.value() > kMax)
419 return Dest{
static_cast<desttype
>(value.value())};
422 using namespace boost::multiprecision;
430 auto quotient = product / div.value();
435 return Dest{
static_cast<desttype
>(quotient)};
456template <
class Source1,
class Source2, unit::muldivable<Source1, Source2> Dest>
458mulDiv(Source1 value, Dest mul, Source2 div)
463template <
class Source1,
class Source2, unit::muldivCommutable<Source1, Source2> Dest>
465mulDiv(Dest value, Source1 mul, Source2 div)
471template <unit::muldivDest Dest>
480template <unit::muldivDest Dest>
485 return mulDiv(mul, value, div);
488template <unit::muldivSource Source1, unit::muldivSources<Source1> Source2>
499 return unitresult->value();
502template <unit::muldivSource Source1, unit::muldivSources<Source1> Source2>
507 return mulDiv(mul, value, div);
510template <unit::IntegralValue Dest, unit::CastableValue<Dest> Src>
518template <unit::IntegralValue Dest, unit::Integral Src>
526template <unit::IntegralValue Dest, unit::CastableValue<Dest> Src>
534template <unit::IntegralValue Dest, unit::Integral Src>
constexpr ValueUnit(value_type value)
constexpr int signum() const noexcept
friend constexpr ValueUnit operator*(value_type lhs, ValueUnit const &rhs)
constexpr value_type value() const
constexpr bool operator<(ValueUnit const &other) const
friend std::istream & operator>>(std::istream &s, ValueUnit &val)
ValueUnit & operator+=(ValueUnit const &other)
constexpr bool operator==(value_type other) const
constexpr bool operator==(ValueUnit const &other) const
constexpr bool operator==(ValueUnit< unit_type, Other > const &other) const
constexpr ValueUnit & operator=(value_type value)
constexpr ValueUnit(ValueUnit const &other)=default
json::Value jsonClipped() const
constexpr ValueUnit & operator=(beast::Zero)
ValueUnit & operator/=(value_type const &rhs)
constexpr ValueUnit operator*(value_type const &rhs) const
constexpr ValueUnit operator-(value_type const &rhs) const
ValueUnit & operator%=(value_type const &rhs)
constexpr ValueUnit & operator=(ValueUnit const &other)=default
ValueUnit operator-() const
constexpr ValueUnit(ValueUnit< unit_type, Other > const &value)
constexpr ValueUnit(beast::Zero)
constexpr value_type fee() const
friend constexpr ValueUnit operator-(value_type lhs, ValueUnit const &rhs)
ValueUnit & operator*=(value_type const &rhs)
constexpr value_type operator/(ValueUnit const &rhs) const
ValueUnit & operator-=(ValueUnit const &other)
constexpr ValueUnit operator+(value_type const &rhs) const
friend constexpr ValueUnit operator+(value_type lhs, ValueUnit const &rhs)
constexpr double decimalFromReference(ValueUnit< unit_type, Other > reference) const
Usable is checked to ensure that only values with known valid type tags can be used (sometimes transp...
std::optional< Dest > mulDivU(Source1 value, Dest mul, Source2 div)
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, ValueUnit< UnitTag, T > const &q)
std::string to_string(ValueUnit< UnitTag, T > const &amount)
ValueUnit< unitlessTag, T > scalar(T value)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
Bips< std::uint32_t > Bips32
TenthBips< std::uint32_t > TenthBips32
TenthBips< std::uint16_t > TenthBips16
constexpr Dest safeCast(Src s) noexcept
constexpr Dest unsafeCast(Src s) noexcept
FeeLevel< std::uint64_t > FeeLevel64
unit::ValueUnit< unit::BipsTag, T > Bips
FeeLevel< double > FeeLevelDouble
unit::ValueUnit< unit::feelevelTag, T > FeeLevel
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
unit::ValueUnit< unit::TenthBipsTag, T > TenthBips
Bips< std::uint16_t > Bips16
Zero allows classes to offer efficient comparisons to zero.