2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2017-08-30 07:28:22 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* Extensions to the Result type to enable simpler handling of XPCOM/NSPR
|
|
|
|
* results. */
|
|
|
|
|
|
|
|
#ifndef mozilla_ResultExtensions_h
|
|
|
|
#define mozilla_ResultExtensions_h
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "nscore.h"
|
2017-08-30 07:28:31 +03:00
|
|
|
#include "prtypes.h"
|
2021-11-30 08:05:53 +03:00
|
|
|
#include "mozilla/dom/quota/RemoveParen.h"
|
2017-08-30 07:28:22 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2021-05-25 10:28:20 +03:00
|
|
|
struct ErrorPropagationTag;
|
|
|
|
|
2017-08-30 07:28:22 +03:00
|
|
|
// Allow nsresult errors to automatically convert to nsresult values, so MOZ_TRY
|
|
|
|
// can be used in XPCOM methods with Result<T, nserror> results.
|
|
|
|
template <>
|
2021-09-15 08:10:51 +03:00
|
|
|
class [[nodiscard]] GenericErrorResult<nsresult> {
|
2017-08-30 07:28:22 +03:00
|
|
|
nsresult mErrorValue;
|
|
|
|
|
|
|
|
template <typename V, typename E2>
|
|
|
|
friend class Result;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {
|
|
|
|
MOZ_ASSERT(NS_FAILED(aErrorValue));
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:28:20 +03:00
|
|
|
GenericErrorResult(nsresult aErrorValue, const ErrorPropagationTag&)
|
|
|
|
: GenericErrorResult(aErrorValue) {}
|
|
|
|
|
2020-08-04 18:23:47 +03:00
|
|
|
operator nsresult() const { return mErrorValue; }
|
2017-08-30 07:28:22 +03:00
|
|
|
};
|
|
|
|
|
2017-08-30 07:28:31 +03:00
|
|
|
// Allow MOZ_TRY to handle `PRStatus` values.
|
2021-10-11 11:34:01 +03:00
|
|
|
template <typename E = nsresult>
|
|
|
|
inline Result<Ok, E> ToResult(PRStatus aValue);
|
2017-08-30 07:28:31 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#include "mozilla/Result.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2021-10-11 11:34:01 +03:00
|
|
|
template <typename ResultType>
|
|
|
|
struct ResultTypeTraits;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ResultTypeTraits<nsresult> {
|
|
|
|
static nsresult From(nsresult aValue) { return aValue; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
inline Result<Ok, E> ToResult(nsresult aValue) {
|
2017-08-30 07:28:31 +03:00
|
|
|
if (NS_FAILED(aValue)) {
|
2021-10-11 11:34:01 +03:00
|
|
|
return Err(ResultTypeTraits<E>::From(aValue));
|
2017-08-30 07:28:31 +03:00
|
|
|
}
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
2021-10-11 11:34:01 +03:00
|
|
|
template <typename E>
|
|
|
|
inline Result<Ok, E> ToResult(PRStatus aValue) {
|
2017-08-30 07:28:31 +03:00
|
|
|
if (aValue == PR_SUCCESS) {
|
|
|
|
return Ok();
|
|
|
|
}
|
2021-10-11 11:34:01 +03:00
|
|
|
return Err(ResultTypeTraits<E>::From(NS_ERROR_FAILURE));
|
2017-08-30 07:28:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 11:25:54 +03:00
|
|
|
namespace detail {
|
|
|
|
template <typename R>
|
|
|
|
auto ResultRefAsParam(R& aResult) {
|
|
|
|
return &aResult;
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename R, typename E, typename RArgMapper, typename Func,
|
|
|
|
typename... Args>
|
|
|
|
Result<R, E> ToResultInvokeInternal(const Func& aFunc,
|
|
|
|
const RArgMapper& aRArgMapper,
|
|
|
|
Args&&... aArgs) {
|
2020-09-16 18:11:56 +03:00
|
|
|
// XXX Thereotically, if R is a pointer to a non-refcounted type, this might
|
|
|
|
// be a non-owning pointer, but unless we find a case where this actually is
|
|
|
|
// relevant, it's safe to forbid any raw pointer result.
|
|
|
|
static_assert(
|
|
|
|
!std::is_pointer_v<R>,
|
|
|
|
"Raw pointer results are not supported, please specify a smart pointer "
|
|
|
|
"result type explicitly, so that getter_AddRefs is used");
|
|
|
|
|
2020-05-19 11:25:54 +03:00
|
|
|
R res;
|
|
|
|
nsresult rv = aFunc(std::forward<Args>(aArgs)..., aRArgMapper(res));
|
|
|
|
if (NS_FAILED(rv)) {
|
2021-11-12 14:56:02 +03:00
|
|
|
return Err(ResultTypeTraits<E>::From(rv));
|
2020-05-19 11:25:54 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct outparam_as_pointer;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct outparam_as_pointer<T*> {
|
|
|
|
using type = T*;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct outparam_as_reference;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct outparam_as_reference<T*> {
|
|
|
|
using type = T&;
|
|
|
|
};
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename R, typename E, template <typename> typename RArg,
|
|
|
|
typename Func, typename... Args>
|
2021-05-10 10:15:07 +03:00
|
|
|
using to_result_retval_t =
|
|
|
|
decltype(std::declval<Func&>()(
|
|
|
|
std::declval<Args&&>()...,
|
|
|
|
std::declval<typename RArg<decltype(ResultRefAsParam(
|
|
|
|
std::declval<R&>()))>::type>()),
|
2021-11-12 14:56:02 +03:00
|
|
|
Result<R, E>(Err(ResultTypeTraits<E>::From(NS_ERROR_FAILURE))));
|
2020-05-19 11:25:54 +03:00
|
|
|
|
|
|
|
// There are two ToResultInvokeSelector overloads, which cover the cases of a) a
|
|
|
|
// pointer-typed output parameter, and b) a reference-typed output parameter,
|
|
|
|
// using to_result_retval_t in connection with outparam_as_pointer and
|
|
|
|
// outparam_as_reference type traits. These type traits may be specialized for
|
|
|
|
// types other than raw pointers to allow calling functions with argument types
|
|
|
|
// that implicitly convert/bind to a raw pointer/reference. The overload that is
|
|
|
|
// used is selected by expression SFINAE: the decltype expression in
|
|
|
|
// to_result_retval_t is only valid in either case.
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename R, typename E, typename Func, typename... Args>
|
2020-05-19 11:25:54 +03:00
|
|
|
auto ToResultInvokeSelector(const Func& aFunc, Args&&... aArgs)
|
2021-11-12 14:56:02 +03:00
|
|
|
-> to_result_retval_t<R, E, outparam_as_pointer, Func, Args...> {
|
|
|
|
return ToResultInvokeInternal<R, E>(
|
2020-05-19 11:25:54 +03:00
|
|
|
aFunc, [](R& res) -> decltype(auto) { return ResultRefAsParam(res); },
|
|
|
|
std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename R, typename E, typename Func, typename... Args>
|
2020-05-19 11:25:54 +03:00
|
|
|
auto ToResultInvokeSelector(const Func& aFunc, Args&&... aArgs)
|
2021-11-12 14:56:02 +03:00
|
|
|
-> to_result_retval_t<R, E, outparam_as_reference, Func, Args...> {
|
|
|
|
return ToResultInvokeInternal<R, E>(
|
2020-05-19 11:25:54 +03:00
|
|
|
aFunc, [](R& res) -> decltype(auto) { return *ResultRefAsParam(res); },
|
|
|
|
std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adapts a function with a nsresult error type and an R* output parameter as
|
|
|
|
* the last parameter to a function returning a mozilla::Result<R, nsresult>
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* This can also be used with member functions together with std::men_fn, e.g.
|
|
|
|
*
|
|
|
|
* nsCOMPtr<nsIFile> file = ...;
|
|
|
|
* auto existsOrErr = ToResultInvoke<bool>(std::mem_fn(&nsIFile::Exists),
|
|
|
|
* *file);
|
|
|
|
*
|
2021-11-30 08:05:53 +03:00
|
|
|
* but it is more convenient to use the member function version, which has the
|
|
|
|
* additional benefit of enabling the deduction of the success result type:
|
2020-05-19 11:25:54 +03:00
|
|
|
*
|
|
|
|
* nsCOMPtr<nsIFile> file = ...;
|
2021-11-30 08:05:53 +03:00
|
|
|
* auto existsOrErr = ToResultInvokeMember(*file, &nsIFile::Exists);
|
2020-05-19 11:25:54 +03:00
|
|
|
*/
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename R, typename E = nsresult, typename Func, typename... Args>
|
|
|
|
Result<R, E> ToResultInvoke(const Func& aFunc, Args&&... aArgs) {
|
|
|
|
return detail::ToResultInvokeSelector<R, E, Func, Args&&...>(
|
2020-05-19 11:25:54 +03:00
|
|
|
aFunc, std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template <typename T>
|
|
|
|
struct tag {
|
|
|
|
using type = T;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Ts>
|
|
|
|
struct select_last {
|
|
|
|
using type = typename decltype((tag<Ts>{}, ...))::type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Ts>
|
|
|
|
using select_last_t = typename select_last<Ts...>::type;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct select_last<> {
|
|
|
|
using type = void;
|
|
|
|
};
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E, typename RArg, typename T, typename Func,
|
|
|
|
typename... Args>
|
2021-11-30 08:05:52 +03:00
|
|
|
auto ToResultInvokeMemberInternal(T& aObj, const Func& aFunc, Args&&... aArgs) {
|
2020-05-19 11:25:54 +03:00
|
|
|
if constexpr (std::is_pointer_v<RArg> ||
|
|
|
|
(std::is_lvalue_reference_v<RArg> &&
|
|
|
|
!std::is_const_v<std::remove_reference_t<RArg>>)) {
|
|
|
|
auto lambda = [&](RArg res) {
|
|
|
|
return (aObj.*aFunc)(std::forward<Args>(aArgs)..., res);
|
|
|
|
};
|
|
|
|
return detail::ToResultInvokeSelector<
|
2021-11-12 14:56:02 +03:00
|
|
|
std::remove_reference_t<std::remove_pointer_t<RArg>>, E,
|
|
|
|
decltype(lambda)>(lambda);
|
2020-05-19 11:25:54 +03:00
|
|
|
} else {
|
2021-11-12 14:56:02 +03:00
|
|
|
// No output parameter present, return a Result<Ok, E>
|
|
|
|
return mozilla::ToResult<E>((aObj.*aFunc)(std::forward<Args>(aArgs)...));
|
2020-05-19 11:25:54 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-17 09:34:39 +03:00
|
|
|
|
2021-11-30 08:05:53 +03:00
|
|
|
// For use in MOZ_TO_RESULT_INVOKE_MEMBER/MOZ_TO_RESULT_INVOKE_MEMBER_TYPED.
|
2020-08-17 09:34:39 +03:00
|
|
|
template <typename T>
|
2020-08-28 18:50:49 +03:00
|
|
|
auto DerefHelper(const T&) -> T&;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
auto DerefHelper(T*) -> T&;
|
2020-08-17 09:34:39 +03:00
|
|
|
|
|
|
|
template <template <class> class SmartPtr, typename T,
|
|
|
|
typename = decltype(*std::declval<const SmartPtr<T>>())>
|
2020-08-28 18:50:49 +03:00
|
|
|
auto DerefHelper(const SmartPtr<T>&) -> T&;
|
2020-08-17 09:34:39 +03:00
|
|
|
|
|
|
|
template <typename T>
|
2020-08-28 18:50:49 +03:00
|
|
|
using DerefedType =
|
|
|
|
std::remove_reference_t<decltype(DerefHelper(std::declval<const T&>()))>;
|
2020-05-19 11:25:54 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args,
|
2020-08-12 21:33:14 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(T& aObj, nsresult (U::*aFunc)(XArgs...),
|
|
|
|
Args&&... aArgs) {
|
2021-11-30 08:05:52 +03:00
|
|
|
return detail::ToResultInvokeMemberInternal<E,
|
2021-11-12 14:56:02 +03:00
|
|
|
detail::select_last_t<XArgs...>>(
|
2020-05-19 11:25:54 +03:00
|
|
|
aObj, aFunc, std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args,
|
2020-08-12 21:33:14 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const T& aObj, nsresult (U::*aFunc)(XArgs...) const,
|
|
|
|
Args&&... aArgs) {
|
2021-11-30 08:05:52 +03:00
|
|
|
return detail::ToResultInvokeMemberInternal<E,
|
2021-11-12 14:56:02 +03:00
|
|
|
detail::select_last_t<XArgs...>>(
|
2020-05-19 11:25:54 +03:00
|
|
|
aObj, aFunc, std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(T* const aObj, nsresult (U::*aFunc)(XArgs...),
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-05-19 11:25:54 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const T* const aObj,
|
|
|
|
nsresult (U::*aFunc)(XArgs...) const,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-05-19 11:25:54 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, template <class> class SmartPtr, typename T,
|
|
|
|
typename U, typename... XArgs, typename... Args,
|
2020-08-12 21:33:14 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>,
|
|
|
|
typename = decltype(*std::declval<const SmartPtr<T>>())>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const SmartPtr<T>& aObj,
|
|
|
|
nsresult (U::*aFunc)(XArgs...), Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-08-12 21:33:14 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, template <class> class SmartPtr, typename T,
|
|
|
|
typename U, typename... XArgs, typename... Args,
|
2020-08-12 21:33:14 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>,
|
|
|
|
typename = decltype(*std::declval<const SmartPtr<T>>())>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const SmartPtr<const T>& aObj,
|
|
|
|
nsresult (U::*aFunc)(XArgs...) const,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-08-12 21:33:14 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:15:58 +03:00
|
|
|
#if defined(XP_WIN) && !defined(_WIN64)
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args,
|
2020-08-19 17:34:06 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(T& aObj, nsresult (__stdcall U::*aFunc)(XArgs...),
|
|
|
|
Args&&... aArgs) {
|
2021-11-30 08:05:52 +03:00
|
|
|
return detail::ToResultInvokeMemberInternal<E,
|
2021-11-12 14:56:02 +03:00
|
|
|
detail::select_last_t<XArgs...>>(
|
2020-05-19 16:15:58 +03:00
|
|
|
aObj, aFunc, std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args,
|
2020-08-19 17:34:06 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const T& aObj,
|
|
|
|
nsresult (__stdcall U::*aFunc)(XArgs...) const,
|
|
|
|
Args&&... aArgs) {
|
2021-11-30 08:05:52 +03:00
|
|
|
return detail::ToResultInvokeMemberInternal<E,
|
2021-11-12 14:56:02 +03:00
|
|
|
detail::select_last_t<XArgs...>>(
|
2020-05-19 16:15:58 +03:00
|
|
|
aObj, aFunc, std::forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(T* const aObj,
|
|
|
|
nsresult (__stdcall U::*aFunc)(XArgs...),
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-05-19 16:15:58 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, typename T, typename U, typename... XArgs,
|
|
|
|
typename... Args>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const T* const aObj,
|
|
|
|
nsresult (__stdcall U::*aFunc)(XArgs...) const,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-05-19 16:15:58 +03:00
|
|
|
}
|
2020-08-19 17:34:06 +03:00
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, template <class> class SmartPtr, typename T,
|
|
|
|
typename U, typename... XArgs, typename... Args,
|
2020-08-19 17:34:06 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>,
|
|
|
|
typename = decltype(*std::declval<const SmartPtr<T>>())>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const SmartPtr<T>& aObj,
|
|
|
|
nsresult (__stdcall U::*aFunc)(XArgs...),
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-08-19 17:34:06 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:56:02 +03:00
|
|
|
template <typename E = nsresult, template <class> class SmartPtr, typename T,
|
|
|
|
typename U, typename... XArgs, typename... Args,
|
2020-08-19 17:34:06 +03:00
|
|
|
typename = std::enable_if_t<std::is_base_of_v<U, T>>,
|
|
|
|
typename = decltype(*std::declval<const SmartPtr<T>>())>
|
2021-11-30 08:05:53 +03:00
|
|
|
auto ToResultInvokeMember(const SmartPtr<const T>& aObj,
|
|
|
|
nsresult (__stdcall U::*aFunc)(XArgs...) const,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
|
2020-08-19 17:34:06 +03:00
|
|
|
}
|
2020-05-19 16:15:58 +03:00
|
|
|
#endif
|
|
|
|
|
2021-11-30 08:05:53 +03:00
|
|
|
// Macro version of ToResultInvokeMember for member functions. 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:
|
2020-08-17 09:34:39 +03:00
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIFile> file;
|
2021-11-30 08:05:53 +03:00
|
|
|
// auto existsOrErr = MOZ_TO_RESULT_INVOKE_MEMBER(file, Exists);
|
|
|
|
#define MOZ_TO_RESULT_INVOKE_MEMBER(obj, methodname, ...) \
|
|
|
|
::mozilla::ToResultInvokeMember( \
|
2020-08-17 09:34:39 +03:00
|
|
|
(obj), &::mozilla::detail::DerefedType<decltype(obj)>::methodname, \
|
|
|
|
##__VA_ARGS__)
|
|
|
|
|
2021-11-30 08:05:53 +03:00
|
|
|
// Macro version of ToResultInvokeMember 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:
|
2020-09-16 13:18:19 +03:00
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIFile> file;
|
2021-11-30 08:05:53 +03:00
|
|
|
// auto existsOrErr =
|
|
|
|
// MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<nsIFile>, file, Clone);
|
|
|
|
#define MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(resultType, obj, methodname, ...) \
|
2021-11-30 08:05:53 +03:00
|
|
|
::mozilla::ToResultInvoke<MOZ_REMOVE_PAREN(resultType)>( \
|
2021-11-30 08:05:53 +03:00
|
|
|
::std::mem_fn( \
|
|
|
|
&::mozilla::detail::DerefedType<decltype(obj)>::methodname), \
|
2020-09-16 13:18:19 +03:00
|
|
|
(obj), ##__VA_ARGS__)
|
|
|
|
|
2017-08-30 07:28:22 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_ResultExtensions_h
|