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  // Failed to do the encryption.
52  ENCRYPTION_FAILURE,
53 
54  // Error when trying to do chunking.
55  CHUNKING_ERROR,
56 
57  // Fail to mux the media file.
58  MUXER_FAILURE,
59 
60  // This track fragment is finalized.
61  FRAGMENT_FINALIZED,
62 
63  // Server errors. Receives malformed response from server.
64  SERVER_ERROR,
65 
66  // Internal errors. Some invariants have been broken.
67  INTERNAL_ERROR,
68 
69  // The operation was stopped.
70  STOPPED,
71 
72  // The operation timed out.
73  TIME_OUT,
74 
75  // Value was not found.
76  NOT_FOUND,
77 
78  // The entity that a client attempted to create (e.g., file or directory)
79  // already exists.
80  ALREADY_EXISTS,
81 };
82 
83 } // namespace error
84 
85 class Status {
86  public:
88  Status() : error_code_(error::OK) {}
89 
93  Status(error::Code error_code, const std::string& error_message)
94  : error_code_(error_code) {
95  if (!ok())
96  error_message_ = error_message;
97  }
98 
99  ~Status() {}
100 
103  static const Status OK; // Identical to 0-arg constructor.
104  static const Status UNKNOWN;
106 
110  void SetError(error::Code error_code, const std::string& error_message) {
111  if (error_code == error::OK) {
112  Clear();
113  } else {
114  error_code_ = error_code;
115  error_message_ = error_message;
116  }
117  }
118 
127  void Update(const Status& new_status) {
128  if (ok())
129  *this = new_status;
130  }
131 
133  void Clear() {
134  error_code_ = error::OK;
135  error_message_ = "";
136  }
137 
138  bool ok() const { return error_code_ == error::OK; }
139  error::Code error_code() const { return error_code_; }
140  const std::string& error_message() const { return error_message_; }
141 
142  bool operator==(const Status& x) const {
143  return error_code_ == x.error_code() && error_message_ == x.error_message();
144  }
145  bool operator!=(const Status& x) const { return !(*this == x); }
146 
150  bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
151 
153  std::string ToString() const;
154 
155  void Swap(Status* other) {
156  error::Code error_code = error_code_;
157  error_code_ = other->error_code_;
158  other->error_code_ = error_code;
159  error_message_.swap(other->error_message_);
160  }
161 
162  private:
163  error::Code error_code_;
164  std::string error_message_;
165 
166  // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
167  // generated copy constructor and assignment operator.
168 };
169 
170 std::ostream& operator<<(std::ostream& os, const Status& x);
171 
172 } // namespace media
173 } // namespace shaka
174 
175 #endif // MEDIA_BASE_STATUS_H_
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:110
bool Matches(const Status &x) const
Definition: status.h:150
std::string ToString() const
Definition: status.cc:68
void Update(const Status &new_status)
Definition: status.h:127
Status()
Creates a "successful" status.
Definition: status.h:88
void Clear()
Clear this status object to contain the OK code and no error message.
Definition: status.h:133
Status(error::Code error_code, const std::string &error_message)
Definition: status.h:93