Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Sep 4, 2024
1 parent a4eda58 commit 8ddaae2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 174 deletions.
59 changes: 8 additions & 51 deletions include/xrpl/protocol/STEitherAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/STMPTAmount.h>
#include <xrpl/protocol/STVariant.h>

namespace ripple {

Expand All @@ -33,7 +34,7 @@ template <typename T>
concept EitherAmountType = std::is_same_v<T, STEitherAmount> ||
std::is_same_v<T, std::optional<STEitherAmount>>;

class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
class STEitherAmount : public STVariant<STAmount, STMPTAmount>
{
private:
std::variant<STAmount, STMPTAmount> amount_;
Expand All @@ -55,29 +56,12 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
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
Expand All @@ -89,12 +73,6 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
STEitherAmount const&
value() const;

std::variant<STAmount, STMPTAmount> const&
getValue() const;

std::variant<STAmount, STMPTAmount>&
getValue();

AccountID
getIssuer() const;

Expand All @@ -110,13 +88,10 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
int
signum() const noexcept;

template <ValidAmountType T>
T const&
get() const;

template <ValidAmountType T>
T&
get();
protected:
// VariantBase
void
decode(SerialIter& sit) override;

private:
STBase*
Expand All @@ -125,24 +100,6 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
move(std::size_t n, void* buf) override;
};

template <ValidAmountType T>
T const&
STEitherAmount::get() const
{
if (std::holds_alternative<T>(amount_))
return std::get<T>(amount_);
Throw<std::logic_error>("Invalid STEitherAmount conversion");
}

template <ValidAmountType T>
T&
STEitherAmount::get()
{
if (std::holds_alternative<T>(amount_))
return std::get<T>(amount_);
Throw<std::logic_error>("Invalid STEitherAmount conversion");
}

template <ValidAmountType T>
decltype(auto)
get(auto&& amount)
Expand Down
164 changes: 98 additions & 66 deletions include/xrpl/protocol/STVariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,38 @@
namespace ripple {
namespace detail {

template<typename T, typename... Ts>
constexpr bool contains() { return std::disjunction_v<std::is_same<T, Ts>...>; }
template <typename T, typename... Ts>
constexpr bool
contains()
{
return std::disjunction_v<std::is_same<T, Ts>...>;
}

template <typename T, typename... Ts>
concept ValidAlts = contains<T, Ts ...>();
concept ValidAlts = requires() { contains<T, Ts...>(); };

} // 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 <typename... Alts>
class STVariant : public STBase,
public CountedObject<STVariant<Alts...>>,
public VariantBase
{
protected:
std::variant<Alts...> alternatives_;

template <typename ... Alts>
class STVariant : public STBase, CountedObject<STVariant<Alts ...>> {
std::variant<Alts ...> alternatives_;
SerializedTypeID const sid_;
public:
template <detail::ValidAlts T>
STVariant(SField const& name, T const& arg);
Expand All @@ -55,129 +75,141 @@ class STVariant : public STBase, CountedObject<STVariant<Alts ...>> {

template <detail::ValidAlts T>
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<STCurrency>
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 <typename ... Alts>
template <typename... Alts>
template <detail::ValidAlts T>
STVariant<Alts ...>::STVariant(SField const& name, T const& arg)
: STBase(name)
, alternatives_(arg)
STVariant<Alts...>::STVariant(SField const& name, T const& arg)
: STBase(name), alternatives_(arg)
{
}

template <typename ... Alts>
STVariant<Alts ...>::STVariant(SField const& name)
: STBase(name)
template <typename... Alts>
STVariant<Alts...>::STVariant(SField const& name) : STBase(name)
{
}

template <typename ... Alts>
STVariant<Alts ...>::STVariant(SerialIter& sit, SField const& name)
: STBase(name)
template <typename... Alts>
STVariant<Alts...>::STVariant(SerialIter& sit, SField const& name)
: STBase(name)
{
std::visit([&]<typename A>(A&& a) {
alternatives_ =
}, alternatives_);
decode(sit);
}

template <typename ... Alts>
template <typename... Alts>
template <detail::ValidAlts T>
T const&
STVariant<Alts ...>::get() const
STVariant<Alts...>::get() const
{
if (!std::holds_alternative<T>(alternatives_))
Throw<std::runtime_error>("The variant doesn't hold alternative");
return std::get<T>(alternatives_);
}

template <typename ... Alts>
template <typename... Alts>
template <detail::ValidAlts T>
T&
STVariant<Alts ...>::get() const
STVariant<Alts...>::get()
{
if (!std::holds_alternative<T>(alternatives_))
Throw<std::runtime_error>("The variant doesn't hold alternative");
return std::get<T>(alternatives_);
}

// STBase
template <typename ... Alts>
SerializedTypeID
STVariant<Alts ...>::getSType() const
{
}

template <typename ... Alts>
template <typename... Alts>
std::string
STVariant<Alts ...>::getText() const
STVariant<Alts...>::getText() const
{
return std::visit([&](auto&& alt) { return alt.getText(); }, alternatives_);
}

template <typename ... Alts>
Json::Value STVariant<Alts ...>::getJson(JsonOptions) const
template <typename... Alts>
std::string
STVariant<Alts...>::getFullText() const
{
return std::visit(
[&](auto&& alt) { return alt.getFullText(); }, alternatives_);
}

template <typename ... Alts>
void
STVariant<Alts ...>::add(Serializer& s) const
template <typename... Alts>
Json::Value
STVariant<Alts...>::getJson(JsonOptions) const
{
return std::visit(
[&](auto&& alt) { return alt.getJson(JsonOptions::none); },
alternatives_);
}

template <typename ... Alts>
bool
STVariant<Alts...>::isEquivalent(const STBase& t) const
template <typename... Alts>
void
STVariant<Alts...>::add(Serializer& s) const
{
std::visit([&](auto&& alt) { alt.add(s); }, alternatives_);
}

template <typename ... Alts>
template <typename... Alts>
bool
STVariant<Alts ...>::isDefault() const
STVariant<Alts...>::isDefault() const
{
return std::visit(
[&](auto&& alt) { return alt.isDefault(); }, alternatives_);
}

template <typename ... Alts>
std::unique_ptr<STCurrency>
STVariant<Alts ...>::construct(SerialIter&, SField const& name)
template <typename... Alts>
void
STVariant<Alts...>::setJson(Json::Value& jv) const
{
return std::visit(
[&](auto&& alt) { return alt.setJson(jv); }, alternatives_);
}

template <typename ... Alts>
STBase*
STVariant<Alts ...>::copy(std::size_t n, void* buf) const
template <typename... Alts>
auto const&
STVariant<Alts...>::getValue() const
{
return alternatives_;
}

template <typename ... Alts>
STBase*
STVariant<Alts ...>::move(std::size_t n, void* buf)
template <typename... Alts>
auto&
STVariant<Alts...>::getValue()
{
return alternatives_;
}

} // namespace ripple
} // namespace ripple

#endif // RIPPLE_PROTOCOL_STVARIANT_H_INCLUDED
#endif // RIPPLE_PROTOCOL_STVARIANT_H_INCLUDED
Loading

0 comments on commit 8ddaae2

Please sign in to comment.