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>
21 #include <limits>
22 #include <ostream>
23
24 #include <android-base/result.h>
25
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.
33 //
34 // Status is implicitly movable via the default noexcept move constructor
35 // and noexcept move-assignment operator.
36 class [[nodiscard]] Status {
37 public:
38 Status() = default;
Status(int code)39 explicit Status(int code) : mCode(code) {}
40
41 // Constructs an error Status, |code| must be non-zero.
Status(int code,std::string msg)42 Status(int code, std::string msg) : mCode(code), mMsg(std::move(msg)) { assert(!ok()); }
43
44 // Constructs an error Status with message. Error |code| is unspecified.
Status(std::string msg)45 explicit Status(std::string msg) : Status(std::numeric_limits<int>::max(), std::move(msg)) {}
46
Status(android::base::Result<void> result)47 Status(android::base::Result<void> result)
48 : mCode(result.ok() ? 0 : static_cast<int>(result.error().code())),
49 mMsg(result.ok() ? "" : result.error().message()) {}
50
code()51 int code() const { return mCode; }
52
ok()53 bool ok() const { return code() == 0; }
54
msg()55 const std::string& msg() const { return mMsg; }
56
57 // Explicitly ignores the Status without triggering [[nodiscard]] errors.
ignoreError()58 void ignoreError() const {}
59
60 bool operator==(const Status& other) const { return code() == other.code(); }
61 bool operator!=(const Status& other) const { return !(*this == other); }
62
63 private:
64 int mCode = 0;
65 std::string mMsg;
66 };
67
68 namespace status {
69
70 const Status ok{0};
71 // EOF is not part of errno space, we'll place it far above the
72 // highest existing value.
73 const Status eof{0x10001, "end of file"};
74 const Status undefined{std::numeric_limits<int>::max(), "undefined"};
75
76 } // namespace status
77
78 // Return true if status is "OK". This is sometimes preferable to
79 // status.ok() when we want to check the state of Status-like objects
80 // that implicitly cast to Status.
isOk(const Status & status)81 inline bool isOk(const Status& status) {
82 return status.ok();
83 }
84
85 // For use only in tests. Used for both Status and Status-like objects. See also isOk().
86 #define EXPECT_OK(status) EXPECT_TRUE(isOk(status))
87 #define ASSERT_OK(status) ASSERT_TRUE(isOk(status))
88
89 // Documents that status is expected to be ok. This function may log
90 // (or assert when running in debug mode) if status has an unexpected value.
expectOk(const Status &)91 inline void expectOk(const Status& /*status*/) {
92 // TODO: put something here, for now this function serves solely as documentation.
93 }
94
95 // Convert POSIX errno to a Status object.
96 // If Status is extended to have more features, this mapping may
97 // become more complex.
98 Status statusFromErrno(int err, const std::string& msg);
99
100 // Helper that checks Status-like object (notably StatusOr) against a
101 // value in the errno space.
102 bool equalToErrno(const Status& status, int err);
103
104 // Helper that converts Status-like object (notably StatusOr) to a
105 // message.
106 std::string toString(const Status& status);
107
108 std::ostream& operator<<(std::ostream& os, const Status& s);
109
110 // Evaluate 'stmt' to a Status object and if it results in an error, return that
111 // error. Use 'tmp' as a variable name to avoid shadowing any variables named
112 // tmp.
113 #define RETURN_IF_NOT_OK_IMPL(tmp, stmt) \
114 do { \
115 ::android::netdutils::Status tmp = (stmt); \
116 if (!isOk(tmp)) { \
117 return tmp; \
118 } \
119 } while (false)
120
121 // Create a unique variable name to avoid shadowing local variables.
122 #define RETURN_IF_NOT_OK_CONCAT(line, stmt) RETURN_IF_NOT_OK_IMPL(__CONCAT(_status_, line), stmt)
123
124 // Macro to allow exception-like handling of error return values.
125 //
126 // If the evaluation of stmt results in an error, return that error
127 // from current function.
128 //
129 // Example usage:
130 // Status bar() { ... }
131 //
132 // RETURN_IF_NOT_OK(status);
133 // RETURN_IF_NOT_OK(bar());
134 #define RETURN_IF_NOT_OK(stmt) RETURN_IF_NOT_OK_CONCAT(__LINE__, stmt)
135
136 } // namespace netdutils
137 } // namespace android
138
139 #endif /* NETUTILS_STATUS_H */
140