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
23
24// This is the pw_Status enum. pw_Status is used to return the status from an
25// operation.
26//
27// In C++, use the pw::Status class instead of the pw_Status enum. pw_Status and
28// Status implicitly convert to one another and can be passed cleanly between C
29// and C++ APIs.
30//
31// pw_Status uses the canonical Google error codes. The following enum was based
32// on Abseil's status/status.h. The values are all-caps and prefixed with
33// PW_STATUS_ instead of using C++ constant style.
34//
35// The status codes are described at
36// https://pigweed.dev/pw_status/reference.html. Consult that
37// guide when deciding which status code to use.
38
40typedef enum {
42 PW_STATUS_OK = 0, // Use OkStatus() in C++
44 PW_STATUS_CANCELLED = 1, // Use Status::Cancelled() in C++
46 PW_STATUS_UNKNOWN = 2, // Use Status::Unknown() in C++
48 PW_STATUS_INVALID_ARGUMENT = 3, // Use Status::InvalidArgument() in C++
50 PW_STATUS_DEADLINE_EXCEEDED = 4, // Use Status::DeadlineExceeded() in C++
52 PW_STATUS_NOT_FOUND = 5, // Use Status::NotFound() in C++
54 PW_STATUS_ALREADY_EXISTS = 6, // Use Status::AlreadyExists() in C++
56 PW_STATUS_PERMISSION_DENIED = 7, // Use Status::PermissionDenied() in C++
58 PW_STATUS_RESOURCE_EXHAUSTED = 8, // Use Status::ResourceExhausted() in C++
60 PW_STATUS_FAILED_PRECONDITION = 9, // Use Status::FailedPrecondition() in C++
62 PW_STATUS_ABORTED = 10, // Use Status::Aborted() in C++
64 PW_STATUS_OUT_OF_RANGE = 11, // Use Status::OutOfRange() in C++
66 PW_STATUS_UNIMPLEMENTED = 12, // Use Status::Unimplemented() in C++
68 PW_STATUS_INTERNAL = 13, // Use Status::Internal() in C++
70 PW_STATUS_UNAVAILABLE = 14, // Use Status::Unavailable() in C++
72 PW_STATUS_DATA_LOSS = 15, // Use Status::DataLoss() in C++
74 PW_STATUS_UNAUTHENTICATED = 16, // Use Status::Unauthenticated() in C++
75
76 // Warning: This error code entry should not be used and you should not rely
77 // on its value, which may change.
78 //
79 // The purpose of this enumerated value is to force people who handle status
80 // codes with `switch` statements to *not* simply enumerate all possible
81 // values, but instead provide a `default` case. Providing such a default
82 // case ensures that code will compile when new codes are added.
83 PW_STATUS_DO_NOT_USE_RESERVED_FOR_FUTURE_EXPANSION_USE_DEFAULT_IN_SWITCH_INSTEAD_,
84} pw_Status; // Use pw::Status in C++
85
87const char* pw_StatusString(pw_Status status);
88
90#define PW_STATUS_LAST PW_STATUS_UNAUTHENTICATED
91
93
94#ifdef __cplusplus
95
96} // extern "C"
97
99namespace pw {
100
102
120class _PW_STATUS_NO_DISCARD Status {
121 public:
122 using Code = pw_Status;
123
124 // Functions that create a Status with the specified code.
125 //
126 // The status codes are described at
127 // https://pigweed.dev/pw_status/reference.html. Consult that guide when
128 // deciding which status code to use.
129 // clang-format off
130
139 [[nodiscard]] static constexpr Status Cancelled() {
140 return PW_STATUS_CANCELLED;
141 }
142
151 [[nodiscard]] static constexpr Status Unknown() {
152 return PW_STATUS_UNKNOWN;
153 }
154
164 [[nodiscard]] static constexpr Status InvalidArgument() {
166 }
167
177 [[nodiscard]] static constexpr Status DeadlineExceeded() {
179 }
180
190 [[nodiscard]] static constexpr Status NotFound() {
191 return PW_STATUS_NOT_FOUND;
192 }
193
203 [[nodiscard]] static constexpr Status AlreadyExists() {
205 }
206
216 [[nodiscard]] static constexpr Status PermissionDenied() {
218 }
219
230 [[nodiscard]] static constexpr Status ResourceExhausted() {
232 }
233
243 [[nodiscard]] static constexpr Status FailedPrecondition() {
245 }
246
255 [[nodiscard]] static constexpr Status Aborted() {
256 return PW_STATUS_ABORTED;
257 }
258
267 [[nodiscard]] static constexpr Status OutOfRange() {
269 }
270
280 [[nodiscard]] static constexpr Status Unimplemented() {
282 }
283
292 [[nodiscard]] static constexpr Status Internal() {
293 return PW_STATUS_INTERNAL;
294 }
295
304 [[nodiscard]] static constexpr Status Unavailable() {
306 }
307
316 [[nodiscard]] static constexpr Status DataLoss() {
317 return PW_STATUS_DATA_LOSS;
318 }
319
329 [[nodiscard]] static constexpr Status Unauthenticated() {
331 }
332 // clang-format on
333
335 constexpr Status(Code code = PW_STATUS_OK) : code_(code) {}
336
337 constexpr Status(const Status&) = default;
338 constexpr Status& operator=(const Status&) = default;
339
341 constexpr Code code() const { return code_; }
342
346 [[nodiscard]] constexpr bool ok() const { return code_ == PW_STATUS_OK; }
347
349 [[nodiscard]] constexpr bool IsCancelled() const {
350 return code_ == PW_STATUS_CANCELLED;
351 }
353 [[nodiscard]] constexpr bool IsUnknown() const {
354 return code_ == PW_STATUS_UNKNOWN;
355 }
357 [[nodiscard]] constexpr bool IsInvalidArgument() const {
358 return code_ == PW_STATUS_INVALID_ARGUMENT;
359 }
361 [[nodiscard]] constexpr bool IsDeadlineExceeded() const {
362 return code_ == PW_STATUS_DEADLINE_EXCEEDED;
363 }
365 [[nodiscard]] constexpr bool IsNotFound() const {
366 return code_ == PW_STATUS_NOT_FOUND;
367 }
369 [[nodiscard]] constexpr bool IsAlreadyExists() const {
370 return code_ == PW_STATUS_ALREADY_EXISTS;
371 }
373 [[nodiscard]] constexpr bool IsPermissionDenied() const {
374 return code_ == PW_STATUS_PERMISSION_DENIED;
375 }
377 [[nodiscard]] constexpr bool IsResourceExhausted() const {
378 return code_ == PW_STATUS_RESOURCE_EXHAUSTED;
379 }
381 [[nodiscard]] constexpr bool IsFailedPrecondition() const {
382 return code_ == PW_STATUS_FAILED_PRECONDITION;
383 }
385 [[nodiscard]] constexpr bool IsAborted() const {
386 return code_ == PW_STATUS_ABORTED;
387 }
389 [[nodiscard]] constexpr bool IsOutOfRange() const {
390 return code_ == PW_STATUS_OUT_OF_RANGE;
391 }
393 [[nodiscard]] constexpr bool IsUnimplemented() const {
394 return code_ == PW_STATUS_UNIMPLEMENTED;
395 }
397 [[nodiscard]] constexpr bool IsInternal() const {
398 return code_ == PW_STATUS_INTERNAL;
399 }
401 [[nodiscard]] constexpr bool IsUnavailable() const {
402 return code_ == PW_STATUS_UNAVAILABLE;
403 }
405 [[nodiscard]] constexpr bool IsDataLoss() const {
406 return code_ == PW_STATUS_DATA_LOSS;
407 }
409 [[nodiscard]] constexpr bool IsUnauthenticated() const {
410 return code_ == PW_STATUS_UNAUTHENTICATED;
411 }
412
419 constexpr void Update(Status other) {
420 if (ok()) {
421 code_ = other.code();
422 }
423 }
424
430 constexpr void IgnoreError() const {}
431
433 [[nodiscard]] const char* str() const { return pw_StatusString(code_); }
434
435 private:
436 Code code_;
437};
438
450[[nodiscard]] constexpr Status OkStatus() { return Status(); }
451
452constexpr bool operator==(const Status& lhs, const Status& rhs) {
453 return lhs.code() == rhs.code();
454}
455
456constexpr bool operator!=(const Status& lhs, const Status& rhs) {
457 return lhs.code() != rhs.code();
458}
459
461
462namespace internal {
463
464// This function and its various overloads are for use by internal macros
465// like PW_TRY.
466constexpr Status ConvertToStatus(Status status) { return status; }
467
468} // namespace internal
469} // namespace pw
470
472
475inline const char* pw_StatusString(pw::Status status) {
476 return pw_StatusString(status.code());
477}
478
480
481#endif // __cplusplus
Definition: status.h:120
static constexpr Status Unauthenticated()
Definition: status.h:329
constexpr bool IsNotFound() const
Definition: status.h:365
static constexpr Status DataLoss()
Definition: status.h:316
static constexpr Status PermissionDenied()
Definition: status.h:216
constexpr bool IsOutOfRange() const
Definition: status.h:389
constexpr void Update(Status other)
Definition: status.h:419
constexpr Code code() const
Definition: status.h:341
static constexpr Status Unimplemented()
Definition: status.h:280
constexpr bool IsCancelled() const
Definition: status.h:349
constexpr bool IsInvalidArgument() const
Definition: status.h:357
constexpr bool IsAlreadyExists() const
Definition: status.h:369
constexpr bool IsResourceExhausted() const
Definition: status.h:377
static constexpr Status InvalidArgument()
Definition: status.h:164
static constexpr Status DeadlineExceeded()
Definition: status.h:177
static constexpr Status Cancelled()
Definition: status.h:139
constexpr bool ok() const
Definition: status.h:346
constexpr bool IsUnauthenticated() const
Definition: status.h:409
constexpr Status(Code code=PW_STATUS_OK)
Statuses are created with a Status::Code.
Definition: status.h:335
constexpr bool IsDataLoss() const
Definition: status.h:405
constexpr bool IsAborted() const
Definition: status.h:385
constexpr bool IsPermissionDenied() const
Definition: status.h:373
static constexpr Status Unavailable()
Definition: status.h:304
static constexpr Status Aborted()
Definition: status.h:255
static constexpr Status FailedPrecondition()
Definition: status.h:243
static constexpr Status Unknown()
Definition: status.h:151
static constexpr Status NotFound()
Definition: status.h:190
static constexpr Status Internal()
Definition: status.h:292
constexpr bool IsInternal() const
Definition: status.h:397
constexpr bool IsUnknown() const
Definition: status.h:353
constexpr void IgnoreError() const
Definition: status.h:430
static constexpr Status OutOfRange()
Definition: status.h:267
static constexpr Status AlreadyExists()
Definition: status.h:203
static constexpr Status ResourceExhausted()
Definition: status.h:230
constexpr bool IsFailedPrecondition() const
Definition: status.h:381
constexpr bool IsUnimplemented() const
Definition: status.h:393
const char * str() const
Definition: status.h:433
constexpr bool IsDeadlineExceeded() const
Definition: status.h:361
constexpr bool IsUnavailable() const
Definition: status.h:401
pw_Status
C API for status codes. In C++, use the pw::Status class instead.
Definition: status.h:40
constexpr Status OkStatus()
Definition: status.h:450
const char * pw_StatusString(pw_Status status)
@ PW_STATUS_OUT_OF_RANGE
Definition: status.h:64
@ PW_STATUS_DATA_LOSS
Definition: status.h:72
@ PW_STATUS_UNAUTHENTICATED
Definition: status.h:74
@ PW_STATUS_ABORTED
Definition: status.h:62
@ PW_STATUS_DEADLINE_EXCEEDED
Definition: status.h:50
@ PW_STATUS_CANCELLED
Definition: status.h:44
@ PW_STATUS_RESOURCE_EXHAUSTED
Definition: status.h:58
@ PW_STATUS_NOT_FOUND
Definition: status.h:52
@ PW_STATUS_INVALID_ARGUMENT
Definition: status.h:48
@ PW_STATUS_OK
Definition: status.h:42
@ PW_STATUS_PERMISSION_DENIED
Definition: status.h:56
@ PW_STATUS_UNIMPLEMENTED
Definition: status.h:66
@ PW_STATUS_ALREADY_EXISTS
Definition: status.h:54
@ PW_STATUS_UNAVAILABLE
Definition: status.h:70
@ PW_STATUS_INTERNAL
Definition: status.h:68
@ PW_STATUS_UNKNOWN
Definition: status.h:46
@ PW_STATUS_FAILED_PRECONDITION
Definition: status.h:60
The Pigweed namespace.
Definition: alignment.h:27