xref: /aosp_15_r20/external/libtextclassifier/native/utils/base/status_macros.h (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_MACROS_H_
18 #define LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_MACROS_H_
19 
20 #include <utility>
21 
22 #include "utils/base/status.h"
23 #include "utils/base/statusor.h"
24 
25 namespace libtextclassifier3 {
26 
27 // An adapter to enable TC3_RETURN_IF_ERROR to be used with either Status or
28 // StatusOr.
29 class StatusAdapter {
30  public:
StatusAdapter(const Status & s)31   explicit StatusAdapter(const Status& s) : s_(s) {}
StatusAdapter(Status && s)32   explicit StatusAdapter(Status&& s) : s_(std::move(s)) {}
33   template <typename T>
StatusAdapter(const StatusOr<T> & s)34   explicit StatusAdapter(const StatusOr<T>& s) : s_(s.status()) {}
35   template <typename T>
StatusAdapter(StatusOr<T> && s)36   explicit StatusAdapter(StatusOr<T>&& s) : s_(std::move(s).status()) {}
37 
ok()38   bool ok() const { return s_.ok(); }
39   explicit operator bool() const { return ok(); }
40 
status()41   const Status& status() const& { return s_; }
status()42   Status status() && { return std::move(s_); }
43 
44  private:
45   Status s_;
46 };
47 
48 }  // namespace libtextclassifier3
49 
50 // Evaluates an expression that produces a `libtextclassifier3::Status`. If the
51 // status is not ok, returns it from the current function.
52 //
53 // For example:
54 //   libtextclassifier3::Status MultiStepFunction() {
55 //     TC3_RETURN_IF_ERROR(Function(args...));
56 //     TC3_RETURN_IF_ERROR(foo.Method(args...));
57 //     return libtextclassifier3::Status();
58 //   }
59 #define TC3_RETURN_IF_ERROR(expr) \
60   TC3_RETURN_IF_ERROR_INTERNAL(expr, std::move(adapter).status())
61 
62 #define TC3_RETURN_NULL_IF_ERROR(expr) \
63   TC3_RETURN_IF_ERROR_INTERNAL(expr, nullptr)
64 
65 #define TC3_RETURN_FALSE_IF_ERROR(expr) \
66   TC3_RETURN_IF_ERROR_INTERNAL(expr, false)
67 
68 #define TC3_RETURN_IF_ERROR_INTERNAL(expr, return_value)   \
69   TC3_STATUS_MACROS_IMPL_ELSE_BLOCKER_                     \
70   if (::libtextclassifier3::StatusAdapter adapter{expr}) { \
71   } else /* NOLINT */                                      \
72     return return_value
73 
74 // The GNU compiler emits a warning for code like:
75 //
76 //   if (foo)
77 //     if (bar) { } else baz;
78 //
79 // because it thinks you might want the else to bind to the first if.  This
80 // leads to problems with code like:
81 //
82 //   if (do_expr) TC3_RETURN_IF_ERROR(expr);
83 //
84 // The "switch (0) case 0:" idiom is used to suppress this.
85 #define TC3_STATUS_MACROS_IMPL_ELSE_BLOCKER_ \
86   switch (0)                                 \
87   case 0:                                    \
88   default:  // NOLINT
89 
90 #endif  // LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_MACROS_H_
91