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