Bug 1637605 - Add support for polymorphic return values to ToResultInvoke. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D75294
This commit is contained in:
Simon Giesecke 2020-05-19 09:07:08 +00:00
parent 24e5290fb8
commit f5342d154a
2 changed files with 55 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include "gtest/gtest.h"
#include "mozilla/ResultExtensions.h"
#include "nsLocalFile.h"
#include <functional>
@ -47,6 +48,12 @@ class TestClass {
nsresult NonOverloadedNoOutput(int aIn) { return NS_OK; }
nsresult NonOverloadedNoOutputFails(int aIn) { return NS_ERROR_FAILURE; }
nsresult PolymorphicNoInput(nsIFile** aOut) {
*aOut = MakeAndAddRef<nsLocalFile>().take();
return NS_OK;
}
nsresult PolymorphicNoInputFails(nsIFile** aOut) { return NS_ERROR_FAILURE; }
};
} // namespace
@ -205,3 +212,35 @@ TEST(ResultExtensions_ToResultInvoke, MemberFunction_NoOutput)
ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
}
}
TEST(ResultExtensions_ToResultInvoke, PolymorphicPointerResult_nsCOMPtr_Result)
{
TestClass foo;
// success
{
auto valOrErr = ToResultInvoke<nsCOMPtr<nsIFile>>(
std::mem_fn(&TestClass::PolymorphicNoInput), foo);
static_assert(std::is_same_v<decltype(valOrErr),
Result<nsCOMPtr<nsIFile>, nsresult>>);
ASSERT_TRUE(valOrErr.isOk());
ASSERT_NE(nullptr, valOrErr.inspect());
ASSERT_EQ(ToResultInvoke<nsString>(std::mem_fn(&nsIFile::GetPath),
*MakeRefPtr<nsLocalFile>())
.inspect(),
ToResultInvoke<nsString>(std::mem_fn(&nsIFile::GetPath),
valOrErr.inspect())
.inspect());
}
// failure
{
auto valOrErr = ToResultInvoke<nsCOMPtr<nsIFile>>(
std::mem_fn(&TestClass::PolymorphicNoInputFails), foo);
static_assert(std::is_same_v<decltype(valOrErr),
Result<nsCOMPtr<nsIFile>, nsresult>>);
ASSERT_TRUE(valOrErr.isErr());
ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
}
}

View File

@ -1507,4 +1507,20 @@ RefPtr<T> ToRefPtr(nsCOMPtr<T>&& aObj) {
return aObj.forget();
}
// Integration with ResultExtensions.h
template <typename R>
auto ResultRefAsParam(nsCOMPtr<R>& aResult) {
return getter_AddRefs(aResult);
}
namespace mozilla::detail {
template <typename T>
struct outparam_as_pointer;
template <typename T>
struct outparam_as_pointer<nsGetterAddRefs<T>> {
using type = T**;
};
} // namespace mozilla::detail
#endif // !defined(nsCOMPtr_h___)