C/C++ API Reference
Loading...
Searching...
No Matches
status.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_status/internal/config.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif // __cplusplus
21
22// This is the pw_Status enum. pw_Status is used to return the status from an
23// operation.
24//
25// In C++, use the pw::Status class instead of the pw_Status enum. pw_Status and
26// Status implicitly convert to one another and can be passed cleanly between C
27// and C++ APIs.
28//
29// pw_Status uses the canonical Google error codes. The following enum was based
30// on Abseil's status/status.h. The values are all-caps and prefixed with
31// PW_STATUS_ instead of using C++ constant style.
32//
33// The status codes are described at
34// https://pigweed.dev/pw_status/reference.html#status-codes. Consult that
35// guide when deciding which status code to use.
36typedef enum {
37 PW_STATUS_OK = 0, // Use OkStatus() in C++
38 PW_STATUS_CANCELLED = 1, // Use Status::Cancelled() in C++
39 PW_STATUS_UNKNOWN = 2, // Use Status::Unknown() in C++
40 PW_STATUS_INVALID_ARGUMENT = 3, // Use Status::InvalidArgument() in C++
41 PW_STATUS_DEADLINE_EXCEEDED = 4, // Use Status::DeadlineExceeded() in C++
42 PW_STATUS_NOT_FOUND = 5, // Use Status::NotFound() in C++
43 PW_STATUS_ALREADY_EXISTS = 6, // Use Status::AlreadyExists() in C++
44 PW_STATUS_PERMISSION_DENIED = 7, // Use Status::PermissionDenied() in C++
45 PW_STATUS_RESOURCE_EXHAUSTED = 8, // Use Status::ResourceExhausted() in C++
46 PW_STATUS_FAILED_PRECONDITION = 9, // Use Status::FailedPrecondition() in C++
47 PW_STATUS_ABORTED = 10, // Use Status::Aborted() in C++
48 PW_STATUS_OUT_OF_RANGE = 11, // Use Status::OutOfRange() in C++
49 PW_STATUS_UNIMPLEMENTED = 12, // Use Status::Unimplemented() in C++
50 PW_STATUS_INTERNAL = 13, // Use Status::Internal() in C++
51 PW_STATUS_UNAVAILABLE = 14, // Use Status::Unavailable() in C++
52 PW_STATUS_DATA_LOSS = 15, // Use Status::DataLoss() in C++
53 PW_STATUS_UNAUTHENTICATED = 16, // Use Status::Unauthenticated() in C++
54
55 // NOTE: this error code entry should not be used and you should not rely on
56 // its value, which may change.
57 //
58 // The purpose of this enumerated value is to force people who handle status
59 // codes with `switch()` statements to *not* simply enumerate all possible
60 // values, but instead provide a "default:" case. Providing such a default
61 // case ensures that code will compile when new codes are added.
62 PW_STATUS_DO_NOT_USE_RESERVED_FOR_FUTURE_EXPANSION_USE_DEFAULT_IN_SWITCH_INSTEAD_,
63} pw_Status; // Use pw::Status in C++
64
65// Returns a null-terminated string representation of the pw_Status.
66const char* pw_StatusString(pw_Status status);
67
68// Indicates the status code with the highest valid value.
69#define PW_STATUS_LAST PW_STATUS_UNAUTHENTICATED
70
71#ifdef __cplusplus
72
73} // extern "C"
74
76namespace pw {
77
86class _PW_STATUS_NO_DISCARD Status {
87 public:
88 using Code = pw_Status;
89
90 // Functions that create a Status with the specified code.
91 //
92 // The status codes are described at
93 // https://pigweed.dev/pw_status#status-codes. Consult that guide when
94 // deciding which status code to use.
95 // clang-format off
96 [[nodiscard]] static constexpr Status Cancelled() {
97 return PW_STATUS_CANCELLED;
98 }
99 [[nodiscard]] static constexpr Status Unknown() {
100 return PW_STATUS_UNKNOWN;
101 }
102 [[nodiscard]] static constexpr Status InvalidArgument() {
103 return PW_STATUS_INVALID_ARGUMENT;
104 }
105 [[nodiscard]] static constexpr Status DeadlineExceeded() {
106 return PW_STATUS_DEADLINE_EXCEEDED;
107 }
108 [[nodiscard]] static constexpr Status NotFound() {
109 return PW_STATUS_NOT_FOUND;
110 }
111 [[nodiscard]] static constexpr Status AlreadyExists() {
112 return PW_STATUS_ALREADY_EXISTS;
113 }
114 [[nodiscard]] static constexpr Status PermissionDenied() {
115 return PW_STATUS_PERMISSION_DENIED;
116 }
117 [[nodiscard]] static constexpr Status ResourceExhausted() {
118 return PW_STATUS_RESOURCE_EXHAUSTED;
119 }
120 [[nodiscard]] static constexpr Status FailedPrecondition() {
121 return PW_STATUS_FAILED_PRECONDITION;
122 }
123 [[nodiscard]] static constexpr Status Aborted() {
124 return PW_STATUS_ABORTED;
125 }
126 [[nodiscard]] static constexpr Status OutOfRange() {
127 return PW_STATUS_OUT_OF_RANGE;
128 }
129 [[nodiscard]] static constexpr Status Unimplemented() {
130 return PW_STATUS_UNIMPLEMENTED;
131 }
132 [[nodiscard]] static constexpr Status Internal() {
133 return PW_STATUS_INTERNAL;
134 }
135 [[nodiscard]] static constexpr Status Unavailable() {
136 return PW_STATUS_UNAVAILABLE;
137 }
138 [[nodiscard]] static constexpr Status DataLoss() {
139 return PW_STATUS_DATA_LOSS;
140 }
141 [[nodiscard]] static constexpr Status Unauthenticated() {
142 return PW_STATUS_UNAUTHENTICATED;
143 }
144 // clang-format on
145
146 // Statuses are created with a Status::Code.
147 constexpr Status(Code code = PW_STATUS_OK) : code_(code) {}
148
149 constexpr Status(const Status&) = default;
150 constexpr Status& operator=(const Status&) = default;
151
153 constexpr Code code() const { return code_; }
154
158 [[nodiscard]] constexpr bool ok() const { return code_ == PW_STATUS_OK; }
159
160 // Functions for checking which status this is.
161 [[nodiscard]] constexpr bool IsCancelled() const {
162 return code_ == PW_STATUS_CANCELLED;
163 }
164 [[nodiscard]] constexpr bool IsUnknown() const {
165 return code_ == PW_STATUS_UNKNOWN;
166 }
167 [[nodiscard]] constexpr bool IsInvalidArgument() const {
168 return code_ == PW_STATUS_INVALID_ARGUMENT;
169 }
170 [[nodiscard]] constexpr bool IsDeadlineExceeded() const {
171 return code_ == PW_STATUS_DEADLINE_EXCEEDED;
172 }
173 [[nodiscard]] constexpr bool IsNotFound() const {
174 return code_ == PW_STATUS_NOT_FOUND;
175 }
176 [[nodiscard]] constexpr bool IsAlreadyExists() const {
177 return code_ == PW_STATUS_ALREADY_EXISTS;
178 }
179 [[nodiscard]] constexpr bool IsPermissionDenied() const {
180 return code_ == PW_STATUS_PERMISSION_DENIED;
181 }
182 [[nodiscard]] constexpr bool IsResourceExhausted() const {
183 return code_ == PW_STATUS_RESOURCE_EXHAUSTED;
184 }
185 [[nodiscard]] constexpr bool IsFailedPrecondition() const {
186 return code_ == PW_STATUS_FAILED_PRECONDITION;
187 }
188 [[nodiscard]] constexpr bool IsAborted() const {
189 return code_ == PW_STATUS_ABORTED;
190 }
191 [[nodiscard]] constexpr bool IsOutOfRange() const {
192 return code_ == PW_STATUS_OUT_OF_RANGE;
193 }
194 [[nodiscard]] constexpr bool IsUnimplemented() const {
195 return code_ == PW_STATUS_UNIMPLEMENTED;
196 }
197 [[nodiscard]] constexpr bool IsInternal() const {
198 return code_ == PW_STATUS_INTERNAL;
199 }
200 [[nodiscard]] constexpr bool IsUnavailable() const {
201 return code_ == PW_STATUS_UNAVAILABLE;
202 }
203 [[nodiscard]] constexpr bool IsDataLoss() const {
204 return code_ == PW_STATUS_DATA_LOSS;
205 }
206 [[nodiscard]] constexpr bool IsUnauthenticated() const {
207 return code_ == PW_STATUS_UNAUTHENTICATED;
208 }
209
214 constexpr void Update(Status other) {
215 if (ok()) {
216 code_ = other.code();
217 }
218 }
219
223 constexpr void IgnoreError() const {}
224
226 [[nodiscard]] const char* str() const { return pw_StatusString(code_); }
227
228 private:
229 Code code_;
230};
231
235[[nodiscard]] constexpr Status OkStatus() { return Status(); }
236
237constexpr bool operator==(const Status& lhs, const Status& rhs) {
238 return lhs.code() == rhs.code();
239}
240
241constexpr bool operator!=(const Status& lhs, const Status& rhs) {
242 return lhs.code() != rhs.code();
243}
244
245namespace internal {
246
247// This function and its various overloads are for use by internal macros
248// like PW_TRY.
249constexpr Status ConvertToStatus(Status status) { return status; }
250
251} // namespace internal
252} // namespace pw
253
254// Create a C++ overload of pw_StatusString so that it supports pw::Status in
255// addition to pw_Status.
256inline const char* pw_StatusString(pw::Status status) {
257 return pw_StatusString(status.code());
258}
259
260#endif // __cplusplus
Definition: status.h:86
constexpr void Update(Status other)
Definition: status.h:214
constexpr Code code() const
Returns the Status::Code (pw_Status) for this Status.
Definition: status.h:153
constexpr bool ok() const
Definition: status.h:158
constexpr void IgnoreError() const
Definition: status.h:223
const char * str() const
Returns a null-terminated string representation of the Status.
Definition: status.h:226
The Pigweed namespace.
Definition: alignment.h:27
constexpr Status OkStatus()
Definition: status.h:235