Skip to content

Commit

Permalink
Add += and -= operators to ValueProxy
Browse files Browse the repository at this point in the history
* Simplify updating some MPT fields in View
  • Loading branch information
gregtatcam committed Oct 15, 2024
1 parent af29f41 commit 9e3cfef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
39 changes: 39 additions & 0 deletions include/xrpl/protocol/STObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,17 @@ class STObject::Proxy
assign(U&& u);
};

// Constraint += and -= ValueProxy operators
// to value types that support arithmetic operations
template <typename TArg, typename TCls>
concept CanAddSubstr =
(std::is_arithmetic_v<TArg> &&
(std::is_same_v<
std::make_unsigned_t<TArg>,
std::make_unsigned_t<typename TCls::value_type>> ||
(std::is_same_v<TArg, STAmount> &&
std::is_same_v<typename TCls::value_type, STAmount>)));

template <class T>
class STObject::ValueProxy : private Proxy<T>
{
Expand All @@ -515,6 +526,16 @@ class STObject::ValueProxy : private Proxy<T>
std::enable_if_t<std::is_assignable_v<T, U>, ValueProxy&>
operator=(U&& u);

// Convenience operators for value types supporting
// arithmetic operations
template <class U>
requires CanAddSubstr<U, T> ValueProxy&
operator+=(U const& t);

template <class U>
requires CanAddSubstr<U, T> ValueProxy&
operator-=(U const& t);

operator value_type() const;

private:
Expand Down Expand Up @@ -732,6 +753,24 @@ STObject::ValueProxy<T>::operator=(U&& u)
return *this;
}

template <typename T>
template <class U>
requires CanAddSubstr<U, T> STObject::ValueProxy<T>&
STObject::ValueProxy<T>::operator+=(U const& t)
{
this->assign(this->value() + t);
return *this;
}

template <class T>
template <class U>
requires CanAddSubstr<U, T> STObject::ValueProxy<T>&
STObject::ValueProxy<T>::operator-=(U const& t)
{
this->assign(this->value() - t);
return *this;
}

template <class T>
STObject::ValueProxy<T>::operator value_type() const
{
Expand Down
12 changes: 4 additions & 8 deletions src/xrpld/ledger/detail/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ Rate
transferRate(ReadView const& view, MPTID const& issuanceID)
{
// fee is 0-50,000 (0-50%), rate is 1,000,000,000-2,000,000,000
// For example, if transfer fee is 50% then 10,000 * 50,000 = 500,000
// which represents 50% of 1,000,000,000
if (auto const sle = view.read(keylet::mptIssuance(issuanceID));
sle && sle->isFieldPresent(sfTransferFee))
return Rate{1'000'000'000u + 10'000 * sle->getFieldU16(sfTransferFee)};
Expand Down Expand Up @@ -1897,11 +1899,7 @@ rippleCreditMPT(
return tecOBJECT_NOT_FOUND;
if (uSenderID == issuer)
{
sleIssuance->setFieldU64(
sfOutstandingAmount,
sleIssuance->getFieldU64(sfOutstandingAmount) +
saAmount.mpt().value());

(*sleIssuance)[sfOutstandingAmount] += saAmount.mpt().value();
view.update(sleIssuance);
}
else
Expand Down Expand Up @@ -1937,9 +1935,7 @@ rippleCreditMPT(
auto const mptokenID = keylet::mptoken(mptID.key, uReceiverID);
if (auto sle = view.peek(mptokenID))
{
sle->setFieldU64(
sfMPTAmount,
sle->getFieldU64(sfMPTAmount) + saAmount.mpt().value());
(*sle)[sfMPTAmount] += saAmount.mpt().value();
view.update(sle);
}
else
Expand Down

0 comments on commit 9e3cfef

Please sign in to comment.