summaryrefslogtreecommitdiffstats
path: root/mfbt/ResultExtensions.h
blob: 97f197d800c009c311e650c39d245170426435d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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"
#include "prtypes.h"
#include "mozilla/dom/quota/RemoveParen.h"

namespace mozilla {

struct ErrorPropagationTag;

// Allow nsresult errors to automatically convert to nsresult values, so MOZ_TRY
// can be used in XPCOM methods with Result<T, nserror> results.
template <>
class [[nodiscard]] GenericErrorResult<nsresult> {
  nsresult mErrorValue;

  template <typename V, typename E2>
  friend class Result;

 public:
  explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {
    MOZ_ASSERT(NS_FAILED(aErrorValue));
  }

  GenericErrorResult(nsresult aErrorValue, const ErrorPropagationTag&)
      : GenericErrorResult(aErrorValue) {}

  operator nsresult() const { return mErrorValue; }
};

// Allow MOZ_TRY to handle `PRStatus` values.
template <typename E = nsresult>
inline Result<Ok, E> ToResult(PRStatus aValue);

}  // namespace mozilla

#include "mozilla/Result.h"

namespace mozilla {

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) {
  if (NS_FAILED(aValue)) {
    return Err(ResultTypeTraits<E>::From(aValue));
  }
  return Ok();
}

template <typename E>
inline Result<Ok, E> ToResult(PRStatus aValue) {
  if (aValue == PR_SUCCESS) {
    return Ok();
  }
  return Err(ResultTypeTraits<E>::From(NS_ERROR_FAILURE));
}

namespace detail {
template <typename R>
auto ResultRefAsParam(R& aResult) {
  return &aResult;
}

template <typename R, typename E, typename RArgMapper, typename Func,
          typename... Args>
Result<R, E> ToResultInvokeInternal(const Func& aFunc,
                                    const RArgMapper& aRArgMapper,
                                    Args&&... aArgs) {
  // 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");

  R res;
  nsresult rv = aFunc(std::forward<Args>(aArgs)..., aRArgMapper(res));
  if (NS_FAILED(rv)) {
    return Err(ResultTypeTraits<E>::From(rv));
  }
  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&;
};

template <typename R, typename E, template <typename> typename RArg,
          typename Func, typename... Args>
using to_result_retval_t =
    decltype(std::declval<Func&>()(
                 std::declval<Args&&>()...,
                 std::declval<typename RArg<decltype(ResultRefAsParam(
                     std::declval<R&>()))>::type>()),
             Result<R, E>(Err(ResultTypeTraits<E>::From(NS_ERROR_FAILURE))));

// 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.
template <typename R, typename E, typename Func, typename... Args>
auto ToResultInvokeSelector(const Func& aFunc, Args&&... aArgs)
    -> to_result_retval_t<R, E, outparam_as_pointer, Func, Args...> {
  return ToResultInvokeInternal<R, E>(
      aFunc, [](R& res) -> decltype(auto) { return ResultRefAsParam(res); },
      std::forward<Args>(aArgs)...);
}

template <typename R, typename E, typename Func, typename... Args>
auto ToResultInvokeSelector(const Func& aFunc, Args&&... aArgs)
    -> to_result_retval_t<R, E, outparam_as_reference, Func, Args...> {
  return ToResultInvokeInternal<R, E>(
      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);
 *
 * 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:
 *
 *    nsCOMPtr<nsIFile> file = ...;
 *    auto existsOrErr = ToResultInvokeMember(*file, &nsIFile::Exists);
 */
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&&...>(
      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;
};

template <typename E, typename RArg, typename T, typename Func,
          typename... Args>
auto ToResultInvokeMemberInternal(T& aObj, const Func& aFunc, Args&&... aArgs) {
  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<
        std::remove_reference_t<std::remove_pointer_t<RArg>>, E,
        decltype(lambda)>(lambda);
  } else {
    // No output parameter present, return a Result<Ok, E>
    return mozilla::ToResult<E>((aObj.*aFunc)(std::forward<Args>(aArgs)...));
  }
}

// For use in MOZ_TO_RESULT_INVOKE_MEMBER/MOZ_TO_RESULT_INVOKE_MEMBER_TYPED.
template <typename T>
auto DerefHelper(const T&) -> T&;

template <typename T>
auto DerefHelper(T*) -> T&;

template <template <class> class SmartPtr, typename T,
          typename = decltype(*std::declval<const SmartPtr<T>>())>
auto DerefHelper(const SmartPtr<T>&) -> T&;

template <typename T>
using DerefedType =
    std::remove_reference_t<decltype(DerefHelper(std::declval<const T&>()))>;
}  // namespace detail

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>>
auto ToResultInvokeMember(T& aObj, nsresult (U::*aFunc)(XArgs...),
                          Args&&... aArgs) {
  return detail::ToResultInvokeMemberInternal<E,
                                              detail::select_last_t<XArgs...>>(
      aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>>
auto ToResultInvokeMember(const T& aObj, nsresult (U::*aFunc)(XArgs...) const,
                          Args&&... aArgs) {
  return detail::ToResultInvokeMemberInternal<E,
                                              detail::select_last_t<XArgs...>>(
      aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args>
auto ToResultInvokeMember(T* const aObj, nsresult (U::*aFunc)(XArgs...),
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args>
auto ToResultInvokeMember(const T* const aObj,
                          nsresult (U::*aFunc)(XArgs...) const,
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, template <class> class SmartPtr, typename T,
          typename U, typename... XArgs, typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>,
          typename = decltype(*std::declval<const SmartPtr<T>>())>
auto ToResultInvokeMember(const SmartPtr<T>& aObj,
                          nsresult (U::*aFunc)(XArgs...), Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, template <class> class SmartPtr, typename T,
          typename U, typename... XArgs, typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>,
          typename = decltype(*std::declval<const SmartPtr<T>>())>
auto ToResultInvokeMember(const SmartPtr<const T>& aObj,
                          nsresult (U::*aFunc)(XArgs...) const,
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

#if defined(XP_WIN) && !defined(_WIN64)
template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>>
auto ToResultInvokeMember(T& aObj, nsresult (__stdcall U::*aFunc)(XArgs...),
                          Args&&... aArgs) {
  return detail::ToResultInvokeMemberInternal<E,
                                              detail::select_last_t<XArgs...>>(
      aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>>
auto ToResultInvokeMember(const T& aObj,
                          nsresult (__stdcall U::*aFunc)(XArgs...) const,
                          Args&&... aArgs) {
  return detail::ToResultInvokeMemberInternal<E,
                                              detail::select_last_t<XArgs...>>(
      aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args>
auto ToResultInvokeMember(T* const aObj,
                          nsresult (__stdcall U::*aFunc)(XArgs...),
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, typename T, typename U, typename... XArgs,
          typename... Args>
auto ToResultInvokeMember(const T* const aObj,
                          nsresult (__stdcall U::*aFunc)(XArgs...) const,
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, template <class> class SmartPtr, typename T,
          typename U, typename... XArgs, typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>,
          typename = decltype(*std::declval<const SmartPtr<T>>())>
auto ToResultInvokeMember(const SmartPtr<T>& aObj,
                          nsresult (__stdcall U::*aFunc)(XArgs...),
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}

template <typename E = nsresult, template <class> class SmartPtr, typename T,
          typename U, typename... XArgs, typename... Args,
          typename = std::enable_if_t<std::is_base_of_v<U, T>>,
          typename = decltype(*std::declval<const SmartPtr<T>>())>
auto ToResultInvokeMember(const SmartPtr<const T>& aObj,
                          nsresult (__stdcall U::*aFunc)(XArgs...) const,
                          Args&&... aArgs) {
  return ToResultInvokeMember<E>(*aObj, aFunc, std::forward<Args>(aArgs)...);
}
#endif

// 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:
//
//     nsCOMPtr<nsIFile> file;
//     auto existsOrErr = MOZ_TO_RESULT_INVOKE_MEMBER(file, Exists);
#define MOZ_TO_RESULT_INVOKE_MEMBER(obj, methodname, ...)                \
  ::mozilla::ToResultInvokeMember(                                       \
      (obj), &::mozilla::detail::DerefedType<decltype(obj)>::methodname, \
      ##__VA_ARGS__)

// 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:
//
//     nsCOMPtr<nsIFile> file;
//     auto existsOrErr =
//         MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<nsIFile>, file, Clone);
#define MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(resultType, obj, methodname, ...) \
  ::mozilla::ToResultInvoke<MOZ_REMOVE_PAREN(resultType)>(                  \
      ::std::mem_fn(                                                        \
          &::mozilla::detail::DerefedType<decltype(obj)>::methodname),      \
      (obj), ##__VA_ARGS__)

}  // namespace mozilla

#endif  // mozilla_ResultExtensions_h