| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_FML_STATUS_H_ |
| 6 | #define FLUTTER_FML_STATUS_H_ |
| 7 | |
| 8 | #include <string_view> |
| 9 | |
| 10 | namespace fml { |
| 11 | |
| 12 | enum class StatusCode { |
| 13 | kOk, |
| 14 | kCancelled, |
| 15 | kUnknown, |
| 16 | kInvalidArgument, |
| 17 | kDeadlineExceeded, |
| 18 | kNotFound, |
| 19 | kAlreadyExists, |
| 20 | kPermissionDenied, |
| 21 | kResourceExhausted, |
| 22 | kFailedPrecondition, |
| 23 | kAborted, |
| 24 | kOutOfRange, |
| 25 | kUnimplemented, |
| 26 | kInternal, |
| 27 | kUnavailable, |
| 28 | kDataLoss, |
| 29 | kUnauthenticated |
| 30 | }; |
| 31 | |
| 32 | /// Class that represents the resolution of the execution of a procedure. This |
| 33 | /// is used similarly to how exceptions might be used, typically as the return |
| 34 | /// value to a synchronous procedure or an argument to an asynchronous callback. |
| 35 | class Status final { |
| 36 | public: |
| 37 | /// Creates an 'ok' status. |
| 38 | Status(); |
| 39 | |
| 40 | Status(fml::StatusCode code, std::string_view message); |
| 41 | |
| 42 | fml::StatusCode code() const; |
| 43 | |
| 44 | /// A noop that helps with static analysis tools if you decide to ignore an |
| 45 | /// error. |
| 46 | void IgnoreError() const; |
| 47 | |
| 48 | /// @return 'true' when the code is kOk. |
| 49 | bool ok() const; |
| 50 | |
| 51 | std::string_view message() const; |
| 52 | |
| 53 | private: |
| 54 | fml::StatusCode code_; |
| 55 | std::string_view message_; |
| 56 | }; |
| 57 | |
| 58 | inline Status::Status() : code_(fml::StatusCode::kOk), message_() {} |
| 59 | |
| 60 | inline Status::Status(fml::StatusCode code, std::string_view message) |
| 61 | : code_(code), message_(message) {} |
| 62 | |
| 63 | inline fml::StatusCode Status::code() const { |
| 64 | return code_; |
| 65 | } |
| 66 | |
| 67 | inline void Status::IgnoreError() const { |
| 68 | // noop |
| 69 | } |
| 70 | |
| 71 | inline bool Status::ok() const { |
| 72 | return code_ == fml::StatusCode::kOk; |
| 73 | } |
| 74 | |
| 75 | inline std::string_view Status::message() const { |
| 76 | return message_; |
| 77 | } |
| 78 | |
| 79 | } // namespace fml |
| 80 | |
| 81 | #endif // FLUTTER_FML_SIZE_H_ |
| 82 | |