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