File: expected_macros.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (409 lines) | stat: -rw-r--r-- 16,184 bytes parent folder | download | duplicates (3)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_TYPES_EXPECTED_MACROS_H_
#define BASE_TYPES_EXPECTED_MACROS_H_

#include <concepts>
#include <functional>
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>

#include "base/compiler_specific.h"
#include "base/macros/concat.h"
#include "base/macros/if.h"
#include "base/macros/is_empty.h"
#include "base/macros/remove_parens.h"
#include "base/macros/uniquify.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/types/expected.h"
#include "base/types/is_instantiation.h"

// Executes an expression `rexpr` that returns an `expected<T, E>` or
// `std::optional<T>`.
//
// For the `expected<T, E>` case:
//   If the result is an error, causes the calling function to return. If no
//   additional arguments are given, the function return value is the `E`
//   returned by `rexpr`. Otherwise, the additional arguments are treated as an
//   invocable that expects an E as its last argument and returns some type
//   (including `void`) convertible to the function's return type; that is, the
//   function returns the result of `std::invoke(..., E)` on the additional
//   arguments.
//
//   This works with move-only types and can be used in functions that return
//   either an `E` directly or a `base::expected<U, E>`, without needing to
//   explicitly wrap the return in `base::unexpected`.
//
// For the `std::optional<T>` case:
//   If the result is `std::nullopt`, causes the calling function to return. If
//   no additional arguments are given, the function return value is the return
//   value of `rexpr` (i.e. an unbound `std::optional<T>`). Otherwise, the
//   additional arguments are treated as an invocable that returns some type
//   (including `void`) convertible to the function's return type; that is, the
//   function returns the result of `std::invoke(...)` on the additional
//   arguments.
//
// # Interface
//
// `RETURN_IF_ERROR(rexpr, ...);`
//
// # Examples for the `expected<T, E>` case
//
// Use with no additional arguments:
// ```
//   SomeErrorCode Foo() {
//     RETURN_IF_ERROR(Function(args...));
//     return SomeErrorCode::kOK;
//   }
// ```
// ```
//   base::expected<int, SomeErrorCode> Bar() {
//     RETURN_IF_ERROR(Function(args...));
//     RETURN_IF_ERROR(obj.Method(args...));
//     return 17;
//   }
// ```
//
// Adjusting the returned error:
// ```
//   RETURN_IF_ERROR(TryProcessing(query),
//                   [](const auto& e) {
//                     return base::StrCat({e, " while processing query"});
//                   });
// ```
//
// Returning a different kind of error:
// ```
//   RETURN_IF_ERROR(TryProcessing(query),
//                   [](auto) { return SomeErrorCode::kFail); });
// ```
//
// Returning void:
// ```
//   RETURN_IF_ERROR(TryProcessing(query), [](auto) {});
// ```
// ```
//   RETURN_IF_ERROR(TryProcessing(query),
//                   [](auto) { LOG(WARNING) << "Uh oh"; }());
// ```
//
// Automatic conversion to `base::expected<U, E>`:
// ```
//   base::expected<int, SomeErrorCode> Foo() {
//     RETURN_IF_ERROR(TryProcessing(query),
//                     [](auto) { return SomeErrorCode::kFail); });
//     return 17;
//   }
// ```
//
// Passing the error to a static/global handler:
// ```
//   RETURN_IF_ERROR(TryProcessing(query), &FailureHandler);
// ```
//
// Passing the error to a handler member function:
// ```
//   RETURN_IF_ERROR(TryProcessing(query), &MyClass::FailureHandler, this);
// ```
//
// # Modified examples for the `std::optional<T>` case
//
// Use with no additional arguments:
// ```
//   std::optional<int> Foo() {
//     RETURN_IF_ERROR(Function(args...));
//     RETURN_IF_ERROR(obj.Method(args...));
//     return 17;
//   }
// ```
//
// Returning some kind of error:
// ```
//   RETURN_IF_ERROR(TryProcessing(query),
//                   [] { return SomeErrorCode::kFail); });
// ```
//
// Returning void:
// ```
//   RETURN_IF_ERROR(TryProcessing(query), [] {});
// ```
// ```
//   RETURN_IF_ERROR(TryProcessing(query), [] { LOG(WARNING) << "Uh oh"; }());
// ```
#define RETURN_IF_ERROR(rexpr, ...)                                        \
  BASE_INTERNAL_EXPECTED_PASS_ARGS(BASE_INTERNAL_EXPECTED_RETURN_IF_ERROR, \
                                   BASE_UNIQUIFY(_expected_value), rexpr,  \
                                   __VA_ARGS__)

// Executes an expression `rexpr` that returns an `expected<T, E>` or
// `std::optional<T>`. If the result is not an error/`std::nullopt`
// (respectively), moves the `T` into whatever `lhs` defines/refers to;
// otherwise, behaves like RETURN_IF_ERROR() above. Avoid side effects in `lhs`,
// as it will not be evaluated in the error case.
//
// # Interface
//
// `ASSIGN_OR_RETURN(lhs, rexpr, ...);`
//
// WARNING: If `lhs` is parenthesized, the parentheses are removed; for this
//          reason, `lhs` may not contain a ternary (`?:`). See examples for
//          motivation.
//
// WARNING: Expands into multiple statements; cannot be used in a single
//          statement (e.g. as the body of an `if` statement without `{}`)!
//
// # Examples for the `expected<T, E>` case
//
// Declaring and initializing a new variable (ValueType can be anything that can
// be initialized with assignment):
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(arg));
// ```
//
// Assigning to an existing variable:
// ```
//   ValueType value;
//   ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
// ```
//
// Initializing a `std::unique_ptr`:
// ```
//   ASSIGN_OR_RETURN(std::unique_ptr<T> ptr, MaybeGetPtr(arg));
// ```
//
// Initializing a map. Because of C++ preprocessor limitations, the type used in
// `ASSIGN_OR_RETURN` cannot contain commas, so wrap `lhs` in parentheses:
// ```
//   ASSIGN_OR_RETURN((flat_map<Foo, Bar> my_map), GetMap());
// ```
// Or use `auto` if the type is obvious enough:
// ```
//   ASSIGN_OR_RETURN(auto code_widget, GetCodeWidget());
// ```
//
// Assigning to structured bindings. The same situation with comma as above, so
// wrap `lhs` in parentheses:
// ```
//   ASSIGN_OR_RETURN((auto [first, second]), GetPair());
// ```
//
// Attempting to assign to a ternary will not compile:
// ```
//   ASSIGN_OR_RETURN((cond ? a : b), MaybeGetValue(arg));  // DOES NOT COMPILE
// ```
//
// Adjusting the returned error:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                    [](const auto& e) {
//                      return base::StrCat({e, " while getting value"});
//                    });
// ```
//
// Returning a different kind of error:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                    [](auto) { return SomeErrorCode::kFail); });
// ```
//
// Returning void:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query), [](auto) {});
// ```
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                    [](auto) { LOG(WARNING) << "Uh oh"; }());
// ```
//
// Automatic conversion to `base::expected<U, E>`:
// ```
//   base::expected<int, SomeErrorCode> Foo() {
//     ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                      [](auto) { return SomeErrorCode::kFail); });
//     return 17;
//   }
// ```
//
// Passing the error to a static/global handler:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query), &FailureHandler);
// ```
//
// Passing the error to a handler member function:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                    &MyClass::FailureHandler, this);
// ```
//
// # Modified examples for the `std::optional<T>` case
//
// Returning some kind of error:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                    [] { return SomeErrorCode::kFail); });
// ```
//
// Returning void:
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query), [] {});
// ```
// ```
//   ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(query),
//                    [] { LOG(WARNING) << "Uh oh"; }());
// ```
#define ASSIGN_OR_RETURN(lhs, rexpr, ...)                                      \
  BASE_INTERNAL_EXPECTED_PASS_ARGS(BASE_INTERNAL_EXPECTED_ASSIGN_OR_RETURN,    \
                                   lhs, BASE_UNIQUIFY(_expected_value), rexpr, \
                                   __VA_ARGS__)

namespace base::internal {

// =================================================================
// == Implementation details, do not rely on anything below here. ==
// =================================================================

// Helper object to allow returning some `E` from a method either directly or in
// the error of an `expected<T, E>`. Supports move-only `E`, as well as `void`.
//
// In order to support `void` return types, `UnexpectedDeducer` is not
// constructed directly with an `E`, but with a lambda that returns `E`; and
// callers must return `Ret()` rather than returning the deducer itself. Using
// both these indirections allows consistent invocation from macros.
template <typename Lambda,
          typename Arg,
          typename E = std::invoke_result_t<Lambda&&, Arg&&>>
class UnexpectedDeducer {
 public:
  constexpr UnexpectedDeducer(Lambda&& lambda, Arg&& arg) noexcept
      : lambda_(std::move(lambda)), arg_(std::move(arg)) {}

  constexpr decltype(auto) Ret() && noexcept {
    if constexpr (std::is_void_v<E>) {
      std::move(lambda_)(std::move(arg_));
    } else {
      return std::move(*this);
    }
  }

  // Allow implicit conversion from `Ret()` to either `expected<T, E>` (for
  // arbitrary `T`) or `E`.
  template <typename T>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr operator expected<T, E>() && noexcept
    requires(!std::is_void_v<E>)
  {
    return expected<T, E>(unexpect, std::move(lambda_)(std::move(arg_)));
  }
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr operator E() && noexcept
    requires(!std::is_void_v<E>)
  {
    return std::move(lambda_)(std::move(arg_));
  }

  // Disallow implicit conversion to `std::optional<T>`. Either `E` is already
  // a type that can convert to this and this is unnecessary due to the
  // conversion operator above, or `E` is some other type and we're discarding
  // whatever was in it. Theoretically this might not be an information loss if
  // e.g. `E` is an unbound `std::optional<U>`, but it seems better to force
  // people to match types in this case. Also note that since `E` isn't
  // convertible, this would be a compile error even without deleting this
  // function; but deleting it makes it clear this isn't an omission in this
  // code, but behavior we explicitly don't want to support.
  template <typename T>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr operator std::optional<T>() && noexcept
    requires(!std::is_void_v<E> && !std::convertible_to<E, std::optional<T>>)
  = delete;  // Use an adapter that returns this type.

 private:
  // RAW_PTR_EXCLUSION: Not intended to handle &&-qualified members.
  // `UnexpectedDeducer` is a short-lived temporary and tries to minimize
  // copying and other overhead; using raw_ptr/ref goes against this design
  // without adding meaningful safety.
  RAW_PTR_EXCLUSION Lambda&& lambda_;
  RAW_PTR_EXCLUSION Arg&& arg_;
};

// Deduce the type of the lambda automatically so callers don't need to spell
// things twice (or use temps) and use decltype.
template <typename Lambda, typename Arg>
UnexpectedDeducer(Lambda, Arg) -> UnexpectedDeducer<Lambda, Arg>;

// Workaround for https://github.com/llvm/llvm-project/issues/58872: Indirect
// through an extra layer so if the compiler attempts to instantiate both arms
// of the constexpr if in `BASE_INTERNAL_EXPECTED_BODY`, it will succeed.
// TODO(https://github.com/llvm/llvm-project/issues/58872): Remove this struct
// and the constructions of it below, and let them invoke `__VA_ARGS__`
// directly.
struct Trampoline {
  template <typename... Args>
  constexpr auto operator()(Args&&... args) const noexcept {
    // Should always succeed if this is actually reached at runtime.
    if constexpr (std::is_invocable_v<Args&&...>) {
      return std::invoke(std::forward<Args>(args)...);
    }
  }
};

}  // namespace base::internal

#define BASE_INTERNAL_EXPECTED_PASS_ARGS(func, ...) func(__VA_ARGS__)

#define BASE_INTERNAL_EXPECTED_BODY(expected, rexpr, name, ...)               \
  auto expected = (rexpr);                                                    \
  {                                                                           \
    static_assert(                                                            \
        base::internal::IsExpected<decltype(expected)> ||                     \
            base::is_instantiation<decltype(expected), std::optional>,        \
        #name                                                                 \
        " should only be used with base::expected<> or std::optional<>");     \
  }                                                                           \
  if (!expected.has_value()) [[unlikely]] {                                   \
    /* Pass `expected` as an arg rather than capturing, so the lambda body */ \
    /* is a template context, so `constexpr if` avoids instantiating the */   \
    /* non-matching arm, since it won't compile otherwise. */                 \
    return base::internal::UnexpectedDeducer(                                 \
               [&](auto&& base_internal_expected__) {                         \
                 if constexpr (base::internal::IsExpected<                    \
                                   decltype(base_internal_expected__)>) {     \
                   return BASE_IF(                                            \
                       BASE_IS_EMPTY(__VA_ARGS__),                            \
                       std::move(base_internal_expected__).error(),           \
                       std::invoke(                                           \
                           base::internal::Trampoline(), __VA_ARGS__,         \
                           std::move(base_internal_expected__).error()));     \
                 } else {                                                     \
                   return BASE_IF(BASE_IS_EMPTY(__VA_ARGS__),                 \
                                  std::move(base_internal_expected__),        \
                                  std::invoke(base::internal::Trampoline(),   \
                                              __VA_ARGS__));                  \
                 }                                                            \
               },                                                             \
               std::move(expected))                                           \
        .Ret();                                                               \
  }

#define BASE_INTERNAL_EXPECTED_RETURN_IF_ERROR(expected, rexpr, ...) \
  do {                                                               \
    BASE_INTERNAL_EXPECTED_BODY(expected, rexpr, RETURN_IF_ERROR,    \
                                __VA_ARGS__);                        \
  } while (false)

#define BASE_INTERNAL_EXPECTED_ASSIGN_OR_RETURN(lhs, expected, rexpr, ...)     \
  {                                                                            \
    constexpr auto lhs_v = std::string_view(#lhs);                             \
    static_assert(!(lhs_v.front() == '(' && lhs_v.back() == ')' &&             \
                    lhs_v.rfind('?') != std::string_view::npos),               \
                  "Identified possible ternary in `lhs`; avoid passing "       \
                  "parenthesized expressions containing '?' to the first "     \
                  "argument of ASSIGN_OR_RETURN()");                           \
  }                                                                            \
  BASE_INTERNAL_EXPECTED_BODY(expected, rexpr, ASSIGN_OR_RETURN, __VA_ARGS__); \
  BASE_REMOVE_PARENS(lhs) = std::move(expected).value();

#endif  // BASE_TYPES_EXPECTED_MACROS_H_