1 /* Copyright 2020 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_CC_EXPERIMENTAL_BASE_PUBLIC_STATUS_H_
17 #define TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_STATUS_H_
18 
19 #include <memory>
20 #include <string>
21 
22 #include "tensorflow/c/tf_status.h"
23 
24 namespace tensorflow {
25 namespace experimental {
26 namespace cc {
27 
28 // Status is a wrapper around an error code and an optional error message.
29 // The set of error codes are defined here:
30 // https://github.com/tensorflow/tensorflow/blob/08931c1e3e9eb2e26230502d678408e66730826c/tensorflow/c/tf_status.h#L39-L60
31 // Many Tensorflow APIs return a Status, or take a Status as an out parameter.
32 // Clients should check for status.ok() after calling these APIs, and either
33 // handle or propagate the error appropriately.
34 // TODO(bmzhao): Add a detailed code example before moving out of experimental.
35 class Status {
36  public:
37   // Create a success status
Status()38   Status() : status_(TF_NewStatus()) {}
39 
40   // Return the status code
41   TF_Code code() const;
42 
43   // Returns the error message in Status.
44   std::string message() const;
45 
46   // Returns the error message in Status.
47   bool ok() const;
48 
49   // Record <code, msg> in Status. Any previous information is lost.
50   // A common use is to clear a status: SetStatus(TF_OK, "");
51   void SetStatus(TF_Code code, const std::string& msg);
52 
53   // Status is movable, but not copyable.
54   Status(Status&&) = default;
55   Status& operator=(Status&&) = default;
56 
57  private:
58   friend class RuntimeBuilder;
59   friend class Runtime;
60   friend class SavedModelAPI;
61   friend class TensorHandle;
62 
63   // Wraps a TF_Status*, and takes ownership of it.
Status(TF_Status * status)64   explicit Status(TF_Status* status) : status_(status) {}
65 
66   // Status is not copyable
67   Status(const Status&) = delete;
68   Status& operator=(const Status&) = delete;
69 
70   // Returns the TF_Status that this object wraps. This object
71   // retains ownership of the pointer.
GetTFStatus()72   TF_Status* GetTFStatus() const { return status_.get(); }
73 
74   struct TFStatusDeleter {
operatorTFStatusDeleter75     void operator()(TF_Status* p) const { TF_DeleteStatus(p); }
76   };
77   std::unique_ptr<TF_Status, TFStatusDeleter> status_;
78 };
79 
code()80 inline TF_Code Status::code() const { return TF_GetCode(status_.get()); }
81 
message()82 inline std::string Status::message() const {
83   return std::string(TF_Message(status_.get()));
84 }
85 
ok()86 inline bool Status::ok() const { return code() == TF_OK; }
87 
SetStatus(TF_Code code,const std::string & msg)88 inline void Status::SetStatus(TF_Code code, const std::string& msg) {
89   TF_SetStatus(status_.get(), code, msg.c_str());
90 }
91 
92 }  // namespace cc
93 }  // namespace experimental
94 }  // namespace tensorflow
95 
96 #endif  // TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_STATUS_H_
97