19#ifndef BASICS_FEES_H_INCLUDED
20#define BASICS_FEES_H_INCLUDED
22#include <xrpl/basics/safe_cast.h>
23#include <xrpl/beast/utility/Zero.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/json/json_value.h>
27#include <boost/multiprecision/cpp_int.hpp>
28#include <boost/operators.hpp>
65template <
class T,
class = enable_if_unit_t<T>>
72template <
class UnitTag,
class T>
73class TaggedFee :
private boost::totally_ordered<TaggedFee<UnitTag, T>>,
74 private boost::additive<TaggedFee<UnitTag, T>>,
75 private boost::equality_comparable<TaggedFee<UnitTag, T>, T>,
76 private boost::dividable<TaggedFee<UnitTag, T>, T>,
77 private boost::modable<TaggedFee<UnitTag, T>, T>,
78 private boost::unit_steppable<TaggedFee<UnitTag, T>>
88 template <
class Other>
93 template <
class OtherFee,
class = enable_if_unit_t<OtherFee>>
95 is_compatible_v<typename OtherFee::value_type> &&
98 template <
class Other>
102 template <
class OtherFee>
140 is_compatible_v<Other> &&
141 is_safetocasttovalue_v<value_type, Other>>>
208 template <
class transparent = value_type>
230 template <
class Other,
class = enable_if_compatible_t<Other>>
240 return fee_ == other;
243 template <
class Other,
class = enable_if_compatible_t<Other>>
258 operator bool() const noexcept
267 return (
fee_ < 0) ? -1 : (
fee_ ? 1 : 0);
277 template <
class Other>
281 return static_cast<double>(
fee_) / reference.
fee();
305 return static_cast<jsontype
>(
fee_);
332template <
class Char,
class Traits,
class UnitTag,
class T>
336 return os << q.value();
339template <
class UnitTag,
class T>
346template <
class Source,
class = enable_if_unit_t<Source>>
350template <
class Dest,
class = enable_if_unit_t<Dest>>
352 can_muldiv_source_v<Dest> &&
362 can_muldiv_source_v<Source1> && can_muldiv_source_v<Source2> &&
373 can_muldiv_sources_v<Source1, Source2> && can_muldiv_dest_v<Dest>;
393template <
class Source1,
class Source2>
397template <
class Source1,
class Source2,
class Dest>
401template <
class Source1,
class Source2,
class Dest>
416 class = enable_muldiv_t<Source1, Source2, Dest>>
421 if (value.value() < 0 || mul.value() < 0 || div.value() < 0)
427 "ripple::feeunit::mulDivU : minimum value input");
429 mul.value() >= 0,
"ripple::feeunit::mulDivU : minimum mul input");
431 div.value() >= 0,
"ripple::feeunit::mulDivU : minimum div input");
435 using desttype =
typename Dest::value_type;
441 if (mul.value() == div.value())
443 if (value.value() > max)
445 return Dest{
static_cast<desttype
>(value.value())};
448 using namespace boost::multiprecision;
456 auto quotient = product / div.value();
461 return Dest{
static_cast<desttype
>(quotient)};
477mulDiv(Source1 value, Dest mul, Source2 div)
486 class = feeunit::enable_muldiv_commute_t<Source1, Source2, Dest>>
488mulDiv(Dest value, Source1 mul, Source2 div)
494template <
class Dest,
class = feeunit::enable_muldiv_dest_t<Dest>>
503template <
class Dest,
class = feeunit::enable_muldiv_dest_t<Dest>>
508 return mulDiv(mul, value, div);
514 class = feeunit::enable_muldiv_sources_t<Source1, Source2>>
525 return unitresult->value();
531 class = feeunit::enable_muldiv_sources_t<Source1, Source2>>
536 return mulDiv(mul, value, div);
539template <
class Dest,
class Src>
548 return Dest{safe_cast<typename Dest::value_type>(s.value())};
551template <
class Dest,
class Src>
560 return Dest{unsafe_cast<typename Dest::value_type>(s.value())};
constexpr TaggedFee(TaggedFee< unit_type, Other > const &fee)
Instances with the same unit, and a type that is "safe" to convert to this one can be converted impli...
std::enable_if_t< is_usable_unit_v< TaggedFee >, Json::Value > jsonClipped() const
constexpr TaggedFee & operator=(TaggedFee const &other)=default
TaggedFee & operator=(value_type fee)
bool operator==(value_type other) const
bool operator==(TaggedFee< unit_type, Other > const &other) const
constexpr value_type operator/(TaggedFee const &rhs) const
friend constexpr TaggedFee operator*(value_type lhs, TaggedFee const &rhs)
static constexpr bool is_compatiblefee_v
constexpr value_type value() const
Returns the underlying value.
friend std::istream & operator>>(std::istream &s, TaggedFee &val)
constexpr int signum() const noexcept
Return the sign of the amount.
std::enable_if_t< std::is_integral_v< transparent >, TaggedFee & > operator%=(value_type const &rhs)
typename std::enable_if_t< is_compatible_v< Other > > enable_if_compatible_t
constexpr TaggedFee & operator=(beast::Zero)
constexpr TaggedFee(value_type fee)
typename std::enable_if_t< is_compatiblefee_v< OtherFee > > enable_if_compatiblefee_t
constexpr TaggedFee(beast::Zero)
TaggedFee operator-() const
constexpr TaggedFee(TaggedFee const &other)=default
TaggedFee & operator-=(TaggedFee const &other)
TaggedFee & operator*=(value_type const &rhs)
constexpr double decimalFromReference(TaggedFee< unit_type, Other > reference) const
bool operator<(TaggedFee const &other) const
static constexpr bool is_compatible_v
constexpr value_type fee() const
Returns the number of drops.
TaggedFee & operator/=(value_type const &rhs)
constexpr TaggedFee operator*(value_type const &rhs) const
TaggedFee & operator+=(TaggedFee const &other)
bool operator!=(TaggedFee< unit_type, Other > const &other) const
bool operator==(TaggedFee const &other) const
typename std::enable_if_t< can_muldiv_sources_v< Source1, Source2 > > enable_muldiv_sources_t
constexpr bool is_usable_unit_v
is_usable_unit_v is checked to ensure that only values with known valid type tags can be used (someti...
std::string to_string(TaggedFee< UnitTag, T > const &amount)
constexpr bool can_muldiv_dest_v
std::optional< Dest > mulDivU(Source1 value, Dest mul, Source2 div)
constexpr bool can_muldiv_sources_v
typename std::enable_if_t< std::is_class_v< T > &&std::is_object_v< typename T::unit_type > &&std::is_object_v< typename T::value_type > > enable_if_unit_t
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, TaggedFee< UnitTag, T > const &q)
constexpr bool can_muldiv_commute_v
typename std::enable_if_t< can_muldiv_v< Source1, Source2, Dest > > enable_muldiv_t
constexpr bool can_muldiv_v
typename std::enable_if_t< can_muldiv_source_v< T > > enable_muldiv_source_t
typename std::enable_if_t< can_muldiv_dest_v< T > > enable_muldiv_dest_t
typename std::enable_if_t< can_muldiv_commute_v< Source1, Source2, Dest > > enable_muldiv_commute_t
TaggedFee< unitlessTag, T > scalar(T value)
constexpr bool can_muldiv_source_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
STAmount multiply(STAmount const &amount, Rate const &rate)
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > unsafe_cast(Src s) noexcept
Zero allows classes to offer efficient comparisons to zero.