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_STATUSOR_H |
| 18 | #define NETUTILS_STATUSOR_H |
| 19 | |
| 20 | #include <cassert> |
| 21 | #include "netdutils/Status.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace netdutils { |
| 25 | |
| 26 | // Wrapper around a combination of Status and application value type. |
| 27 | // T may be any copyable or movable type. |
| 28 | template <typename T> |
Bernie Innocenti | 0bdee43 | 2018-10-12 22:24:33 +0900 | [diff] [blame^] | 29 | class [[nodiscard]] StatusOr { |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 30 | public: |
| 31 | StatusOr() = default; |
| 32 | StatusOr(const Status status) : mStatus(status) { assert(!isOk(status)); } |
| 33 | StatusOr(const T& value) : mStatus(status::ok), mValue(value) {} |
| 34 | StatusOr(T&& value) : mStatus(status::ok), mValue(std::move(value)) {} |
| 35 | |
| 36 | // Move constructor ok (if T supports move) |
Chih-Hung Hsieh | 59519ea | 2018-09-25 13:58:40 -0700 | [diff] [blame] | 37 | StatusOr(StatusOr&&) noexcept = default; |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 38 | // Move assignment ok (if T supports move) |
Chih-Hung Hsieh | 59519ea | 2018-09-25 13:58:40 -0700 | [diff] [blame] | 39 | StatusOr& operator=(StatusOr&&) noexcept = default; |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 40 | // Copy constructor ok (if T supports copy) |
| 41 | StatusOr(const StatusOr&) = default; |
| 42 | // Copy assignment ok (if T supports copy) |
| 43 | StatusOr& operator=(const StatusOr&) = default; |
| 44 | |
| 45 | // Return const references to wrapped type |
| 46 | // It is an error to call value() when !isOk(status()) |
| 47 | const T& value() const & { return mValue; } |
| 48 | const T&& value() const && { return mValue; } |
| 49 | |
| 50 | // Return rvalue references to wrapped type |
| 51 | // It is an error to call value() when !isOk(status()) |
| 52 | T& value() & { return mValue; } |
| 53 | T&& value() && { return mValue; } |
| 54 | |
| 55 | // Return status assigned in constructor |
| 56 | const Status status() const { return mStatus; } |
| 57 | |
Bernie Innocenti | 0bdee43 | 2018-10-12 22:24:33 +0900 | [diff] [blame^] | 58 | // Explicitly ignores the Status without triggering [[nodiscard]] errors. |
| 59 | void ignoreError() const {} |
| 60 | |
| 61 | // Implicit cast to Status. |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 62 | operator Status() const { return status(); } |
| 63 | |
| 64 | private: |
| 65 | Status mStatus = status::undefined; |
| 66 | T mValue; |
| 67 | }; |
| 68 | |
| 69 | template <typename T> |
| 70 | inline std::ostream& operator<<(std::ostream& os, const StatusOr<T>& s) { |
Joel Scherpelz | 01cc549 | 2017-06-16 10:45:14 +0900 | [diff] [blame] | 71 | return os << "StatusOr[status: " << s.status() << "]"; |
Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | #define ASSIGN_OR_RETURN_IMPL(tmp, lhs, stmt) \ |
| 75 | auto tmp = (stmt); \ |
| 76 | RETURN_IF_NOT_OK(tmp); \ |
| 77 | lhs = std::move(tmp.value()); |
| 78 | |
| 79 | #define ASSIGN_OR_RETURN_CONCAT(line, lhs, stmt) \ |
| 80 | ASSIGN_OR_RETURN_IMPL(__CONCAT(_status_or_, line), lhs, stmt) |
| 81 | |
| 82 | // Macro to allow exception-like handling of error return values. |
| 83 | // |
| 84 | // If the evaluation of stmt results in an error, return that error |
| 85 | // from the current function. Otherwise, assign the result to lhs. |
| 86 | // |
| 87 | // This macro supports both move and copy assignment operators. lhs |
| 88 | // may be either a new local variable or an existing non-const |
| 89 | // variable accessible in the current scope. |
| 90 | // |
| 91 | // Example usage: |
| 92 | // StatusOr<MyType> foo() { ... } |
| 93 | // |
| 94 | // ASSIGN_OR_RETURN(auto myVar, foo()); |
| 95 | // ASSIGN_OR_RETURN(myExistingVar, foo()); |
| 96 | // ASSIGN_OR_RETURN(myMemberVar, foo()); |
| 97 | #define ASSIGN_OR_RETURN(lhs, stmt) ASSIGN_OR_RETURN_CONCAT(__LINE__, lhs, stmt) |
| 98 | |
| 99 | } // namespace netdutils |
| 100 | } // namespace android |
| 101 | |
| 102 | #endif /* NETUTILS_STATUSOR_H */ |