diff --git a/include/xrpl/protocol/STObject.h b/include/xrpl/protocol/STObject.h index 2a08447a91b..9a8cbb7141e 100644 --- a/include/xrpl/protocol/STObject.h +++ b/include/xrpl/protocol/STObject.h @@ -55,11 +55,11 @@ throwFieldNotFound(SField const& field) class STObject : public STBase, public CountedObject { // Proxy value for a STBase derived class - template + template class Proxy; - template + template class ValueProxy; - template + template class OptionalProxy; struct Transform @@ -296,7 +296,7 @@ class STObject : public STBase, public CountedObject /** Overload for amount fields that don't support MPT */ - ValueProxy + ValueProxy operator[](TypedField const& f); /** Return a modifiable field value as std::optional @@ -314,7 +314,7 @@ class STObject : public STBase, public CountedObject /** Overload for amount fields that don't support MPT */ - OptionalProxy + OptionalProxy operator[](OptionaledField const& of); /** Get the value of a field. @@ -363,7 +363,7 @@ class STObject : public STBase, public CountedObject /** Overload for amount fields that don't support MPT */ - ValueProxy + ValueProxy at(TypedField const& f); /** Return a modifiable field value as std::optional @@ -381,7 +381,7 @@ class STObject : public STBase, public CountedObject /** Overload for amount fields that don't support MPT */ - OptionalProxy + OptionalProxy at(OptionaledField const& of); /** Set a field. @@ -527,11 +527,24 @@ class STObject : public STBase, public CountedObject //------------------------------------------------------------------------------ -template +namespace detail { +template +struct ValueType +{ + using value_type = typename T::value_type; +}; +template <> +struct ValueType +{ + using value_type = STAmount; +}; +} // namespace detail + +template class STObject::Proxy { protected: - using value_type = VT; + using value_type = detail::ValueType::value_type; STObject* st_; SOEStyle style_; @@ -552,11 +565,11 @@ class STObject::Proxy assign(U&& u); }; -template -class STObject::ValueProxy : private Proxy +template +class STObject::ValueProxy : private Proxy { private: - using value_type = VT; + using value_type = detail::ValueType::value_type; public: ValueProxy(ValueProxy const&) = default; @@ -575,11 +588,11 @@ class STObject::ValueProxy : private Proxy ValueProxy(STObject* st, TypedField const* f); }; -template -class STObject::OptionalProxy : private Proxy +template +class STObject::OptionalProxy : private Proxy { private: - using value_type = VT; + using value_type = detail::ValueType::value_type; using optional_type = std::optional::type>; @@ -711,8 +724,8 @@ class STObject::FieldErr : public std::runtime_error using std::runtime_error::runtime_error; }; -template -STObject::Proxy::Proxy(STObject* st, TypedField const* f) +template +STObject::Proxy::Proxy(STObject* st, TypedField const* f) : st_(st), f_(f) { if (st_->mType) @@ -729,9 +742,9 @@ STObject::Proxy::Proxy(STObject* st, TypedField const* f) } } -template +template auto -STObject::Proxy::value() const -> value_type +STObject::Proxy::value() const -> value_type { auto const t = find(); if (t) @@ -750,8 +763,7 @@ STObject::Proxy::value() const -> value_type template <> inline auto -STObject::Proxy::value() const - -> STAmount +STObject::Proxy::value() const -> STAmount { auto const t = find(); if (t) @@ -768,17 +780,17 @@ STObject::Proxy::value() const return STAmount{}; } -template +template inline T const* -STObject::Proxy::find() const +STObject::Proxy::find() const { return dynamic_cast(st_->peekAtPField(*f_)); } -template +template template void -STObject::Proxy::assign(U&& u) +STObject::Proxy::assign(U&& u) { if (style_ == soeDEFAULT && u == value_type{}) { @@ -796,71 +808,68 @@ STObject::Proxy::assign(U&& u) //------------------------------------------------------------------------------ -template +template template -std::enable_if_t, STObject::ValueProxy&> -STObject::ValueProxy::operator=(U&& u) +std::enable_if_t, STObject::ValueProxy&> +STObject::ValueProxy::operator=(U&& u) { this->assign(std::forward(u)); return *this; } -template -STObject::ValueProxy::operator value_type() const +template +STObject::ValueProxy::operator value_type() const { return this->value(); } -template -STObject::ValueProxy::ValueProxy( - STObject* st, - TypedField const* f) - : Proxy(st, f) +template +STObject::ValueProxy::ValueProxy(STObject* st, TypedField const* f) + : Proxy(st, f) { } //------------------------------------------------------------------------------ -template -STObject::OptionalProxy::operator bool() const noexcept +template +STObject::OptionalProxy::operator bool() const noexcept { return engaged(); } -template +template auto -STObject::OptionalProxy::operator*() const -> value_type +STObject::OptionalProxy::operator*() const -> value_type { return this->value(); } -template -STObject::OptionalProxy::operator typename STObject:: - OptionalProxy::optional_type() const +template +STObject::OptionalProxy::operator typename STObject::OptionalProxy:: + optional_type() const { return optional_value(); } -template -typename STObject::OptionalProxy::optional_type -STObject::OptionalProxy::operator~() const +template +typename STObject::OptionalProxy::optional_type +STObject::OptionalProxy::operator~() const { return optional_value(); } -template +template auto -STObject::OptionalProxy::operator=(std::nullopt_t const&) +STObject::OptionalProxy::operator=(std::nullopt_t const&) -> OptionalProxy& { disengage(); return *this; } -template +template auto -STObject::OptionalProxy::operator=(optional_type&& v) - -> OptionalProxy& +STObject::OptionalProxy::operator=(optional_type&& v) -> OptionalProxy& { if (v) this->assign(std::move(*v)); @@ -869,9 +878,9 @@ STObject::OptionalProxy::operator=(optional_type&& v) return *this; } -template +template auto -STObject::OptionalProxy::operator=(optional_type const& v) +STObject::OptionalProxy::operator=(optional_type const& v) -> OptionalProxy& { if (v) @@ -881,33 +890,33 @@ STObject::OptionalProxy::operator=(optional_type const& v) return *this; } -template +template template -std::enable_if_t, STObject::OptionalProxy&> -STObject::OptionalProxy::operator=(U&& u) +std::enable_if_t, STObject::OptionalProxy&> +STObject::OptionalProxy::operator=(U&& u) { this->assign(std::forward(u)); return *this; } -template -STObject::OptionalProxy::OptionalProxy( +template +STObject::OptionalProxy::OptionalProxy( STObject* st, TypedField const* f) - : Proxy(st, f) + : Proxy(st, f) { } -template +template bool -STObject::OptionalProxy::engaged() const noexcept +STObject::OptionalProxy::engaged() const noexcept { return this->style_ == soeDEFAULT || this->find() != nullptr; } -template +template void -STObject::OptionalProxy::disengage() +STObject::OptionalProxy::disengage() { if (this->style_ == soeREQUIRED || this->style_ == soeDEFAULT) Throw( @@ -918,18 +927,18 @@ STObject::OptionalProxy::disengage() this->st_->makeFieldAbsent(*this->f_); } -template +template auto -STObject::OptionalProxy::optional_value() const -> optional_type +STObject::OptionalProxy::optional_value() const -> optional_type { if (!engaged()) return std::nullopt; return this->value(); } -template -typename STObject::OptionalProxy::value_type -STObject::OptionalProxy::value_or(value_type val) const +template +typename STObject::OptionalProxy::value_type +STObject::OptionalProxy::value_or(value_type val) const { return engaged() ? this->value() : val; } @@ -1070,7 +1079,7 @@ STObject::operator[](TypedField const& f) -> ValueProxy inline auto STObject::operator[](TypedField const& f) - -> ValueProxy + -> ValueProxy { return at(f); } @@ -1084,7 +1093,7 @@ STObject::operator[](OptionaledField const& of) -> OptionalProxy inline auto STObject::operator[](OptionaledField const& of) - -> OptionalProxy + -> OptionalProxy { return at(of); } @@ -1171,9 +1180,9 @@ STObject::at(TypedField const& f) -> ValueProxy inline auto STObject::at(TypedField const& f) - -> ValueProxy + -> ValueProxy { - return ValueProxy(this, &f); + return ValueProxy(this, &f); } template @@ -1185,9 +1194,9 @@ STObject::at(OptionaledField const& of) -> OptionalProxy inline auto STObject::at(OptionaledField const& of) - -> OptionalProxy + -> OptionalProxy { - return OptionalProxy(this, of.f); + return OptionalProxy(this, of.f); } template