DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
status.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MEDIA_BASE_STATUS_H_
8 #define MEDIA_BASE_STATUS_H_
9 
10 #include <string>
11 
12 #include "packager/base/logging.h"
13 
14 namespace edash_packager {
15 namespace media {
16 
17 namespace error {
18 
20 enum Code {
21  // Not an error; returned on success
22  OK,
23 
24  // Unknown error. An example of where this error may be returned is
25  // errors raised by APIs that do not return enough error information
26  // may be converted to this error.
27  UNKNOWN,
28 
29  // The operation was cancelled (typically by the caller).
30  CANCELLED,
31 
32  // Client specified an invalid argument. INVALID_ARGUMENT indicates
33  // arguments that are problematic regardless of the state of the system
34  // (e.g. a malformed file name).
35  INVALID_ARGUMENT,
36 
37  // Operation is not implemented or not supported/enabled.
38  UNIMPLEMENTED,
39 
40  // Cannot open file.
41  FILE_FAILURE,
42 
43  // End of stream.
44  END_OF_STREAM,
45 
46  // Failure to get HTTP response successfully,
47  HTTP_FAILURE,
48 
49  // Unable to parse the media file.
50  PARSER_FAILURE,
51 
52  // Fail to mux the media file.
53  MUXER_FAILURE,
54 
55  // This track fragment is finalized.
56  FRAGMENT_FINALIZED,
57 
58  // Server errors. Receives malformed response from server.
59  SERVER_ERROR,
60 
61  // Internal errors. Some invariants have been broken.
62  INTERNAL_ERROR,
63 
64  // The operation was stopped.
65  STOPPED,
66 
67  // The operation timed out.
68  TIME_OUT,
69 
70  // Value was not found.
71  NOT_FOUND,
72 };
73 
74 } // namespace error
75 
76 class Status {
77  public:
79  Status() : error_code_(error::OK) {}
80 
84  Status(error::Code error_code, const std::string& error_message)
85  : error_code_(error_code) {
86  if (!ok())
87  error_message_ = error_message;
88  }
89 
90  ~Status() {}
91 
94  static const Status& OK; // Identical to 0-arg constructor.
95  static const Status& UNKNOWN;
97 
101  void SetError(error::Code error_code, const std::string& error_message) {
102  if (error_code == error::OK) {
103  Clear();
104  } else {
105  error_code_ = error_code;
106  error_message_ = error_message;
107  }
108  }
109 
118  void Update(const Status& new_status) {
119  if (ok())
120  *this = new_status;
121  }
122 
124  void Clear() {
125  error_code_ = error::OK;
126  error_message_ = "";
127  }
128 
129  bool ok() const { return error_code_ == error::OK; }
130  error::Code error_code() const { return error_code_; }
131  const std::string& error_message() const { return error_message_; }
132 
133  bool operator==(const Status& x) const {
134  return error_code_ == x.error_code() && error_message_ == x.error_message();
135  }
136  bool operator!=(const Status& x) const { return !(*this == x); }
137 
141  bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
142 
144  std::string ToString() const;
145 
146  void Swap(Status* other) {
147  error::Code error_code = error_code_;
148  error_code_ = other->error_code_;
149  other->error_code_ = error_code;
150  error_message_.swap(other->error_message_);
151  }
152 
153  private:
154  error::Code error_code_;
155  std::string error_message_;
156 
157  // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
158  // generated copy constructor and assignment operator.
159 };
160 
161 extern std::ostream& operator<<(std::ostream& os, const Status& x);
162 
163 } // namespace media
164 } // namespace edash_packager
165 
166 #endif // MEDIA_BASE_STATUS_H_
void Update(const Status &new_status)
Definition: status.h:118
Status()
Creates a "successful" status.
Definition: status.h:79
void Clear()
Clear this status object to contain the OK code and no error message.
Definition: status.h:124
Status(error::Code error_code, const std::string &error_message)
Definition: status.h:84
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:101
std::string ToString() const
Definition: status.cc:59
bool Matches(const Status &x) const
Definition: status.h:141