summaryrefslogtreecommitdiffstats
path: root/dom/quota/ResultExtensions.h
blob: c19bf926612edc9511e552c3331abc5534f54915 (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
/* -*- 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/. */

#ifndef DOM_QUOTA_QMRESULTINLINES_H_
#define DOM_QUOTA_QMRESULTINLINES_H_

#include "nsError.h"
#include "mozilla/Result.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/dom/QMResult.h"
#include "mozilla/dom/quota/Config.h"
#include "mozilla/dom/quota/RemoveParen.h"

#ifdef QM_ERROR_STACKS_ENABLED
#  include "mozilla/ResultVariant.h"
#endif

namespace mozilla {

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

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

 public:
  explicit GenericErrorResult(bool aErrorValue) : mErrorValue(aErrorValue) {
    MOZ_ASSERT(!aErrorValue);
  }

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

  MOZ_IMPLICIT operator bool() const { return mErrorValue; }
};

// Allow MOZ_TRY/QM_TRY to handle `bool` values.
template <typename E = nsresult>
inline Result<Ok, E> ToResult(bool aValue) {
  if (aValue) {
    return Ok();
  }
  return Err(ResultTypeTraits<E>::From(NS_ERROR_FAILURE));
}

constexpr nsresult ToNSResult(nsresult aError) { return aError; }

#ifdef QM_ERROR_STACKS_ENABLED

inline nsresult ToNSResult(const QMResult& aError) { return aError.NSResult(); }

// Allow QMResult errors to use existing stack id and to increase the frame id
// during error propagation.
template <>
class [[nodiscard]] GenericErrorResult<QMResult> {
  QMResult mErrorValue;

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

 public:
  explicit GenericErrorResult(const QMResult& aErrorValue)
      : mErrorValue(aErrorValue) {
    MOZ_ASSERT(NS_FAILED(aErrorValue.NSResult()));
  }

  explicit GenericErrorResult(QMResult&& aErrorValue)
      : mErrorValue(std::move(aErrorValue)) {
    MOZ_ASSERT(NS_FAILED(aErrorValue.NSResult()));
  }

  explicit GenericErrorResult(const QMResult& aErrorValue,
                              const ErrorPropagationTag&)
      : GenericErrorResult(aErrorValue.Propagate()) {}

  explicit GenericErrorResult(QMResult&& aErrorValue,
                              const ErrorPropagationTag&)
      : GenericErrorResult(aErrorValue.Propagate()) {}

  operator QMResult() const { return mErrorValue; }

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

template <>
struct ResultTypeTraits<QMResult> {
  static QMResult From(nsresult aValue) { return ToQMResult(aValue); }

  static QMResult From(const QMResult& aValue) { return aValue; }

  static QMResult From(QMResult&& aValue) { return std::move(aValue); }
};

template <typename E>
inline Result<Ok, E> ToResult(const QMResult& aValue) {
  if (NS_FAILED(aValue.NSResult())) {
    return Err(ResultTypeTraits<E>::From(aValue));
  }
  return Ok();
}

template <typename E>
inline Result<Ok, E> ToResult(QMResult&& aValue) {
  if (NS_FAILED(aValue.NSResult())) {
    return Err(ResultTypeTraits<E>::From(aValue));
  }
  return Ok();
}
#endif

template <typename E = nsresult, typename V, typename E2>
inline Result<V, E> ToResultTransform(Result<V, E2>&& aValue) {
  return std::forward<Result<V, E2>>(aValue).mapErr(
      [](auto&& err) { return ResultTypeTraits<E>::From(err); });
}

// TODO: Maybe move this to mfbt/ResultExtensions.h
template <typename R, typename Func, typename... Args>
Result<R, nsresult> ToResultGet(const Func& aFunc, Args&&... aArgs) {
  nsresult rv;
  R res = aFunc(std::forward<Args>(aArgs)..., &rv);
  if (NS_FAILED(rv)) {
    return Err(rv);
  }
  return res;
}

}  // namespace mozilla

// TODO: Maybe move this to mfbt/ResultExtensions.h
#define MOZ_TO_RESULT(expr) ToResult(expr)

#define QM_TO_RESULT(expr) ToResult<QMResult>(expr)

#define QM_TO_RESULT_TRANSFORM(value) ToResultTransform<QMResult>(value)

#define MOZ_TO_RESULT_GET_TYPED(resultType, ...) \
  ::mozilla::ToResultGet<MOZ_REMOVE_PAREN(resultType)>(__VA_ARGS__)

#define MOZ_TO_RESULT_INVOKE_TYPED(resultType, ...) \
  ::mozilla::ToResultInvoke<MOZ_REMOVE_PAREN(resultType)>(__VA_ARGS__)

#define QM_TO_RESULT_INVOKE_MEMBER(obj, methodname, ...)                 \
  ::mozilla::ToResultInvokeMember<QMResult>(                             \
      (obj), &::mozilla::detail::DerefedType<decltype(obj)>::methodname, \
      ##__VA_ARGS__)

#define QM_TO_RESULT_INVOKE_MEMBER_TYPED(resultType, obj, methodname, ...) \
  (::mozilla::ToResultInvoke<resultType, QMResult>(                        \
      ::std::mem_fn(                                                       \
          &::mozilla::detail::DerefedType<decltype(obj)>::methodname),     \
      (obj), ##__VA_ARGS__))

#endif