Skip to content

Commit

Permalink
Remove VT template parameter. Deduce value type with the ValueType he…
Browse files Browse the repository at this point in the history
…lper structure.
  • Loading branch information
gregtatcam committed Aug 5, 2024
1 parent cdd3251 commit 8725e2f
Showing 1 changed file with 82 additions and 73 deletions.
155 changes: 82 additions & 73 deletions include/xrpl/protocol/STObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ throwFieldNotFound(SField const& field)
class STObject : public STBase, public CountedObject<STObject>
{
// Proxy value for a STBase derived class
template <class T, SFieldMPT, class VT = T::value_type>
template <class T, SFieldMPT>
class Proxy;
template <class T, SFieldMPT, class VT = T::value_type>
template <class T, SFieldMPT>
class ValueProxy;
template <class T, SFieldMPT, class VT = T::value_type>
template <class T, SFieldMPT>
class OptionalProxy;

struct Transform
Expand Down Expand Up @@ -296,7 +296,7 @@ class STObject : public STBase, public CountedObject<STObject>

/** Overload for amount fields that don't support MPT
*/
ValueProxy<STEitherAmount, SFieldMPT::No, STAmount>
ValueProxy<STEitherAmount, SFieldMPT::No>
operator[](TypedField<STEitherAmount, SFieldMPT::No> const& f);

/** Return a modifiable field value as std::optional
Expand All @@ -314,7 +314,7 @@ class STObject : public STBase, public CountedObject<STObject>

/** Overload for amount fields that don't support MPT
*/
OptionalProxy<STEitherAmount, SFieldMPT::No, STAmount>
OptionalProxy<STEitherAmount, SFieldMPT::No>
operator[](OptionaledField<STEitherAmount, SFieldMPT::No> const& of);

/** Get the value of a field.
Expand Down Expand Up @@ -363,7 +363,7 @@ class STObject : public STBase, public CountedObject<STObject>

/** Overload for amount fields that don't support MPT
*/
ValueProxy<STEitherAmount, SFieldMPT::No, STAmount>
ValueProxy<STEitherAmount, SFieldMPT::No>
at(TypedField<STEitherAmount, SFieldMPT::No> const& f);

/** Return a modifiable field value as std::optional
Expand All @@ -381,7 +381,7 @@ class STObject : public STBase, public CountedObject<STObject>

/** Overload for amount fields that don't support MPT
*/
OptionalProxy<STEitherAmount, SFieldMPT::No, STAmount>
OptionalProxy<STEitherAmount, SFieldMPT::No>
at(OptionaledField<STEitherAmount, SFieldMPT::No> const& of);

/** Set a field.
Expand Down Expand Up @@ -527,11 +527,24 @@ class STObject : public STBase, public CountedObject<STObject>

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

template <class T, SFieldMPT M, class VT>
namespace detail {
template <typename T, SFieldMPT>
struct ValueType
{
using value_type = typename T::value_type;
};
template <>
struct ValueType<STEitherAmount, SFieldMPT::No>
{
using value_type = STAmount;
};
} // namespace detail

template <class T, SFieldMPT M>
class STObject::Proxy
{
protected:
using value_type = VT;
using value_type = detail::ValueType<T, M>::value_type;

STObject* st_;
SOEStyle style_;
Expand All @@ -552,11 +565,11 @@ class STObject::Proxy
assign(U&& u);
};

template <class T, SFieldMPT M, class VT>
class STObject::ValueProxy : private Proxy<T, M, VT>
template <class T, SFieldMPT M>
class STObject::ValueProxy : private Proxy<T, M>
{
private:
using value_type = VT;
using value_type = detail::ValueType<T, M>::value_type;

public:
ValueProxy(ValueProxy const&) = default;
Expand All @@ -575,11 +588,11 @@ class STObject::ValueProxy : private Proxy<T, M, VT>
ValueProxy(STObject* st, TypedField<T, M> const* f);
};

template <class T, SFieldMPT M, class VT>
class STObject::OptionalProxy : private Proxy<T, M, VT>
template <class T, SFieldMPT M>
class STObject::OptionalProxy : private Proxy<T, M>
{
private:
using value_type = VT;
using value_type = detail::ValueType<T, M>::value_type;

using optional_type = std::optional<typename std::decay<value_type>::type>;

Expand Down Expand Up @@ -711,8 +724,8 @@ class STObject::FieldErr : public std::runtime_error
using std::runtime_error::runtime_error;
};

template <class T, SFieldMPT M, class VT>
STObject::Proxy<T, M, VT>::Proxy(STObject* st, TypedField<T, M> const* f)
template <class T, SFieldMPT M>
STObject::Proxy<T, M>::Proxy(STObject* st, TypedField<T, M> const* f)
: st_(st), f_(f)
{
if (st_->mType)
Expand All @@ -729,9 +742,9 @@ STObject::Proxy<T, M, VT>::Proxy(STObject* st, TypedField<T, M> const* f)
}
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
auto
STObject::Proxy<T, M, VT>::value() const -> value_type
STObject::Proxy<T, M>::value() const -> value_type
{
auto const t = find();
if (t)
Expand All @@ -750,8 +763,7 @@ STObject::Proxy<T, M, VT>::value() const -> value_type

template <>
inline auto
STObject::Proxy<STEitherAmount, SFieldMPT::No, STAmount>::value() const
-> STAmount
STObject::Proxy<STEitherAmount, SFieldMPT::No>::value() const -> STAmount
{
auto const t = find();
if (t)
Expand All @@ -768,17 +780,17 @@ STObject::Proxy<STEitherAmount, SFieldMPT::No, STAmount>::value() const
return STAmount{};
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
inline T const*
STObject::Proxy<T, M, VT>::find() const
STObject::Proxy<T, M>::find() const
{
return dynamic_cast<T const*>(st_->peekAtPField(*f_));
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
template <class U>
void
STObject::Proxy<T, M, VT>::assign(U&& u)
STObject::Proxy<T, M>::assign(U&& u)
{
if (style_ == soeDEFAULT && u == value_type{})
{
Expand All @@ -796,71 +808,68 @@ STObject::Proxy<T, M, VT>::assign(U&& u)

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

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
template <class U>
std::enable_if_t<std::is_assignable_v<T, U>, STObject::ValueProxy<T, M, VT>&>
STObject::ValueProxy<T, M, VT>::operator=(U&& u)
std::enable_if_t<std::is_assignable_v<T, U>, STObject::ValueProxy<T, M>&>
STObject::ValueProxy<T, M>::operator=(U&& u)
{
this->assign(std::forward<U>(u));
return *this;
}

template <class T, SFieldMPT M, class VT>
STObject::ValueProxy<T, M, VT>::operator value_type() const
template <class T, SFieldMPT M>
STObject::ValueProxy<T, M>::operator value_type() const
{
return this->value();
}

template <class T, SFieldMPT M, class VT>
STObject::ValueProxy<T, M, VT>::ValueProxy(
STObject* st,
TypedField<T, M> const* f)
: Proxy<T, M, VT>(st, f)
template <class T, SFieldMPT M>
STObject::ValueProxy<T, M>::ValueProxy(STObject* st, TypedField<T, M> const* f)
: Proxy<T, M>(st, f)
{
}

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

template <class T, SFieldMPT M, class VT>
STObject::OptionalProxy<T, M, VT>::operator bool() const noexcept
template <class T, SFieldMPT M>
STObject::OptionalProxy<T, M>::operator bool() const noexcept
{
return engaged();
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
auto
STObject::OptionalProxy<T, M, VT>::operator*() const -> value_type
STObject::OptionalProxy<T, M>::operator*() const -> value_type
{
return this->value();
}

template <class T, SFieldMPT M, class VT>
STObject::OptionalProxy<T, M, VT>::operator typename STObject::
OptionalProxy<T, M, VT>::optional_type() const
template <class T, SFieldMPT M>
STObject::OptionalProxy<T, M>::operator typename STObject::OptionalProxy<T, M>::
optional_type() const
{
return optional_value();
}

template <class T, SFieldMPT M, class VT>
typename STObject::OptionalProxy<T, M, VT>::optional_type
STObject::OptionalProxy<T, M, VT>::operator~() const
template <class T, SFieldMPT M>
typename STObject::OptionalProxy<T, M>::optional_type
STObject::OptionalProxy<T, M>::operator~() const
{
return optional_value();
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
auto
STObject::OptionalProxy<T, M, VT>::operator=(std::nullopt_t const&)
STObject::OptionalProxy<T, M>::operator=(std::nullopt_t const&)
-> OptionalProxy&
{
disengage();
return *this;
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
auto
STObject::OptionalProxy<T, M, VT>::operator=(optional_type&& v)
-> OptionalProxy&
STObject::OptionalProxy<T, M>::operator=(optional_type&& v) -> OptionalProxy&
{
if (v)
this->assign(std::move(*v));
Expand All @@ -869,9 +878,9 @@ STObject::OptionalProxy<T, M, VT>::operator=(optional_type&& v)
return *this;
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
auto
STObject::OptionalProxy<T, M, VT>::operator=(optional_type const& v)
STObject::OptionalProxy<T, M>::operator=(optional_type const& v)
-> OptionalProxy&
{
if (v)
Expand All @@ -881,33 +890,33 @@ STObject::OptionalProxy<T, M, VT>::operator=(optional_type const& v)
return *this;
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
template <class U>
std::enable_if_t<std::is_assignable_v<T, U>, STObject::OptionalProxy<T, M, VT>&>
STObject::OptionalProxy<T, M, VT>::operator=(U&& u)
std::enable_if_t<std::is_assignable_v<T, U>, STObject::OptionalProxy<T, M>&>
STObject::OptionalProxy<T, M>::operator=(U&& u)
{
this->assign(std::forward<U>(u));
return *this;
}

template <class T, SFieldMPT M, class VT>
STObject::OptionalProxy<T, M, VT>::OptionalProxy(
template <class T, SFieldMPT M>
STObject::OptionalProxy<T, M>::OptionalProxy(
STObject* st,
TypedField<T, M> const* f)
: Proxy<T, M, VT>(st, f)
: Proxy<T, M>(st, f)
{
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
bool
STObject::OptionalProxy<T, M, VT>::engaged() const noexcept
STObject::OptionalProxy<T, M>::engaged() const noexcept
{
return this->style_ == soeDEFAULT || this->find() != nullptr;
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
void
STObject::OptionalProxy<T, M, VT>::disengage()
STObject::OptionalProxy<T, M>::disengage()
{
if (this->style_ == soeREQUIRED || this->style_ == soeDEFAULT)
Throw<STObject::FieldErr>(
Expand All @@ -918,18 +927,18 @@ STObject::OptionalProxy<T, M, VT>::disengage()
this->st_->makeFieldAbsent(*this->f_);
}

template <class T, SFieldMPT M, class VT>
template <class T, SFieldMPT M>
auto
STObject::OptionalProxy<T, M, VT>::optional_value() const -> optional_type
STObject::OptionalProxy<T, M>::optional_value() const -> optional_type
{
if (!engaged())
return std::nullopt;
return this->value();
}

template <class T, SFieldMPT M, class VT>
typename STObject::OptionalProxy<T, M, VT>::value_type
STObject::OptionalProxy<T, M, VT>::value_or(value_type val) const
template <class T, SFieldMPT M>
typename STObject::OptionalProxy<T, M>::value_type
STObject::OptionalProxy<T, M>::value_or(value_type val) const
{
return engaged() ? this->value() : val;
}
Expand Down Expand Up @@ -1070,7 +1079,7 @@ STObject::operator[](TypedField<T, M> const& f) -> ValueProxy<T, M>

inline auto
STObject::operator[](TypedField<STEitherAmount, SFieldMPT::No> const& f)
-> ValueProxy<STEitherAmount, SFieldMPT::No, STAmount>
-> ValueProxy<STEitherAmount, SFieldMPT::No>
{
return at(f);
}
Expand All @@ -1084,7 +1093,7 @@ STObject::operator[](OptionaledField<T, M> const& of) -> OptionalProxy<T, M>

inline auto
STObject::operator[](OptionaledField<STEitherAmount, SFieldMPT::No> const& of)
-> OptionalProxy<STEitherAmount, SFieldMPT::No, STAmount>
-> OptionalProxy<STEitherAmount, SFieldMPT::No>
{
return at(of);
}
Expand Down Expand Up @@ -1171,9 +1180,9 @@ STObject::at(TypedField<T, M> const& f) -> ValueProxy<T, M>

inline auto
STObject::at(TypedField<STEitherAmount, SFieldMPT::No> const& f)
-> ValueProxy<STEitherAmount, SFieldMPT::No, STAmount>
-> ValueProxy<STEitherAmount, SFieldMPT::No>
{
return ValueProxy<STEitherAmount, SFieldMPT::No, STAmount>(this, &f);
return ValueProxy<STEitherAmount, SFieldMPT::No>(this, &f);
}

template <class T, SFieldMPT M>
Expand All @@ -1185,9 +1194,9 @@ STObject::at(OptionaledField<T, M> const& of) -> OptionalProxy<T, M>

inline auto
STObject::at(OptionaledField<STEitherAmount, SFieldMPT::No> const& of)
-> OptionalProxy<STEitherAmount, SFieldMPT::No, STAmount>
-> OptionalProxy<STEitherAmount, SFieldMPT::No>
{
return OptionalProxy<STEitherAmount, SFieldMPT::No, STAmount>(this, of.f);
return OptionalProxy<STEitherAmount, SFieldMPT::No>(this, of.f);
}

template <class Tag>
Expand Down

0 comments on commit 8725e2f

Please sign in to comment.