Bug 1661404 - Add MOZ_TO_RESULT_INVOKE_TYPED macro. r=janv

Differential Revision: https://phabricator.services.mozilla.com/D90368
This commit is contained in:
Simon Giesecke 2020-09-16 10:18:19 +00:00
parent 9593c04f82
commit 720fbb0265
2 changed files with 31 additions and 5 deletions

View File

@ -297,8 +297,9 @@ auto ToResultInvoke(const SmartPtr<const T>& aObj,
#endif
// Macro version of ToResultInvoke for member functions. The macro has the
// advantage of not requiring spelling out the type name, at the expense of
// having a non-standard syntax. It can be used like this:
// advantage of not requiring spelling out the member function's declarator type
// name, at the expense of having a non-standard syntax. It can be used like
// this:
//
// nsCOMPtr<nsIFile> file;
// auto existsOrErr = MOZ_TO_RESULT_INVOKE(file, Exists);
@ -307,6 +308,19 @@ auto ToResultInvoke(const SmartPtr<const T>& aObj,
(obj), &::mozilla::detail::DerefedType<decltype(obj)>::methodname, \
##__VA_ARGS__)
// Macro version of ToResultInvoke for member functions, where the result type
// does not match the output parameter type. The macro has the advantage of not
// requiring spelling out the member function's declarator type name, at the
// expense of having a non-standard syntax. It can be used like this:
//
// nsCOMPtr<nsIFile> file;
// auto existsOrErr = MOZ_TO_RESULT_INVOKE(nsCOMPtr<nsIFile>, file, Clone);
#define MOZ_TO_RESULT_INVOKE_TYPED(resultType, obj, methodname, ...) \
::mozilla::ToResultInvoke<resultType>( \
::std::mem_fn( \
&::mozilla::detail::DerefedType<decltype(obj)>::methodname), \
(obj), ##__VA_ARGS__)
} // namespace mozilla
#endif // mozilla_ResultExtensions_h

View File

@ -455,8 +455,7 @@ TEST(ResultExtensions_ToResultInvoke, nsCOMPtr_NS_IMETHOD_bool_Result)
ASSERT_TRUE(ToResultInvoke(file, &nsIFile::Equals, file).isOk());
}
TEST(ResultExtensions_ToResultInvoke,
RawPtr_AbstractClass_MemberFunction_NoInput_Macro)
TEST(ResultExtensions_ToResultInvoke, RawPtr_AbstractClass_MemberFunction_Macro)
{
nsCOMPtr<nsIFile> file = MakeAndAddRef<nsLocalFile>();
auto* filePtr = file.get();
@ -465,4 +464,17 @@ TEST(ResultExtensions_ToResultInvoke,
static_assert(std::is_same_v<decltype(valOrErr), Result<bool, nsresult>>);
ASSERT_TRUE(valOrErr.isOk());
ASSERT_EQ(true, valOrErr.unwrap());
}
}
TEST(ResultExtensions_ToResultInvoke,
RawPtr_AbstractClass_MemberFunction_NoInput_Macro_Typed)
{
nsCOMPtr<nsIFile> file = MakeAndAddRef<nsLocalFile>();
auto* filePtr = file.get();
auto valOrErr = MOZ_TO_RESULT_INVOKE_TYPED(nsCOMPtr<nsIFile>, filePtr, Clone);
static_assert(
std::is_same_v<decltype(valOrErr), Result<nsCOMPtr<nsIFile>, nsresult>>);
ASSERT_TRUE(valOrErr.isOk());
ASSERT_NE(nullptr, valOrErr.unwrap());
}