| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 17 | // Result<T, E> is the type that is used to pass a success value of type T or an error code of type |
| 18 | // E, optionally together with an error message. T and E can be any type. If E is omitted it |
| 19 | // defaults to int, which is useful when errno(3) is used as the error code. |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 20 | // |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 21 | // Passing a success value or an error value: |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 22 | // |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 23 | // Result<std::string> readFile() { |
| 24 | // std::string content; |
| 25 | // if (base::ReadFileToString("path", &content)) { |
| 26 | // return content; // ok case |
| 27 | // } else { |
| 28 | // return ErrnoError() << "failed to read"; // error case |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 29 | // } |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 30 | // } |
| 31 | // |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 32 | // Checking the result and then unwrapping the value or propagating the error: |
| 33 | // |
| 34 | // Result<bool> hasAWord() { |
| 35 | // auto content = readFile(); |
| 36 | // if (!content.ok()) { |
| 37 | // return Error() << "failed to process: " << content.error(); |
| 38 | // } |
| 39 | // return (*content.find("happy") != std::string::npos); |
| 40 | // } |
| 41 | // |
| 42 | // Using custom error code type: |
| 43 | // |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 44 | // enum class MyError { A, B }; // assume that this is the error code you already have |
| 45 | // |
| 46 | // // To use the error code with Result, define a wrapper class that provides the following |
| 47 | // operations and use the wrapper class as the second type parameter (E) when instantiating |
| 48 | // Result<T, E> |
| 49 | // |
| 50 | // 1. default constructor |
| 51 | // 2. copy constructor / and move constructor if copying is expensive |
| 52 | // 3. conversion operator to the error code type |
| 53 | // 4. value() function that return the error code value |
| 54 | // 5. print() function that gives a string representation of the error ode value |
| 55 | // |
| 56 | // struct MyErrorWrapper { |
| 57 | // MyError val_; |
| 58 | // MyErrorWrapper() : val_(/* reasonable default value */) {} |
| 59 | // MyErrorWrapper(MyError&& e) : val_(std:forward<MyError>(e)) {} |
| 60 | // operator const MyError&() const { return val_; } |
| 61 | // MyError value() const { return val_; } |
| 62 | // std::string print() const { |
| 63 | // switch(val_) { |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 64 | // MyError::A: return "A"; |
| 65 | // MyError::B: return "B"; |
| 66 | // } |
| 67 | // } |
| 68 | // }; |
| 69 | // |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 70 | // #define NewMyError(e) Error<MyErrorWrapper>(MyError::e) |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 71 | // |
| 72 | // Result<T, MyError> val = NewMyError(A) << "some message"; |
| 73 | // |
| 74 | // Formatting the error message using fmtlib: |
| 75 | // |
| 76 | // Errorf("{} errors", num); // equivalent to Error() << num << " errors"; |
| 77 | // ErrnoErrorf("{} errors", num); // equivalent to ErrnoError() << num << " errors"; |
| 78 | // |
| 79 | // Returning success or failure, but not the value: |
| 80 | // |
| 81 | // Result<void> doSomething() { |
| 82 | // if (success) return {}; |
| 83 | // else return Error() << "error occurred"; |
| 84 | // } |
| 85 | // |
| 86 | // Extracting error code: |
| 87 | // |
| 88 | // Result<T> val = Error(3) << "some error occurred"; |
| 89 | // assert(3 == val.error().code()); |
| 90 | // |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 91 | |
| 92 | #pragma once |
| 93 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 94 | #include <assert.h> |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 95 | #include <errno.h> |
| 96 | |
| 97 | #include <sstream> |
| 98 | #include <string> |
| 99 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 100 | #include "android-base/errors.h" |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 101 | #include "android-base/expected.h" |
| Tom Cherry | 377d1ad | 2019-06-14 14:34:54 -0700 | [diff] [blame] | 102 | #include "android-base/format.h" |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 103 | |
| 104 | namespace android { |
| 105 | namespace base { |
| 106 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 107 | // Errno is a wrapper class for errno(3). Use this type instead of `int` when instantiating |
| 108 | // `Result<T, E>` and `Error<E>` template classes. This is required to distinguish errno from other |
| 109 | // integer-based error code types like `status_t`. |
| 110 | struct Errno { |
| 111 | Errno() : val_(0) {} |
| 112 | Errno(int e) : val_(e) {} |
| 113 | int value() const { return val_; } |
| 114 | operator int() const { return value(); } |
| 115 | std::string print() const { return strerror(value()); } |
| 116 | |
| 117 | int val_; |
| 118 | |
| 119 | // TODO(b/209929099): remove this conversion operator. This currently is needed to not break |
| 120 | // existing places where error().code() is used to construct enum values. |
| 121 | template <typename E, typename = std::enable_if_t<std::is_enum_v<E>>> |
| 122 | operator E() const { |
| 123 | return E(val_); |
| 124 | } |
| 125 | }; |
| 126 | |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 127 | template <typename E = Errno, bool include_message = true> |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 128 | struct ResultError { |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 129 | template <typename T, typename P, typename = std::enable_if_t<std::is_convertible_v<P, E>>> |
| 130 | ResultError(T&& message, P&& code) |
| 131 | : message_(std::forward<T>(message)), code_(E(std::forward<P>(code))) {} |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 132 | |
| 133 | template <typename T> |
| Maciej Żenczykowski | b39ecb4 | 2020-01-26 20:12:30 -0800 | [diff] [blame] | 134 | // NOLINTNEXTLINE(google-explicit-constructor) |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 135 | operator android::base::expected<T, ResultError<E>>() const { |
| 136 | return android::base::unexpected(ResultError<E>(message_, code_)); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | std::string message() const { return message_; } |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 140 | const E& code() const { return code_; } |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 141 | |
| 142 | private: |
| 143 | std::string message_; |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 144 | E code_; |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 145 | }; |
| 146 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 147 | template <typename E> |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 148 | struct ResultError<E, /* include_message */ false> { |
| 149 | template <typename P, typename = std::enable_if_t<std::is_convertible_v<P, E>>> |
| 150 | ResultError(P&& code) : code_(E(std::forward<P>(code))) {} |
| 151 | |
| 152 | template <typename T> |
| 153 | operator android::base::expected<T, ResultError<E, false>>() const { |
| 154 | return android::base::unexpected(ResultError<E, false>(code_)); |
| 155 | } |
| 156 | |
| 157 | const E& code() const { return code_; } |
| 158 | |
| 159 | private: |
| 160 | E code_; |
| 161 | }; |
| 162 | |
| 163 | template <typename E> |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 164 | inline bool operator==(const ResultError<E>& lhs, const ResultError<E>& rhs) { |
| Jiyong Park | bdf42dc | 2019-06-05 18:33:01 +0900 | [diff] [blame] | 165 | return lhs.message() == rhs.message() && lhs.code() == rhs.code(); |
| 166 | } |
| 167 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 168 | template <typename E> |
| 169 | inline bool operator!=(const ResultError<E>& lhs, const ResultError<E>& rhs) { |
| Jiyong Park | bdf42dc | 2019-06-05 18:33:01 +0900 | [diff] [blame] | 170 | return !(lhs == rhs); |
| 171 | } |
| 172 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 173 | template <typename E> |
| 174 | inline std::ostream& operator<<(std::ostream& os, const ResultError<E>& t) { |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 175 | os << t.message(); |
| 176 | return os; |
| 177 | } |
| 178 | |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 179 | namespace internal { |
| 180 | // Stream class that does nothing and is has zero (actually 1) size. It is used instead of |
| 181 | // std::stringstream when include_message is false so that we use less on stack. |
| 182 | // sizeof(std::stringstream) is 280 on arm64. |
| 183 | struct DoNothingStream { |
| 184 | template <typename T> |
| 185 | DoNothingStream& operator<<(T&&) { |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | std::string str() const { return ""; } |
| 190 | }; |
| 191 | } // namespace internal |
| 192 | |
| 193 | template <typename E = Errno, bool include_message = true, |
| 194 | typename = std::enable_if_t<!std::is_same_v<E, int>>> |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 195 | class Error { |
| 196 | public: |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 197 | Error() : code_(0), has_code_(false) {} |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 198 | template <typename P, typename = std::enable_if_t<std::is_convertible_v<P, E>>> |
| Maciej Żenczykowski | b39ecb4 | 2020-01-26 20:12:30 -0800 | [diff] [blame] | 199 | // NOLINTNEXTLINE(google-explicit-constructor) |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 200 | Error(P&& code) : code_(std::forward<P>(code)), has_code_(true) {} |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 201 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 202 | template <typename T, typename P, typename = std::enable_if_t<std::is_convertible_v<E, P>>> |
| Maciej Żenczykowski | b39ecb4 | 2020-01-26 20:12:30 -0800 | [diff] [blame] | 203 | // NOLINTNEXTLINE(google-explicit-constructor) |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 204 | operator android::base::expected<T, ResultError<P>>() const { |
| 205 | return android::base::unexpected(ResultError<P>(str(), static_cast<P>(code_))); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 206 | } |
| 207 | |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 208 | template <typename T, typename P, typename = std::enable_if_t<std::is_convertible_v<E, P>>> |
| 209 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 210 | operator android::base::expected<T, ResultError<P, false>>() const { |
| 211 | return android::base::unexpected(ResultError<P, false>(static_cast<P>(code_))); |
| 212 | } |
| 213 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 214 | template <typename T> |
| 215 | Error& operator<<(T&& t) { |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 216 | static_assert(include_message, "<< not supported when include_message = false"); |
| Maciej Żenczykowski | ce99a91 | 2020-04-24 11:21:21 -0700 | [diff] [blame] | 217 | // NOLINTNEXTLINE(bugprone-suspicious-semicolon) |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 218 | if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, ResultError<E>>) { |
| 219 | if (!has_code_) { |
| 220 | code_ = t.code(); |
| 221 | } |
| Jooyung Han | 072c714 | 2019-06-11 13:38:01 +0900 | [diff] [blame] | 222 | return (*this) << t.message(); |
| 223 | } |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 224 | int saved = errno; |
| 225 | ss_ << t; |
| 226 | errno = saved; |
| 227 | return *this; |
| 228 | } |
| 229 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 230 | const std::string str() const { |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 231 | static_assert(include_message, "str() not supported when include_message = false"); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 232 | std::string str = ss_.str(); |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 233 | if (has_code_) { |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 234 | if (str.empty()) { |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 235 | return code_.print(); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 236 | } |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 237 | return std::move(str) + ": " + code_.print(); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 238 | } |
| 239 | return str; |
| 240 | } |
| 241 | |
| 242 | Error(const Error&) = delete; |
| 243 | Error(Error&&) = delete; |
| 244 | Error& operator=(const Error&) = delete; |
| 245 | Error& operator=(Error&&) = delete; |
| 246 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 247 | template <typename T, typename... Args> |
| 248 | friend Error ErrorfImpl(const T&& fmt, const Args&... args); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 249 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 250 | template <typename T, typename... Args> |
| 251 | friend Error ErrnoErrorfImpl(const T&& fmt, const Args&... args); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 252 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 253 | private: |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 254 | Error(bool has_code, E code, const std::string& message) : code_(code), has_code_(has_code) { |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 255 | (*this) << message; |
| 256 | } |
| 257 | |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 258 | std::conditional_t<include_message, std::stringstream, internal::DoNothingStream> ss_; |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 259 | E code_; |
| 260 | const bool has_code_; |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 261 | }; |
| 262 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 263 | inline Error<Errno> ErrnoError() { |
| 264 | return Error<Errno>(Errno{errno}); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 265 | } |
| 266 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 267 | template <typename E> |
| 268 | inline E ErrorCode(E code) { |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 269 | return code; |
| 270 | } |
| 271 | |
| 272 | // Return the error code of the last ResultError object, if any. |
| 273 | // Otherwise, return `code` as it is. |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 274 | template <typename T, typename E, typename... Args> |
| 275 | inline E ErrorCode(E code, T&& t, const Args&... args) { |
| 276 | if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, ResultError<E>>) { |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 277 | return ErrorCode(t.code(), args...); |
| 278 | } |
| 279 | return ErrorCode(code, args...); |
| 280 | } |
| 281 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 282 | template <typename T, typename... Args> |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 283 | inline Error<Errno> ErrorfImpl(const T&& fmt, const Args&... args) { |
| 284 | return Error(false, ErrorCode(Errno{}, args...), fmt::format(fmt, args...)); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 285 | } |
| 286 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 287 | template <typename T, typename... Args> |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 288 | inline Error<Errno> ErrnoErrorfImpl(const T&& fmt, const Args&... args) { |
| 289 | return Error<Errno>(true, Errno{errno}, fmt::format(fmt, args...)); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 292 | #define Errorf(fmt, ...) android::base::ErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__) |
| 293 | #define ErrnoErrorf(fmt, ...) android::base::ErrnoErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__) |
| 294 | |
| Jiyong Park | 959593a | 2022-01-03 17:43:12 +0900 | [diff] [blame^] | 295 | template <typename T, typename E = Errno, bool include_message = true> |
| 296 | using Result = android::base::expected<T, ResultError<E, include_message>>; |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 297 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 298 | // Specialization of android::base::OkOrFail<V> for V = Result<T, E>. See android-base/errors.h |
| 299 | // for the contract. |
| 300 | template <typename T, typename E> |
| 301 | struct OkOrFail<Result<T, E>> { |
| 302 | typedef Result<T, E> V; |
| 303 | // Checks if V is ok or fail |
| 304 | static bool IsOk(const V& val) { return val.ok(); } |
| 305 | |
| 306 | // Turns V into a success value |
| 307 | static T Unwrap(V&& val) { return std::move(val.value()); } |
| 308 | |
| 309 | // Consumes V when it's a fail value |
| 310 | static OkOrFail<V> Fail(V&& v) { |
| 311 | assert(!IsOk(v)); |
| 312 | return OkOrFail<V>{std::move(v)}; |
| 313 | } |
| 314 | V val_; |
| 315 | |
| 316 | // Turns V into S (convertible from E) or Result<U, E> |
| 317 | template <typename S, typename = std::enable_if_t<std::is_convertible_v<E, S>>> |
| 318 | operator S() && { |
| 319 | return val_.error().code(); |
| 320 | } |
| 321 | template <typename U> |
| 322 | operator Result<U, E>() && { |
| 323 | return val_.error(); |
| 324 | } |
| 325 | |
| 326 | static std::string ErrorMessage(const V& val) { return val.error().message(); } |
| 327 | }; |
| 328 | |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 329 | // Macros for testing the results of functions that return android::base::Result. |
| 330 | // These also work with base::android::expected. |
| Yifan Hong | f21c546 | 2021-05-21 18:43:16 -0700 | [diff] [blame] | 331 | // For advanced matchers and customized error messages, see result-gtest.h. |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 332 | |
| Jiyong Park | c6efce1 | 2021-12-20 15:17:58 +0900 | [diff] [blame] | 333 | #define ASSERT_RESULT_OK(stmt) \ |
| 334 | if (const auto& tmp = (stmt); !tmp.ok()) \ |
| 335 | FAIL() << "Value of: " << #stmt << "\n" \ |
| 336 | << " Actual: " << tmp.error().message() << "\n" \ |
| 337 | << "Expected: is ok\n" |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 338 | |
| Jiyong Park | c6efce1 | 2021-12-20 15:17:58 +0900 | [diff] [blame] | 339 | #define EXPECT_RESULT_OK(stmt) \ |
| 340 | if (const auto& tmp = (stmt); !tmp.ok()) \ |
| 341 | ADD_FAILURE() << "Value of: " << #stmt << "\n" \ |
| 342 | << " Actual: " << tmp.error().message() << "\n" \ |
| 343 | << "Expected: is ok\n" |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 344 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 345 | } // namespace base |
| 346 | } // namespace android |