Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +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 | |
| 17 | #ifndef NETUTILS_STATUS_H |
| 18 | #define NETUTILS_STATUS_H |
| 19 | |
| 20 | #include <cassert> |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 21 | #include <limits> |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 22 | #include <ostream> |
| 23 | |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame^] | 24 | #include <android-base/result.h> |
| 25 | |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace netdutils { |
| 28 | |
| 29 | // Simple status implementation suitable for use on the stack in low |
| 30 | // or moderate performance code. This can definitely be improved but |
| 31 | // for now short string optimization is expected to keep the common |
| 32 | // success case fast. |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 33 | // |
| 34 | // Status is implicitly movable via the default noexcept move constructor |
| 35 | // and noexcept move-assignment operator. |
| 36 | class [[nodiscard]] Status { |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 37 | public: |
| 38 | Status() = default; |
Jonathan Basseri | 0e9d5fa | 2017-08-22 11:39:17 -0700 | [diff] [blame] | 39 | explicit Status(int code) : mCode(code) {} |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 40 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 41 | // Constructs an error Status, |code| must be non-zero. |
| 42 | Status(int code, std::string msg) : mCode(code), mMsg(std::move(msg)) { assert(!ok()); } |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 43 | |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame^] | 44 | Status(android::base::Result<void> result) |
| 45 | : mCode(result ? 0 : result.error().code()), mMsg(result ? "" : result.error().message()) {} |
| 46 | |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 47 | int code() const { return mCode; } |
| 48 | |
| 49 | bool ok() const { return code() == 0; } |
| 50 | |
| 51 | const std::string& msg() const { return mMsg; } |
| 52 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 53 | // Explicitly ignores the Status without triggering [[nodiscard]] errors. |
| 54 | void ignoreError() const {} |
| 55 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 56 | bool operator==(const Status& other) const { return code() == other.code(); } |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 57 | bool operator!=(const Status& other) const { return !(*this == other); } |
| 58 | |
| 59 | private: |
| 60 | int mCode = 0; |
| 61 | std::string mMsg; |
| 62 | }; |
| 63 | |
| 64 | namespace status { |
| 65 | |
| 66 | const Status ok{0}; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 67 | // EOF is not part of errno space, we'll place it far above the |
| 68 | // highest existing value. |
| 69 | const Status eof{0x10001, "end of file"}; |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 70 | const Status undefined{std::numeric_limits<int>::max(), "undefined"}; |
| 71 | |
| 72 | } // namespace status |
| 73 | |
| 74 | // Return true if status is "OK". This is sometimes preferable to |
| 75 | // status.ok() when we want to check the state of Status-like objects |
| 76 | // that implicitly cast to Status. |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 77 | inline bool isOk(const Status& status) { |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 78 | return status.ok(); |
| 79 | } |
| 80 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 81 | // For use only in tests. |
| 82 | #define EXPECT_OK(status) EXPECT_TRUE((status).ok()) |
| 83 | |
| 84 | // Documents that status is expected to be ok. This function may log |
| 85 | // (or assert when running in debug mode) if status has an unexpected value. |
| 86 | inline void expectOk(const Status& /*status*/) { |
| 87 | // TODO: put something here, for now this function serves solely as documentation. |
| 88 | } |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 89 | |
| 90 | // Convert POSIX errno to a Status object. |
| 91 | // If Status is extended to have more features, this mapping may |
| 92 | // become more complex. |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 93 | Status statusFromErrno(int err, const std::string& msg); |
| 94 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 95 | // Helper that checks Status-like object (notably StatusOr) against a |
| 96 | // value in the errno space. |
| 97 | bool equalToErrno(const Status& status, int err); |
| 98 | |
| 99 | // Helper that converts Status-like object (notably StatusOr) to a |
| 100 | // message. |
| 101 | std::string toString(const Status& status); |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 102 | |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 103 | std::ostream& operator<<(std::ostream& os, const Status& s); |
| 104 | |
Jonathan Basseri | 774f006 | 2017-08-22 10:40:27 -0700 | [diff] [blame] | 105 | // Evaluate 'stmt' to a Status object and if it results in an error, return that |
| 106 | // error. Use 'tmp' as a variable name to avoid shadowing any variables named |
| 107 | // tmp. |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 108 | #define RETURN_IF_NOT_OK_IMPL(tmp, stmt) \ |
| 109 | do { \ |
| 110 | ::android::netdutils::Status tmp = (stmt); \ |
| 111 | if (!isOk(tmp)) { \ |
| 112 | return tmp; \ |
| 113 | } \ |
| 114 | } while (false) |
| 115 | |
Jonathan Basseri | 774f006 | 2017-08-22 10:40:27 -0700 | [diff] [blame] | 116 | // Create a unique variable name to avoid shadowing local variables. |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 117 | #define RETURN_IF_NOT_OK_CONCAT(line, stmt) RETURN_IF_NOT_OK_IMPL(__CONCAT(_status_, line), stmt) |
| 118 | |
| 119 | // Macro to allow exception-like handling of error return values. |
| 120 | // |
| 121 | // If the evaluation of stmt results in an error, return that error |
| 122 | // from current function. |
| 123 | // |
| 124 | // Example usage: |
| 125 | // Status bar() { ... } |
| 126 | // |
| 127 | // RETURN_IF_NOT_OK(status); |
| 128 | // RETURN_IF_NOT_OK(bar()); |
| 129 | #define RETURN_IF_NOT_OK(stmt) RETURN_IF_NOT_OK_CONCAT(__LINE__, stmt) |
| 130 | |
| 131 | } // namespace netdutils |
| 132 | } // namespace android |
| 133 | |
| 134 | #endif /* NETUTILS_STATUS_H */ |