Skip to content

Commit

Permalink
Add TypedFieldAmount to declare Amount fields.
Browse files Browse the repository at this point in the history
Remove VT template parameter. Deduce value type from ValueType alias.
  • Loading branch information
gregtatcam committed Aug 5, 2024
1 parent cdd3251 commit d9f77a3
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 189 deletions.
57 changes: 37 additions & 20 deletions include/xrpl/protocol/SField.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,8 @@ class SField

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, SFieldMPT M = SFieldMPT::None>
requires ValidFields<T, M>
template <class T>
struct TypedField : SField
{
using type = T;
Expand All @@ -322,25 +315,49 @@ struct TypedField : SField
};

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

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

template <class T>
inline OptionaledField<T>
operator~(TypedField<T> const& f)
{
return OptionaledField<T>(f);
}

// Amount fields

/** A field with a type known at compile time. */
template <SFieldMPT>
struct TypedFieldAmount : public TypedField<STEitherAmount>
{
template <class... Args>
explicit TypedFieldAmount(private_access_tag_t pat, Args&&... args);
};

explicit OptionaledField(TypedField<T, M> const& f_) : f(&f_)
/** Indicate std::optional field semantics. */
template <SFieldMPT M>
struct OptionaledFieldAmount : public OptionaledField<STEitherAmount>
{
explicit OptionaledFieldAmount(TypedFieldAmount<M> const& f_)
: OptionaledField<STEitherAmount>(f_)
{
}
};

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

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

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

using SF_ACCOUNT = TypedField<STAccount>;
using SF_AMOUNT = TypedField<STEitherAmount, SFieldMPT::No>;
using SF_EITHER_AMOUNT = TypedField<STEitherAmount, SFieldMPT::Yes>;
using SF_AMOUNT = TypedFieldAmount<SFieldMPT::No>;
using SF_EITHER_AMOUNT = TypedFieldAmount<SFieldMPT::Yes>;
using SF_ISSUE = TypedField<STIssue>;
using SF_CURRENCY = TypedField<STCurrency>;
using SF_VL = TypedField<STBlob>;
Expand Down
11 changes: 8 additions & 3 deletions include/xrpl/protocol/SOTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ class SOElement
{
init(fieldName);
}
SOElement(TypedFieldAmount<SFieldMPT::No> const& fieldName, SOEStyle style)
: sField_(fieldName), style_(style), supportMpt_(soeMPTNotSupported)
{
init(fieldName);
}
SOElement(
TypedField<STEitherAmount, SFieldMPT::Yes> const& fieldName,
TypedFieldAmount<SFieldMPT::Yes> const& fieldName,
SOEStyle style,
SOETxMPTAmount supportMpt = SOETxMPTAmount::soeMPTNotSupported)
SOETxMPTAmount supportMpt = soeMPTNotSupported)
: sField_(fieldName), style_(style), supportMpt_(supportMpt)
{
init(fieldName);
Expand All @@ -99,7 +104,7 @@ class SOElement
bool
supportMPT() const
{
return supportMpt_ == SOETxMPTAmount::soeMPTSupported;
return supportMpt_ == soeMPTSupported;
}
};

Expand Down
Loading

0 comments on commit d9f77a3

Please sign in to comment.