diff options
Diffstat (limited to 'tensorflow/core/public/status.h')
-rw-r--r-- | tensorflow/core/public/status.h | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tensorflow/core/public/status.h b/tensorflow/core/public/status.h new file mode 100644 index 0000000000..d0405b8876 --- /dev/null +++ b/tensorflow/core/public/status.h @@ -0,0 +1,96 @@ +#ifndef TENSORFLOW_PUBLIC_STATUS_H_ +#define TENSORFLOW_PUBLIC_STATUS_H_ + +#include <iosfwd> +#include <string> +#include "tensorflow/core/lib/core/error_codes.pb.h" +#include "tensorflow/core/lib/core/stringpiece.h" +#include "tensorflow/core/platform/logging.h" + +namespace tensorflow { + +class Status { + public: + /// Create a success status. + Status() : state_(NULL) {} + ~Status(); + + /// \brief Create a status with the specified error code and msg as a + /// human-readable string containing more detailed information. + Status(tensorflow::error::Code code, tensorflow::StringPiece msg); + + /// Copy the specified status. + Status(const Status& s); + void operator=(const Status& s); + + static Status OK() { return Status(); } + + /// Returns true iff the status indicates success. + bool ok() const { return (state_ == NULL); } + + tensorflow::error::Code code() const { + return ok() ? tensorflow::error::OK : state_->code; + } + + const string& error_message() const { + return ok() ? empty_string() : state_->msg; + } + + bool operator==(const Status& x) const; + bool operator!=(const Status& x) const; + + /// \brief If "ok()", stores "new_status" into *this. If "!ok()", preserves + /// the current status, but may augment with additional information + /// about "new_status". + /// + /// Convenient way of keeping track of the first error encountered. + /// Instead of: + /// if (overall_status.ok()) overall_status = new_status + /// Use: + /// overall_status.Update(new_status); + void Update(const Status& new_status); + + /// \brief Return a string representation of this status suitable for + /// printing. Returns the string "OK" for success. + string ToString() const; + + private: + static const string& empty_string(); + struct State { + tensorflow::error::Code code; + string msg; + }; + /// OK status has a NULL state_. Otherwise, state_ points to + /// a State structure containing the error code and message(s) + State* state_; + + void SlowCopyFrom(const State* src); +}; + +inline Status::Status(const Status& s) + : state_((s.state_ == NULL) ? NULL : new State(*s.state_)) {} + +inline void Status::operator=(const Status& s) { + /// The following condition catches both aliasing (when this == &s), + /// and the common case where both s and *this are ok. + if (state_ != s.state_) { + SlowCopyFrom(s.state_); + } +} + +inline bool Status::operator==(const Status& x) const { + return (this->state_ == x.state_) || (ToString() == x.ToString()); +} + +inline bool Status::operator!=(const Status& x) const { return !(*this == x); } + +std::ostream& operator<<(std::ostream& os, const Status& x); + +typedef std::function<void(const Status&)> StatusCallback; + +#define TF_CHECK_OK(val) CHECK_EQ(::tensorflow::Status::OK(), (val)) +#define TF_QCHECK_OK(val) QCHECK_EQ(::tensorflow::Status::OK(), (val)) + +} // namespace tensorflow + +#endif // TENSORFLOW_PUBLIC_STATUS_H_ |