Skip to content

Commit

Permalink
Split AMOUNT fields into two types SF_AMOUNT (don't support MPT)
Browse files Browse the repository at this point in the history
and SF_EITHER_AMOUNT (support MPT).
Extend STObject with getters/setters for SF_AMOUNT fields.
  • Loading branch information
gregtatcam committed Aug 1, 2024
1 parent c2a7281 commit 7d674a0
Show file tree
Hide file tree
Showing 67 changed files with 758 additions and 582 deletions.
38 changes: 26 additions & 12 deletions include/xrpl/protocol/SField.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class STCurrency;
STYPE(STI_UINT128, 4) \
STYPE(STI_UINT256, 5) \
STYPE(STI_AMOUNT, 6) \
STYPE(STI_EITHER_AMOUNT, 6) \
STYPE(STI_VL, 7) \
STYPE(STI_ACCOUNT, 8) \
\
Expand Down Expand Up @@ -301,8 +302,17 @@ class SField
static std::map<int, SField const*> knownCodeToField;
};

enum class SFieldMPT { None, Yes, No };

// clang-format off
template <typename T, SFieldMPT F>
concept ValidFields = (std::is_same_v<T, STEitherAmount> &&
(F == SFieldMPT::No || F == SFieldMPT::Yes)) ||
(!std::is_same_v<T, STEitherAmount> && F == SFieldMPT::None);

/** A field with a type known at compile time. */
template <class T>
template <class T, SFieldMPT M = SFieldMPT::None>
requires ValidFields<T, M>
struct TypedField : SField
{
using type = T;
Expand All @@ -312,22 +322,25 @@ struct TypedField : SField
};

/** Indicate std::optional field semantics. */
template <class T>
template <class T, SFieldMPT M = SFieldMPT::None>
requires ValidFields<T, M>
struct OptionaledField
{
TypedField<T> const* f;
TypedField<T, M> const* f;

explicit OptionaledField(TypedField<T> const& f_) : f(&f_)
explicit OptionaledField(TypedField<T, M> const& f_) : f(&f_)
{
}
};

template <class T>
inline OptionaledField<T>
operator~(TypedField<T> const& f)
template <class T, SFieldMPT M = SFieldMPT::None>
requires ValidFields<T, M>
inline OptionaledField<T, M>
operator~(TypedField<T, M> const& f)
{
return OptionaledField<T>(f);
return OptionaledField<T, M>(f);
}
// clang-format on

//------------------------------------------------------------------------------

Expand All @@ -346,7 +359,8 @@ using SF_UINT384 = TypedField<STBitString<384>>;
using SF_UINT512 = TypedField<STBitString<512>>;

using SF_ACCOUNT = TypedField<STAccount>;
using SF_AMOUNT = TypedField<STEitherAmount>;
using SF_AMOUNT = TypedField<STEitherAmount, SFieldMPT::No>;
using SF_EITHER_AMOUNT = TypedField<STEitherAmount, SFieldMPT::Yes>;
using SF_ISSUE = TypedField<STIssue>;
using SF_CURRENCY = TypedField<STCurrency>;
using SF_VL = TypedField<STBlob>;
Expand Down Expand Up @@ -520,16 +534,16 @@ extern SF_UINT256 const sfHookNamespace;
extern SF_UINT256 const sfHookSetTxnID;

// currency amount (common)
extern SF_AMOUNT const sfAmount;
extern SF_EITHER_AMOUNT const sfAmount;
extern SF_AMOUNT const sfBalance;
extern SF_AMOUNT const sfLimitAmount;
extern SF_AMOUNT const sfTakerPays;
extern SF_AMOUNT const sfTakerGets;
extern SF_AMOUNT const sfLowLimit;
extern SF_AMOUNT const sfHighLimit;
extern SF_AMOUNT const sfFee;
extern SF_AMOUNT const sfSendMax;
extern SF_AMOUNT const sfDeliverMin;
extern SF_EITHER_AMOUNT const sfSendMax;
extern SF_EITHER_AMOUNT const sfDeliverMin;
extern SF_AMOUNT const sfAmount2;
extern SF_AMOUNT const sfEPrice;
extern SF_AMOUNT const sfBidMin;
Expand Down
15 changes: 8 additions & 7 deletions include/xrpl/protocol/SOTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ enum SOEStyle {
// constructed with STObject::makeInnerObject()
};

enum SOESupportMPT : bool { soeMPTYes = true, soeMPTNo = false };
/** Amount fields that can support MPT */
enum SOETxMPTAmount { soeMPTNone, soeMPTSupported, soeMPTNotSupported };

//------------------------------------------------------------------------------

Expand All @@ -50,7 +51,7 @@ class SOElement
// Use std::reference_wrapper so SOElement can be stored in a std::vector.
std::reference_wrapper<SField const> sField_;
SOEStyle style_;
SOESupportMPT supportMpt_;
SOETxMPTAmount supportMpt_;

private:
void
Expand All @@ -70,14 +71,14 @@ class SOElement
SOElement(SField const& fieldName, SOEStyle style)
: sField_(fieldName)
, style_(style)
, supportMpt_(SOESupportMPT::soeMPTNo)
, supportMpt_(SOETxMPTAmount::soeMPTNone)
{
init(fieldName);
}
SOElement(
TypedField<STEitherAmount> const& fieldName,
TypedField<STEitherAmount, SFieldMPT::Yes> const& fieldName,
SOEStyle style,
SOESupportMPT supportMpt = SOESupportMPT::soeMPTNo)
SOETxMPTAmount supportMpt = SOETxMPTAmount::soeMPTNotSupported)
: sField_(fieldName), style_(style), supportMpt_(supportMpt)
{
init(fieldName);
Expand All @@ -95,10 +96,10 @@ class SOElement
return style_;
}

SOESupportMPT
bool
supportMPT() const
{
return supportMpt_;
return supportMpt_ == SOETxMPTAmount::soeMPTSupported;
}
};

Expand Down
15 changes: 5 additions & 10 deletions include/xrpl/protocol/STEitherAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
STEitherAmount(SerialIter& sit, SField const& name);
STEitherAmount(XRPAmount const& amount);
STEitherAmount(STAmount const& amount);
STEitherAmount(SField const& name, STAmount const& amount);
STEitherAmount(SField const& name, STAmount const& amount = STAmount{});
STEitherAmount(SField const& name, STMPTAmount const& amount);
STEitherAmount(STMPTAmount const& amount);

STEitherAmount&
Expand Down Expand Up @@ -102,15 +103,6 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
AccountID const&
getIssuer() const;

bool
badAsset() const;

bool
sameAsset(STEitherAmount const& amount) const;

bool
sameIssue(STEitherAmount const& amount) const;

bool
negative() const;

Expand Down Expand Up @@ -192,6 +184,9 @@ get(auto&& amount)
STEitherAmount
amountFromJson(SField const& name, Json::Value const& v);

STAmount
amountFromJson(SF_AMOUNT const& name, Json::Value const& v);

bool
amountFromJsonNoThrow(STEitherAmount& result, Json::Value const& jvSource);

Expand Down
89 changes: 81 additions & 8 deletions include/xrpl/protocol/STMPTAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,96 @@ class STMPTAmount final : public MPTAmount,
int
signum() const noexcept;

bool
operator==(STMPTAmount const& rhs) const;
STMPTAmount&
operator+=(STMPTAmount const& other);

bool
operator!=(STMPTAmount const& rhs) const;
STMPTAmount&
operator-=(STMPTAmount const& other);

STMPTAmount
operator-() const;

STMPTAmount& operator=(beast::Zero);
};

inline STMPTAmount
operator+(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
if (lhs.issue() != rhs.issue())
Throw<std::runtime_error>("Can't add amounts that aren't comparable!");
return {lhs.issue(), lhs.value() + rhs.value()};
}

inline STMPTAmount
operator-(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
return lhs + (-rhs);
}

inline STMPTAmount&
STMPTAmount::operator+=(const ripple::STMPTAmount& other)
{
*this = *this + other;
return *this;
}

inline STMPTAmount&
STMPTAmount::operator-=(const ripple::STMPTAmount& other)
{
*this = *this - other;
return *this;
}

inline STMPTAmount
STMPTAmount::operator-() const
{
return {issue_, -value_};
}

inline STMPTAmount&
STMPTAmount::operator=(beast::Zero)
{
clear();
return *this;
}

inline bool
operator==(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
return lhs.issue() == rhs.issue() && lhs.value() == rhs.value();
}

inline bool
operator<(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
if (lhs.issue() != rhs.issue())
Throw<std::runtime_error>(
"Can't compare amounts that are't comparable!");
return lhs.value() < rhs.value();
}

inline bool
operator!=(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
return !(lhs == rhs);
}

inline bool
operator>(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
return rhs < lhs;
}

inline bool
STMPTAmount::operator==(STMPTAmount const& rhs) const
operator<=(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
return value_ == rhs.value_ && issue_ == rhs.issue_;
return !(rhs < lhs);
}

inline bool
STMPTAmount::operator!=(STMPTAmount const& rhs) const
operator>=(STMPTAmount const& lhs, STMPTAmount const& rhs)
{
return !operator==(rhs);
return !(lhs < rhs);
}

STMPTAmount
Expand Down
Loading

0 comments on commit 7d674a0

Please sign in to comment.