• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_H_
18 #define LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_H_
19 
20 #include <string>
21 
22 #include "utils/base/logging.h"
23 
24 namespace libtextclassifier3 {
25 
26 enum class StatusCode {
27   // Not an error; returned on success
28   OK = 0,
29 
30   // All of the following StatusCodes represent errors.
31   CANCELLED = 1,
32   UNKNOWN = 2,
33   INVALID_ARGUMENT = 3,
34   DEADLINE_EXCEEDED = 4,
35   NOT_FOUND = 5,
36   ALREADY_EXISTS = 6,
37   PERMISSION_DENIED = 7,
38   RESOURCE_EXHAUSTED = 8,
39   FAILED_PRECONDITION = 9,
40   ABORTED = 10,
41   OUT_OF_RANGE = 11,
42   UNIMPLEMENTED = 12,
43   INTERNAL = 13,
44   UNAVAILABLE = 14,
45   DATA_LOSS = 15,
46   UNAUTHENTICATED = 16
47 };
48 
49 // A Status is a combination of an error code and a string message (for non-OK
50 // error codes).
51 class Status {
52  public:
53   // Creates an OK status
54   Status();
55 
56   // Make a Status from the specified error and message.
57   Status(StatusCode error, const std::string& error_message);
58 
59   // Some pre-defined Status objects
60   static const Status& OK;
61   static const Status& UNKNOWN;
62 
63   // Accessors
ok()64   bool ok() const { return code_ == StatusCode::OK; }
error_code()65   int error_code() const { return static_cast<int>(code_); }
66 
CanonicalCode()67   StatusCode CanonicalCode() const { return code_; }
68 
error_message()69   const std::string& error_message() const { return message_; }
70 
71   // Noop function provided to allow callers to suppress compiler warnings about
72   // ignored return values.
IgnoreError()73   void IgnoreError() const {}
74 
75   bool operator==(const Status& x) const;
76   bool operator!=(const Status& x) const;
77 
78   std::string ToString() const;
79 
80  private:
81   StatusCode code_;
82   std::string message_;
83 };
84 
85 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
86                                          const Status& status);
87 
88 }  // namespace libtextclassifier3
89 
90 #endif  // LIBTEXTCLASSIFIER_UTILS_BASE_STATUS_H_
91