diff --git a/include/xrpl/protocol/STEitherAmount.h b/include/xrpl/protocol/STEitherAmount.h index 57812221fd7..187984cebd5 100644 --- a/include/xrpl/protocol/STEitherAmount.h +++ b/include/xrpl/protocol/STEitherAmount.h @@ -127,43 +127,37 @@ class STEitherAmount : public STBase, public CountedObject }; template -T const& -get(STEitherAmount const& amount) +decltype(auto) +get(auto&& amount) { - if (std::holds_alternative(amount.getValue())) - return std::get(amount.getValue()); - Throw("Incorrect type to STEitherAmount::get"); -} - -template -T& -get(STEitherAmount& amount) -{ - if (std::holds_alternative(amount.getValue())) - return std::get(amount.getValue()); - Throw("Incorrect type to STEitherAmount::get"); -} - -template -std::optional -get(std::optional const& amount); - -template <> -inline std::optional -get(std::optional const& amount) -{ - if (amount) - return get(*amount); - return std::nullopt; -} - -template <> -inline std::optional -get(std::optional const& amount) -{ - if (amount) - return get(*amount); - return std::nullopt; + if constexpr (std:: + is_same_v, STEitherAmount>) + { + if (std::holds_alternative(amount.getValue())) + return std::get(amount.getValue()); + Throw("Invalid STEitherAmount conversion"); + } + else if constexpr (std::is_same_v< + std::decay_t, + std::optional>) + { + static std::optional t; + if (amount.has_value()) + return std::make_optional(get(*amount)); + return t; + } + else if constexpr (std::is_convertible_v< + std::decay_t, + STEitherAmount>) + { + T a = get(amount.operator STEitherAmount()); + return a; + } + else + { + bool const alwaysFalse = !std::is_same_v; + static_assert(alwaysFalse, "Converting non-STEitherAmount"); + } } STEitherAmount @@ -178,6 +172,15 @@ amountFromJsonNoThrow(STAmount& result, Json::Value const& jvSource); inline bool operator==(STEitherAmount const& lhs, STEitherAmount const& rhs) { + return std::visit( + [&](T1 const& a1, T2 const& a2) { + if constexpr (std::is_same_v) + return a1 == a2; + else + return false; + }, + lhs.getValue(), + rhs.getValue()); if (lhs.isIssue() == rhs.isIssue()) return lhs.getValue() == rhs.getValue(); return false; diff --git a/src/test/app/MPToken_test.cpp b/src/test/app/MPToken_test.cpp index fcd5fbef9bf..64413d1c3f4 100644 --- a/src/test/app/MPToken_test.cpp +++ b/src/test/app/MPToken_test.cpp @@ -337,7 +337,8 @@ class MPToken_test : public beast::unit_test::suite env, alice, {.holders = {&bob}, - .xrpHolders = acctReserve + get(XRP(1)).xrp()}); + .xrpHolders = + acctReserve + get(XRP(1).value()).xrp()}); mptAlice1.create(); MPTTester mptAlice2(env, alice, {.fund = false});