| 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 | |
| 127 | template <typename E = Errno> |
| 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> |
| 148 | inline bool operator==(const ResultError<E>& lhs, const ResultError<E>& rhs) { |
| Jiyong Park | bdf42dc | 2019-06-05 18:33:01 +0900 | [diff] [blame] | 149 | return lhs.message() == rhs.message() && lhs.code() == rhs.code(); |
| 150 | } |
| 151 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 152 | template <typename E> |
| 153 | inline bool operator!=(const ResultError<E>& lhs, const ResultError<E>& rhs) { |
| Jiyong Park | bdf42dc | 2019-06-05 18:33:01 +0900 | [diff] [blame] | 154 | return !(lhs == rhs); |
| 155 | } |
| 156 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 157 | template <typename E> |
| 158 | inline std::ostream& operator<<(std::ostream& os, const ResultError<E>& t) { |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 159 | os << t.message(); |
| 160 | return os; |
| 161 | } |
| 162 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 163 | template <typename E = Errno, typename = std::enable_if_t<!std::is_same_v<E, int>>> |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 164 | class Error { |
| 165 | public: |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 166 | Error() : code_(0), has_code_(false) {} |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 167 | 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] | 168 | // NOLINTNEXTLINE(google-explicit-constructor) |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 169 | Error(P&& code) : code_(std::forward<P>(code)), has_code_(true) {} |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 170 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 171 | 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] | 172 | // NOLINTNEXTLINE(google-explicit-constructor) |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 173 | operator android::base::expected<T, ResultError<P>>() const { |
| 174 | return android::base::unexpected(ResultError<P>(str(), static_cast<P>(code_))); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | template <typename T> |
| 178 | Error& operator<<(T&& t) { |
| Maciej Żenczykowski | ce99a91 | 2020-04-24 11:21:21 -0700 | [diff] [blame] | 179 | // NOLINTNEXTLINE(bugprone-suspicious-semicolon) |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 180 | if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, ResultError<E>>) { |
| 181 | if (!has_code_) { |
| 182 | code_ = t.code(); |
| 183 | } |
| Jooyung Han | 072c714 | 2019-06-11 13:38:01 +0900 | [diff] [blame] | 184 | return (*this) << t.message(); |
| 185 | } |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 186 | int saved = errno; |
| 187 | ss_ << t; |
| 188 | errno = saved; |
| 189 | return *this; |
| 190 | } |
| 191 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 192 | const std::string str() const { |
| 193 | std::string str = ss_.str(); |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 194 | if (has_code_) { |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 195 | if (str.empty()) { |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 196 | return code_.print(); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 197 | } |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 198 | return std::move(str) + ": " + code_.print(); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 199 | } |
| 200 | return str; |
| 201 | } |
| 202 | |
| 203 | Error(const Error&) = delete; |
| 204 | Error(Error&&) = delete; |
| 205 | Error& operator=(const Error&) = delete; |
| 206 | Error& operator=(Error&&) = delete; |
| 207 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 208 | template <typename T, typename... Args> |
| 209 | friend Error ErrorfImpl(const T&& fmt, const Args&... args); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 210 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 211 | template <typename T, typename... Args> |
| 212 | friend Error ErrnoErrorfImpl(const T&& fmt, const Args&... args); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 213 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 214 | private: |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 215 | 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] | 216 | (*this) << message; |
| 217 | } |
| 218 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 219 | std::stringstream ss_; |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 220 | E code_; |
| 221 | const bool has_code_; |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 222 | }; |
| 223 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 224 | inline Error<Errno> ErrnoError() { |
| 225 | return Error<Errno>(Errno{errno}); |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 228 | template <typename E> |
| 229 | inline E ErrorCode(E code) { |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 230 | return code; |
| 231 | } |
| 232 | |
| 233 | // Return the error code of the last ResultError object, if any. |
| 234 | // Otherwise, return `code` as it is. |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 235 | template <typename T, typename E, typename... Args> |
| 236 | inline E ErrorCode(E code, T&& t, const Args&... args) { |
| 237 | 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] | 238 | return ErrorCode(t.code(), args...); |
| 239 | } |
| 240 | return ErrorCode(code, args...); |
| 241 | } |
| 242 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 243 | template <typename T, typename... Args> |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 244 | inline Error<Errno> ErrorfImpl(const T&& fmt, const Args&... args) { |
| 245 | return Error(false, ErrorCode(Errno{}, args...), fmt::format(fmt, args...)); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 246 | } |
| 247 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 248 | template <typename T, typename... Args> |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 249 | inline Error<Errno> ErrnoErrorfImpl(const T&& fmt, const Args&... args) { |
| 250 | return Error<Errno>(true, Errno{errno}, fmt::format(fmt, args...)); |
| Jiyong Park | 4e223e6 | 2019-06-12 16:25:04 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| Tom Cherry | 81f5f50 | 2020-02-04 15:18:29 -0800 | [diff] [blame] | 253 | #define Errorf(fmt, ...) android::base::ErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__) |
| 254 | #define ErrnoErrorf(fmt, ...) android::base::ErrnoErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__) |
| 255 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 256 | template <typename T, typename E = Errno> |
| Jiyong Park | f7bb3e8 | 2021-12-10 22:46:25 +0900 | [diff] [blame] | 257 | using Result = android::base::expected<T, ResultError<E>>; |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 258 | |
| Jiyong Park | 7706f49 | 2021-12-14 22:33:38 +0900 | [diff] [blame] | 259 | // Specialization of android::base::OkOrFail<V> for V = Result<T, E>. See android-base/errors.h |
| 260 | // for the contract. |
| 261 | template <typename T, typename E> |
| 262 | struct OkOrFail<Result<T, E>> { |
| 263 | typedef Result<T, E> V; |
| 264 | // Checks if V is ok or fail |
| 265 | static bool IsOk(const V& val) { return val.ok(); } |
| 266 | |
| 267 | // Turns V into a success value |
| 268 | static T Unwrap(V&& val) { return std::move(val.value()); } |
| 269 | |
| 270 | // Consumes V when it's a fail value |
| 271 | static OkOrFail<V> Fail(V&& v) { |
| 272 | assert(!IsOk(v)); |
| 273 | return OkOrFail<V>{std::move(v)}; |
| 274 | } |
| 275 | V val_; |
| 276 | |
| 277 | // Turns V into S (convertible from E) or Result<U, E> |
| 278 | template <typename S, typename = std::enable_if_t<std::is_convertible_v<E, S>>> |
| 279 | operator S() && { |
| 280 | return val_.error().code(); |
| 281 | } |
| 282 | template <typename U> |
| 283 | operator Result<U, E>() && { |
| 284 | return val_.error(); |
| 285 | } |
| 286 | |
| 287 | static std::string ErrorMessage(const V& val) { return val.error().message(); } |
| 288 | }; |
| 289 | |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 290 | // Macros for testing the results of functions that return android::base::Result. |
| 291 | // These also work with base::android::expected. |
| Yifan Hong | f21c546 | 2021-05-21 18:43:16 -0700 | [diff] [blame] | 292 | // For advanced matchers and customized error messages, see result-gtest.h. |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 293 | |
| Jiyong Park | c6efce1 | 2021-12-20 15:17:58 +0900 | [diff] [blame^] | 294 | #define ASSERT_RESULT_OK(stmt) \ |
| 295 | if (const auto& tmp = (stmt); !tmp.ok()) \ |
| 296 | FAIL() << "Value of: " << #stmt << "\n" \ |
| 297 | << " Actual: " << tmp.error().message() << "\n" \ |
| 298 | << "Expected: is ok\n" |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 299 | |
| Jiyong Park | c6efce1 | 2021-12-20 15:17:58 +0900 | [diff] [blame^] | 300 | #define EXPECT_RESULT_OK(stmt) \ |
| 301 | if (const auto& tmp = (stmt); !tmp.ok()) \ |
| 302 | ADD_FAILURE() << "Value of: " << #stmt << "\n" \ |
| 303 | << " Actual: " << tmp.error().message() << "\n" \ |
| 304 | << "Expected: is ok\n" |
| Bernie Innocenti | 9cc7edc | 2020-02-06 02:51:42 +0900 | [diff] [blame] | 305 | |
| Jiyong Park | ab7dc5a | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 306 | } // namespace base |
| 307 | } // namespace android |