DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 <iostream>
11 #include <string>
12 
13 namespace shaka {
14 namespace media {
15 
16 namespace error {
17 
19 enum Code {
20  // Not an error; returned on success
21  OK,
22 
23  // Unknown error. An example of where this error may be returned is
24  // errors raised by APIs that do not return enough error information
25  // may be converted to this error.
26  UNKNOWN,
27 
28  // The operation was cancelled (typically by the caller).
29  CANCELLED,
30 
31  // Client specified an invalid argument. INVALID_ARGUMENT indicates
32  // arguments that are problematic regardless of the state of the system
33  // (e.g. a malformed file name).
34  INVALID_ARGUMENT,
35 
36  // Operation is not implemented or not supported/enabled.
37  UNIMPLEMENTED,
38 
39  // Cannot open file.
40  FILE_FAILURE,
41 
42  // End of stream.
43  END_OF_STREAM,
44 
45  // Failure to get HTTP response successfully,
46  HTTP_FAILURE,
47 
48  // Unable to parse the media file.
49  PARSER_FAILURE,
50 
51  // Fail to mux the media file.
52  MUXER_FAILURE,
53 
54  // This track fragment is finalized.
55  FRAGMENT_FINALIZED,
56 
57  // Server errors. Receives malformed response from server.
58  SERVER_ERROR,
59 
60  // Internal errors. Some invariants have been broken.
61  INTERNAL_ERROR,
62 
63  // The operation was stopped.
64  STOPPED,
65 
66  // The operation timed out.
67  TIME_OUT,
68 
69  // Value was not found.
70  NOT_FOUND,
71 
72  // The entity that a client attempted to create (e.g., file or directory)
73  // already exists.
74  ALREADY_EXISTS,
75 };
76 
77 } // namespace error
78 
79 class Status {
80  public:
82  Status() : error_code_(error::OK) {}
83 
87  Status(error::Code error_code, const std::string& error_message)
88  : error_code_(error_code) {
89  if (!ok())
90  error_message_ = error_message;
91  }
92 
93  ~Status() {}
94 
97  static const Status OK; // Identical to 0-arg constructor.
98  static const Status UNKNOWN;
100 
104  void SetError(error::Code error_code, const std::string& error_message) {
105  if (error_code == error::OK) {
106  Clear();
107  } else {
108  error_code_ = error_code;
109  error_message_ = error_message;
110  }
111  }
112 
121  void Update(const Status& new_status) {
122  if (ok())
123  *this = new_status;
124  }
125 
127  void Clear() {
128  error_code_ = error::OK;
129  error_message_ = "";
130  }
131 
132  bool ok() const { return error_code_ == error::OK; }
133  error::Code error_code() const { return error_code_; }
134  const std::string& error_message() const { return error_message_; }
135 
136  bool operator==(const Status& x) const {
137  return error_code_ == x.error_code() && error_message_ == x.error_message();
138  }
139  bool operator!=(const Status& x) const { return !(*this == x); }
140 
144  bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
145 
147  std::string ToString() const;
148 
149  void Swap(Status* other) {
150  error::Code error_code = error_code_;
151  error_code_ = other->error_code_;
152  other->error_code_ = error_code;
153  error_message_.swap(other->error_message_);
154  }
155 
156  private:
157  error::Code error_code_;
158  std::string error_message_;
159 
160  // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
161  // generated copy constructor and assignment operator.
162 };
163 
164 std::ostream& operator<<(std::ostream& os, const Status& x);
165 
166 } // namespace media
167 } // namespace shaka
168 
169 #endif // MEDIA_BASE_STATUS_H_
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:104
bool Matches(const Status &x) const
Definition: status.h:144
std::string ToString() const
Definition: status.cc:60
void Update(const Status &new_status)
Definition: status.h:121
Status()
Creates a "successful" status.
Definition: status.h:82
void Clear()
Clear this status object to contain the OK code and no error message.
Definition: status.h:127
Status(error::Code error_code, const std::string &error_message)
Definition: status.h:87