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_STATUS_MACROS_IMPL_ELSE_BLOCKER_ \ 61 if (::libtextclassifier3::StatusAdapter adapter{expr}) { \ 62 } else /* NOLINT */ \ 63 return std::move(adapter).status() 64 65 // The GNU compiler emits a warning for code like: 66 // 67 // if (foo) 68 // if (bar) { } else baz; 69 // 70 // because it thinks you might want the else to bind to the first if. This 71 // leads to problems with code like: 72 // 73 // if (do_expr) TC3_RETURN_IF_ERROR(expr); 74 // 75 // The "switch (0) case 0:" idiom is used to suppress this. 76 #define TC3_STATUS_MACROS_IMPL_ELSE_BLOCKER_ \ 77 switch (0) \ 78 case 0: \ 79 default: // NOLINT 80 81 #endif // LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_MACROS_H_ 82