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