1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://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,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_STATUS_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_STATUS_H_
18
19 #include <string>
20
21 namespace tflite {
22 namespace gpu {
23
24 enum class StatusCode {
25 kOk = 0,
26 kCancelled = 1,
27 kUnknown = 2,
28 kInvalidArgument = 3,
29 kDeadlineExceeded = 4,
30 kNotFound = 5,
31 kAlreadyExists = 6,
32 kPermissionDenied = 7,
33 kResourceExhausted = 8,
34 kFailedPrecondition = 9,
35 kAborted = 10,
36 kOutOfRange = 11,
37 kUnimplemented = 12,
38 kInternal = 13,
39 kUnavailable = 14,
40 kDataLoss = 15,
41 kUnauthenticated = 16,
42 kDoNotUseReservedForFutureExpansionUseDefaultInSwitchInstead_ = 20
43 };
44
45 // Lite version of Status without dependency on protobuf.
46 // TODO(b/128867901): Migrate to absl::Status.
47 class Status {
48 public:
49 Status() = default;
Status(StatusCode code)50 Status(StatusCode code) : code_(code) {}
Status(StatusCode code,const std::string & error_message)51 Status(StatusCode code, const std::string& error_message)
52 : code_(code), error_message_(error_message) {}
53
error_message()54 const std::string& error_message() const { return error_message_; }
code()55 StatusCode code() const { return code_; }
ok()56 bool ok() const { return code_ == StatusCode::kOk; }
57
IgnoreError()58 void IgnoreError() const {}
59
60 private:
61 StatusCode code_ = StatusCode::kOk;
62 std::string error_message_;
63 };
64
65 #define RETURN_IF_ERROR(status) \
66 { \
67 const auto status2 = (status); \
68 if (!status2.ok()) return status2; \
69 }
70
OkStatus()71 inline Status OkStatus() { return Status(); }
72
AlreadyExistsError(const std::string & message)73 inline Status AlreadyExistsError(const std::string& message) {
74 return Status(StatusCode::kAlreadyExists, message);
75 }
76
DeadlineExceededError(const std::string & message)77 inline Status DeadlineExceededError(const std::string& message) {
78 return Status(StatusCode::kDeadlineExceeded, message);
79 }
80
FailedPreconditionError(const std::string & message)81 inline Status FailedPreconditionError(const std::string& message) {
82 return Status(StatusCode::kFailedPrecondition, message);
83 }
84
InternalError(const std::string & message)85 inline Status InternalError(const std::string& message) {
86 return Status(StatusCode::kInternal, message);
87 }
88
InvalidArgumentError(const std::string & message)89 inline Status InvalidArgumentError(const std::string& message) {
90 return Status(StatusCode::kInvalidArgument, message);
91 }
92
NotFoundError(const std::string & message)93 inline Status NotFoundError(const std::string& message) {
94 return Status(StatusCode::kNotFound, message);
95 }
96
OutOfRangeError(const std::string & message)97 inline Status OutOfRangeError(const std::string& message) {
98 return Status(StatusCode::kOutOfRange, message);
99 }
100
PermissionDeniedError(const std::string & message)101 inline Status PermissionDeniedError(const std::string& message) {
102 return Status(StatusCode::kPermissionDenied, message);
103 }
104
ResourceExhaustedError(const std::string & message)105 inline Status ResourceExhaustedError(const std::string& message) {
106 return Status(StatusCode::kResourceExhausted, message);
107 }
108
UnavailableError(const std::string & message)109 inline Status UnavailableError(const std::string& message) {
110 return Status(StatusCode::kUnavailable, message);
111 }
112
UnimplementedError(const std::string & message)113 inline Status UnimplementedError(const std::string& message) {
114 return Status(StatusCode::kUnimplemented, message);
115 }
116
UnknownError(const std::string & message)117 inline Status UnknownError(const std::string& message) {
118 return Status(StatusCode::kUnknown, message);
119 }
120
121 } // namespace gpu
122 } // namespace tflite
123
124 #endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_STATUS_H_
125