1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // From: util/task/contrib/status_macros/status_macros.h
9
10 #ifndef GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_
11 #define GOOGLE_PROTOBUF_STUBS_STATUS_MACROS_H_
12
13 #include "absl/status/status.h"
14 #include "absl/status/statusor.h"
15 #include "google/protobuf/stubs/common.h"
16
17 // Needs to be last.
18 #include "google/protobuf/port_def.inc" // NOLINT
19
20 namespace google {
21 namespace protobuf {
22 namespace util {
23
24 // Run a command that returns a util::Status. If the called code returns an
25 // error status, return that status up out of this method too.
26 //
27 // Example:
28 // RETURN_IF_ERROR(DoThings(4));
29 #define RETURN_IF_ERROR(expr) \
30 do { \
31 /* Using _status below to avoid capture problems if expr is "status". */ \
32 const absl::Status _status = (expr); \
33 if (PROTOBUF_PREDICT_FALSE(!_status.ok())) return _status; \
34 } while (0)
35
36 // Internal helper for concatenating macro values.
37 #define STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y
38 #define STATUS_MACROS_CONCAT_NAME(x, y) STATUS_MACROS_CONCAT_NAME_INNER(x, y)
39
40 template <typename T>
DoAssignOrReturn(T & lhs,absl::StatusOr<T> result)41 absl::Status DoAssignOrReturn(T& lhs, absl::StatusOr<T> result) {
42 if (result.ok()) {
43 lhs = result.value();
44 }
45 return result.status();
46 }
47
48 #define ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \
49 absl::Status status = DoAssignOrReturn(lhs, (rexpr)); \
50 if (PROTOBUF_PREDICT_FALSE(!status.ok())) return status;
51
52 // Executes an expression that returns a util::StatusOr, extracting its value
53 // into the variable defined by lhs (or returning on error).
54 //
55 // Example: Assigning to an existing value
56 // ValueType value;
57 // ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
58 //
59 // WARNING: ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
60 // in a single statement (e.g. as the body of an if statement without {})!
61 #define ASSIGN_OR_RETURN(lhs, rexpr) \
62 ASSIGN_OR_RETURN_IMPL( \
63 STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, rexpr);
64
65 } // namespace util
66 } // namespace protobuf
67 } // namespace google
68
69 #include "google/protobuf/port_undef.inc" // NOLINT
70
71 #endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_
72