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 "binder/Status.h"
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.
31 class Status {
32 public:
33 Status() = default;
34
Status(int code)35 explicit Status(int code) : mCode(code) {}
36
Status(int code,const std::string & msg)37 Status(int code, const std::string& msg) : mCode(code), mMsg(msg) { assert(!ok()); }
38
code()39 int code() const { return mCode; }
40
ok()41 bool ok() const { return code() == 0; }
42
msg()43 const std::string& msg() const { return mMsg; }
44
45 bool operator==(const Status& other) const { return code() == other.code(); }
46 bool operator!=(const Status& other) const { return !(*this == other); }
47
48 private:
49 int mCode = 0;
50 std::string mMsg;
51 };
52
53 namespace status {
54
55 const Status ok{0};
56 // EOF is not part of errno space, we'll place it far above the
57 // highest existing value.
58 const Status eof{0x10001, "end of file"};
59 const Status undefined{std::numeric_limits<int>::max(), "undefined"};
60
61 } // namespace status
62
63 // Return true if status is "OK". This is sometimes preferable to
64 // status.ok() when we want to check the state of Status-like objects
65 // that implicitly cast to Status.
isOk(const Status & status)66 inline bool isOk(const Status& status) {
67 return status.ok();
68 }
69
70 // Document that status is expected to be ok. This function may log
71 // (or assert when running in debug mode) if status has an unexpected
72 // value.
73 void expectOk(const Status& status);
74
75 // Convert POSIX errno to a Status object.
76 // If Status is extended to have more features, this mapping may
77 // become more complex.
78 Status statusFromErrno(int err, const std::string& msg);
79
80 // Helper that checks Status-like object (notably StatusOr) against a
81 // value in the errno space.
82 bool equalToErrno(const Status& status, int err);
83
84 // Converts netdutils Status into binder Status.
85 binder::Status asBinderStatus(const netdutils::Status& status);
86
87 // Helper that converts Status-like object (notably StatusOr) to a
88 // message.
89 std::string toString(const Status& status);
90
91 std::ostream& operator<<(std::ostream& os, const Status& s);
92
93 // Evaluate 'stmt' to a Status object and if it results in an error, return that
94 // error. Use 'tmp' as a variable name to avoid shadowing any variables named
95 // tmp.
96 #define RETURN_IF_NOT_OK_IMPL(tmp, stmt) \
97 do { \
98 ::android::netdutils::Status tmp = (stmt); \
99 if (!isOk(tmp)) { \
100 return tmp; \
101 } \
102 } while (false)
103
104 // Create a unique variable name to avoid shadowing local variables.
105 #define RETURN_IF_NOT_OK_CONCAT(line, stmt) RETURN_IF_NOT_OK_IMPL(__CONCAT(_status_, line), stmt)
106
107 // Macro to allow exception-like handling of error return values.
108 //
109 // If the evaluation of stmt results in an error, return that error
110 // from current function.
111 //
112 // Example usage:
113 // Status bar() { ... }
114 //
115 // RETURN_IF_NOT_OK(status);
116 // RETURN_IF_NOT_OK(bar());
117 #define RETURN_IF_NOT_OK(stmt) RETURN_IF_NOT_OK_CONCAT(__LINE__, stmt)
118
119 } // namespace netdutils
120 } // namespace android
121
122 #endif /* NETUTILS_STATUS_H */
123