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  // Error when trying to generate trick play stream.
83  TRICK_PLAY_ERROR,
84 };
85 
86 } // namespace error
87 
88 class Status {
89  public:
91  Status() : error_code_(error::OK) {}
92 
96  Status(error::Code error_code, const std::string& error_message)
97  : error_code_(error_code) {
98  if (!ok())
99  error_message_ = error_message;
100  }
101 
102  ~Status() {}
103 
106  static const Status OK; // Identical to 0-arg constructor.
107  static const Status UNKNOWN;
109 
113  void SetError(error::Code error_code, const std::string& error_message) {
114  if (error_code == error::OK) {
115  Clear();
116  } else {
117  error_code_ = error_code;
118  error_message_ = error_message;
119  }
120  }
121 
130  void Update(const Status& new_status) {
131  if (ok())
132  *this = new_status;
133  }
134 
136  void Clear() {
137  error_code_ = error::OK;
138  error_message_ = "";
139  }
140 
141  bool ok() const { return error_code_ == error::OK; }
142  error::Code error_code() const { return error_code_; }
143  const std::string& error_message() const { return error_message_; }
144 
145  bool operator==(const Status& x) const {
146  return error_code_ == x.error_code() && error_message_ == x.error_message();
147  }
148  bool operator!=(const Status& x) const { return !(*this == x); }
149 
153  bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
154 
156  std::string ToString() const;
157 
158  void Swap(Status* other) {
159  error::Code error_code = error_code_;
160  error_code_ = other->error_code_;
161  other->error_code_ = error_code;
162  error_message_.swap(other->error_message_);
163  }
164 
165  private:
166  error::Code error_code_;
167  std::string error_message_;
168 
169  // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
170  // generated copy constructor and assignment operator.
171 };
172 
173 std::ostream& operator<<(std::ostream& os, const Status& x);
174 
175 } // namespace media
176 } // namespace shaka
177 
178 #endif // MEDIA_BASE_STATUS_H_
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:113
bool Matches(const Status &x) const
Definition: status.h:153
std::string ToString() const
Definition: status.cc:68
void Update(const Status &new_status)
Definition: status.h:130
Status()
Creates a "successful" status.
Definition: status.h:91
void Clear()
Clear this status object to contain the OK code and no error message.
Definition: status.h:136
Status(error::Code error_code, const std::string &error_message)
Definition: status.h:96