• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PRIVATE_JOIN_AND_COMPUTE_UTIL_STATUS_MACROS_H_
17 #define PRIVATE_JOIN_AND_COMPUTE_UTIL_STATUS_MACROS_H_
18 
19 #include "absl/base/port.h"
20 #include "absl/status/status.h"
21 #include "absl/status/statusor.h"
22 
23 // Helper macro that checks if the right hand side (rexpression) evaluates to a
24 // StatusOr with Status OK, and if so assigns the value to the value on the left
25 // hand side (lhs), otherwise returns the error status. Example:
26 //   ASSIGN_OR_RETURN(lhs, rexpression);
27 #ifndef ASSIGN_OR_RETURN
28 #define ASSIGN_OR_RETURN(lhs, rexpr)                                       \
29   PRIVATE_JOIN_AND_COMPUTE_ASSIGN_OR_RETURN_IMPL_(                         \
30       PRIVATE_JOIN_AND_COMPUTE_STATUS_MACROS_IMPL_CONCAT_(status_or_value, \
31                                                           __LINE__),       \
32       lhs, rexpr)
33 
34 // Internal helper.
35 #define PRIVATE_JOIN_AND_COMPUTE_ASSIGN_OR_RETURN_IMPL_(statusor, lhs, rexpr) \
36   auto statusor = (rexpr);                                                    \
37   if (ABSL_PREDICT_FALSE(!statusor.ok())) {                                   \
38     return std::move(statusor).status();                                      \
39   }                                                                           \
40   lhs = *std::move(statusor)
41 #endif  // ASSIGN_OR_RETURN
42 
43 // Helper macro that checks if the given expression evaluates to a
44 // Status with Status OK. If not,  returns the error status. Example:
45 //   RETURN_IF_ERROR(expression);
46 #ifndef RETURN_IF_ERROR
47 #define RETURN_IF_ERROR(expr)                                           \
48   PRIVATE_JOIN_AND_COMPUTE_RETURN_IF_ERROR_IMPL_(                       \
49       PRIVATE_JOIN_AND_COMPUTE_STATUS_MACROS_IMPL_CONCAT_(status_value, \
50                                                           __LINE__),    \
51       expr)
52 
53 // Internal helper.
54 #define PRIVATE_JOIN_AND_COMPUTE_RETURN_IF_ERROR_IMPL_(status, expr) \
55   auto status = (expr);                                              \
56   if (ABSL_PREDICT_FALSE(!status.ok())) {                            \
57     return status;                                                   \
58   }
59 #endif  // RETURN_IF_ERROR
60 
61 // Internal helper for concatenating macro values.
62 #define PRIVATE_JOIN_AND_COMPUTE_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
63 #define PRIVATE_JOIN_AND_COMPUTE_STATUS_MACROS_IMPL_CONCAT_(x, y) \
64   PRIVATE_JOIN_AND_COMPUTE_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y)
65 
66 #endif  // PRIVATE_JOIN_AND_COMPUTE_UTIL_STATUS_MACROS_H_
67