Skip to content

Commit

Permalink
Fix linux build, refactor get<T>() to
Browse files Browse the repository at this point in the history
differentiate return by val and by ref
  • Loading branch information
gregtatcam committed Jul 21, 2024
1 parent c53bb8e commit 5d7e181
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
61 changes: 46 additions & 15 deletions include/xrpl/protocol/STEitherAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,44 +119,75 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>
int
signum() const noexcept;

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

template <ValidAmountType T>
T&
get();

private:
STBase*
copy(std::size_t n, void* buf) const override;
STBase*
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)
{
if constexpr (std::
is_same_v<std::decay_t<decltype(amount)>, STEitherAmount>)
using TAmnt = std::decay_t<decltype(amount)>;
if constexpr (std::is_same_v<TAmnt, STEitherAmount>)
{
if (std::holds_alternative<T>(amount.getValue()))
return std::get<T>(amount.getValue());
Throw<std::logic_error>("Invalid STEitherAmount conversion");
if constexpr (std::is_lvalue_reference_v<decltype(amount)>)
return amount.template get<T>();
else
{
T a = amount.template get<T>();
return a;
}
}
else if constexpr (std::is_same_v<
std::decay_t<decltype(amount)>,
std::optional<STEitherAmount>>)
else if constexpr (std::is_same_v<TAmnt, std::optional<STEitherAmount>>)
{
static std::optional<T> t;
if (amount.has_value())
return std::make_optional(get<T>(*amount));
return std::make_optional(amount->template get<T>());
return t;
}
else if constexpr (std::is_convertible_v<
std::decay_t<decltype(amount)>,
STEitherAmount>)
else if constexpr (std::is_convertible_v<TAmnt, STEitherAmount>)
{
T a = get<T>(amount.operator STEitherAmount());
return a;
if constexpr (std::is_lvalue_reference_v<decltype(amount)>)
return amount.operator STEitherAmount().template get<T>();
else
{
T a = amount.operator STEitherAmount().template get<T>();
return a;
}
}
else
{
bool const alwaysFalse = !std::is_same_v<T, T>;
static_assert(alwaysFalse, "Converting non-STEitherAmount");
static_assert(alwaysFalse, "Invalid STEitherAmount conversion");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libxrpl/protocol/STEitherAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ STEitherAmount::sameIssue(STEitherAmount const& amount) const
amount.amount_);
}

STEitherAmount::value_type const&
STEitherAmount const&
STEitherAmount::value() const
{
return *this;
Expand All @@ -225,8 +225,8 @@ AccountID const&
STEitherAmount::getIssuer() const
{
if (isIssue())
return get<STAmount>(amount_).getIssuer();
return get<STMPTAmount>(amount_).getIssuer();
return get<STAmount>().getIssuer();
return get<STMPTAmount>().getIssuer();
}

STBase*
Expand Down

0 comments on commit 5d7e181

Please sign in to comment.