From 8ddaae27828b50087beb9c7112277983b757de4e Mon Sep 17 00:00:00 2001 From: gregtatcam Date: Wed, 4 Sep 2024 06:57:13 +0100 Subject: [PATCH] Update --- include/xrpl/protocol/STEitherAmount.h | 59 ++------- include/xrpl/protocol/STVariant.h | 164 ++++++++++++++---------- src/libxrpl/protocol/STEitherAmount.cpp | 73 +++-------- 3 files changed, 122 insertions(+), 174 deletions(-) diff --git a/include/xrpl/protocol/STEitherAmount.h b/include/xrpl/protocol/STEitherAmount.h index d4b5756451f..aa753a4b9dc 100644 --- a/include/xrpl/protocol/STEitherAmount.h +++ b/include/xrpl/protocol/STEitherAmount.h @@ -22,6 +22,7 @@ #include #include +#include namespace ripple { @@ -33,7 +34,7 @@ template concept EitherAmountType = std::is_same_v || std::is_same_v>; -class STEitherAmount : public STBase, public CountedObject +class STEitherAmount : public STVariant { private: std::variant amount_; @@ -55,29 +56,12 @@ class STEitherAmount : public STBase, public CountedObject STEitherAmount& operator=(XRPAmount const&); + // STBase SerializedTypeID getSType() const override; - std::string - getFullText() const override; - - std::string - getText() const override; - - Json::Value getJson(JsonOptions) const override; - - void - setJson(Json::Value&) const; - - void - add(Serializer& s) const override; - - bool - isEquivalent(const STBase& t) const override; - bool - isDefault() const override; - + isEquivalent(STBase const&) const override; //------------------------------------------------------------------------------ bool @@ -89,12 +73,6 @@ class STEitherAmount : public STBase, public CountedObject STEitherAmount const& value() const; - std::variant const& - getValue() const; - - std::variant& - getValue(); - AccountID getIssuer() const; @@ -110,13 +88,10 @@ class STEitherAmount : public STBase, public CountedObject int signum() const noexcept; - template - T const& - get() const; - - template - T& - get(); +protected: + // VariantBase + void + decode(SerialIter& sit) override; private: STBase* @@ -125,24 +100,6 @@ class STEitherAmount : public STBase, public CountedObject move(std::size_t n, void* buf) override; }; -template -T const& -STEitherAmount::get() const -{ - if (std::holds_alternative(amount_)) - return std::get(amount_); - Throw("Invalid STEitherAmount conversion"); -} - -template -T& -STEitherAmount::get() -{ - if (std::holds_alternative(amount_)) - return std::get(amount_); - Throw("Invalid STEitherAmount conversion"); -} - template decltype(auto) get(auto&& amount) diff --git a/include/xrpl/protocol/STVariant.h b/include/xrpl/protocol/STVariant.h index 0dd41ec486a..6621e844752 100644 --- a/include/xrpl/protocol/STVariant.h +++ b/include/xrpl/protocol/STVariant.h @@ -30,18 +30,38 @@ namespace ripple { namespace detail { -template -constexpr bool contains() { return std::disjunction_v...>; } +template +constexpr bool +contains() +{ + return std::disjunction_v...>; +} template -concept ValidAlts = contains(); +concept ValidAlts = requires() { contains(); }; -} // namespace detail +} // namespace detail + +class VariantBase +{ +protected: + VariantBase() = default; + ~VariantBase() = default; + VariantBase(VariantBase const&) = default; + VariantBase& + operator=(VariantBase const&) = default; + virtual void + decode(SerialIter& sit) = 0; +}; + +template +class STVariant : public STBase, + public CountedObject>, + public VariantBase +{ +protected: + std::variant alternatives_; -template -class STVariant : public STBase, CountedObject> { - std::variant alternatives_; - SerializedTypeID const sid_; public: template STVariant(SField const& name, T const& arg); @@ -55,129 +75,141 @@ class STVariant : public STBase, CountedObject> { template T& - get() const; + get(); // STBase - SerializedTypeID - getSType() const override; std::string getText() const override; + std::string + getFullText() const override; + Json::Value getJson(JsonOptions) const override; void add(Serializer& s) const override; - bool - isEquivalent(const STBase& t) const override; - bool isDefault() const override; -private: - static std::unique_ptr - construct(SerialIter&, SField const& name); + //--------------------------------- + + void + setJson(Json::Value&) const; - STBase* - copy(std::size_t n, void* buf) const override; - STBase* - move(std::size_t n, void* buf) override; + auto const& + getValue() const; + auto& + getValue(); + +private: friend class detail::STVar; }; -template +template template -STVariant::STVariant(SField const& name, T const& arg) -: STBase(name) -, alternatives_(arg) +STVariant::STVariant(SField const& name, T const& arg) + : STBase(name), alternatives_(arg) { } -template -STVariant::STVariant(SField const& name) -: STBase(name) +template +STVariant::STVariant(SField const& name) : STBase(name) { } -template -STVariant::STVariant(SerialIter& sit, SField const& name) -: STBase(name) +template +STVariant::STVariant(SerialIter& sit, SField const& name) + : STBase(name) { - std::visit([&](A&& a) { - alternatives_ = - }, alternatives_); + decode(sit); } -template +template template T const& -STVariant::get() const +STVariant::get() const { + if (!std::holds_alternative(alternatives_)) + Throw("The variant doesn't hold alternative"); + return std::get(alternatives_); } -template +template template T& -STVariant::get() const +STVariant::get() { + if (!std::holds_alternative(alternatives_)) + Throw("The variant doesn't hold alternative"); + return std::get(alternatives_); } // STBase -template -SerializedTypeID -STVariant::getSType() const -{ -} -template +template std::string -STVariant::getText() const +STVariant::getText() const { + return std::visit([&](auto&& alt) { return alt.getText(); }, alternatives_); } -template -Json::Value STVariant::getJson(JsonOptions) const +template +std::string +STVariant::getFullText() const { + return std::visit( + [&](auto&& alt) { return alt.getFullText(); }, alternatives_); } -template -void -STVariant::add(Serializer& s) const +template +Json::Value +STVariant::getJson(JsonOptions) const { + return std::visit( + [&](auto&& alt) { return alt.getJson(JsonOptions::none); }, + alternatives_); } -template -bool -STVariant::isEquivalent(const STBase& t) const +template +void +STVariant::add(Serializer& s) const { + std::visit([&](auto&& alt) { alt.add(s); }, alternatives_); } -template +template bool -STVariant::isDefault() const +STVariant::isDefault() const { + return std::visit( + [&](auto&& alt) { return alt.isDefault(); }, alternatives_); } -template -std::unique_ptr -STVariant::construct(SerialIter&, SField const& name) +template +void +STVariant::setJson(Json::Value& jv) const { + return std::visit( + [&](auto&& alt) { return alt.setJson(jv); }, alternatives_); } -template -STBase* -STVariant::copy(std::size_t n, void* buf) const +template +auto const& +STVariant::getValue() const { + return alternatives_; } -template -STBase* -STVariant::move(std::size_t n, void* buf) +template +auto& +STVariant::getValue() { + return alternatives_; } -} // namespace ripple +} // namespace ripple -#endif // RIPPLE_PROTOCOL_STVARIANT_H_INCLUDED +#endif // RIPPLE_PROTOCOL_STVARIANT_H_INCLUDED diff --git a/src/libxrpl/protocol/STEitherAmount.cpp b/src/libxrpl/protocol/STEitherAmount.cpp index 2fc4d3f00de..082d55a39c5 100644 --- a/src/libxrpl/protocol/STEitherAmount.cpp +++ b/src/libxrpl/protocol/STEitherAmount.cpp @@ -28,17 +28,12 @@ namespace ripple { STEitherAmount::STEitherAmount(SerialIter& sit, SField const& name) - : STBase(name) + : STVariant(name) { - auto const u8 = sit.peek8(); - if (((static_cast(u8) << 56) & STAmount::cNotNative) == 0 && - (u8 & STMPTAmount::cMPToken) != 0) - amount_.emplace(sit); - else - amount_.emplace(sit); } -STEitherAmount::STEitherAmount(XRPAmount const& amount) : amount_{amount} +STEitherAmount::STEitherAmount(XRPAmount const& amount) + : STVariant(sfGeneric), amount_{amount} { } @@ -47,12 +42,12 @@ STEitherAmount::STEitherAmount(STAmount const& amount) : amount_{amount} } STEitherAmount::STEitherAmount(SField const& name, STAmount const& amount) - : STBase(name), amount_{amount} + : amount_{amount} { } STEitherAmount::STEitherAmount(SField const& name, STMPTAmount const& amount) - : STBase(name), amount_{amount} + : STVariant(name), amount_{amount} { } @@ -60,6 +55,17 @@ STEitherAmount::STEitherAmount(STMPTAmount const& amount) : amount_{amount} { } +void +STEitherAmount::decode(SerialIter& sit) +{ + auto const u8 = sit.peek8(); + if (((static_cast(u8) << 56) & STAmount::cNotNative) == 0 && + (u8 & STMPTAmount::cMPToken) != 0) + amount_.emplace(sit); + else + amount_.emplace(sit); +} + STEitherAmount& STEitherAmount::operator=(STAmount const& amount) { @@ -87,36 +93,6 @@ STEitherAmount::getSType() const return STI_AMOUNT; } -std::string -STEitherAmount::getFullText() const -{ - return std::visit([&](auto&& a) { return a.getFullText(); }, amount_); -} - -std::string -STEitherAmount::getText() const -{ - return std::visit([&](auto&& a) { return a.getText(); }, amount_); -} - -Json::Value STEitherAmount::getJson(JsonOptions) const -{ - return std::visit( - [&](auto&& a) { return a.getJson(JsonOptions::none); }, amount_); -} - -void -STEitherAmount::setJson(Json::Value& jv) const -{ - std::visit([&](auto&& a) { a.setJson(jv); }, amount_); -} - -void -STEitherAmount::add(Serializer& s) const -{ - std::visit([&](auto&& a) { a.add(s); }, amount_); -} - bool STEitherAmount::isEquivalent(const STBase& t) const { @@ -124,11 +100,6 @@ STEitherAmount::isEquivalent(const STBase& t) const return v && *this == *v; } -bool -STEitherAmount::isDefault() const -{ - return std::visit([&](auto&& a) { return a.isDefault(); }, amount_); -} //------------------------------------------------------------------------------ bool STEitherAmount::isMPT() const @@ -171,18 +142,6 @@ STEitherAmount::value() const return *this; } -std::variant const& -STEitherAmount::getValue() const -{ - return amount_; -} - -std::variant& -STEitherAmount::getValue() -{ - return amount_; -} - AccountID STEitherAmount::getIssuer() const {