xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/status_macros.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
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 // This file is a custom fork of util/task/status_macros.h. This will become
16 // obsolete and will be replaced once Abseil releases absl::Status.
17 
18 #ifndef THIRD_PARTY_SAPI_UTIL_STATUS_MACROS_H_
19 #define THIRD_PARTY_SAPI_UTIL_STATUS_MACROS_H_
20 
21 #include <utility>
22 
23 #include "absl/base/optimization.h"
24 #include "absl/status/status.h"
25 
26 // Internal helper for concatenating macro values.
27 #define SAPI_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
28 #define SAPI_MACROS_IMPL_CONCAT(x, y) SAPI_MACROS_IMPL_CONCAT_INNER_(x, y)
29 
30 #define SAPI_RETURN_IF_ERROR(expr)                                           \
31   SAPI_RETURN_IF_ERROR_IMPL(SAPI_MACROS_IMPL_CONCAT(_sapi_status, __LINE__), \
32                             expr)
33 
34 #define SAPI_RETURN_IF_ERROR_IMPL(status, expr) \
35   do {                                          \
36     const auto status = (expr);                 \
37     if (ABSL_PREDICT_FALSE(!status.ok())) {     \
38       return status;                            \
39     }                                           \
40   } while (0)
41 
42 #define SAPI_ASSIGN_OR_RETURN(lhs, rexpr) \
43   SAPI_ASSIGN_OR_RETURN_IMPL(             \
44       SAPI_MACROS_IMPL_CONCAT(_sapi_statusor, __LINE__), lhs, rexpr)
45 
46 #define SAPI_ASSIGN_OR_RETURN_IMPL(statusor, lhs, rexpr) \
47   auto statusor = (rexpr);                               \
48   if (ABSL_PREDICT_FALSE(!statusor.ok())) {              \
49     return statusor.status();                            \
50   }                                                      \
51   lhs = std::move(statusor).value()
52 
53 #endif  // THIRD_PARTY_SAPI_UTIL_STATUS_MACROS_H_
54