DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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 edash_packager {
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 
73 } // namespace error
74 
75 class Status {
76  public:
78  Status() : error_code_(error::OK) {}
79 
83  Status(error::Code error_code, const std::string& error_message)
84  : error_code_(error_code) {
85  if (!ok())
86  error_message_ = error_message;
87  }
88 
89  ~Status() {}
90 
93  static const Status OK; // Identical to 0-arg constructor.
94  static const Status UNKNOWN;
96 
100  void SetError(error::Code error_code, const std::string& error_message) {
101  if (error_code == error::OK) {
102  Clear();
103  } else {
104  error_code_ = error_code;
105  error_message_ = error_message;
106  }
107  }
108 
117  void Update(const Status& new_status) {
118  if (ok())
119  *this = new_status;
120  }
121 
123  void Clear() {
124  error_code_ = error::OK;
125  error_message_ = "";
126  }
127 
128  bool ok() const { return error_code_ == error::OK; }
129  error::Code error_code() const { return error_code_; }
130  const std::string& error_message() const { return error_message_; }
131 
132  bool operator==(const Status& x) const {
133  return error_code_ == x.error_code() && error_message_ == x.error_message();
134  }
135  bool operator!=(const Status& x) const { return !(*this == x); }
136 
140  bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
141 
143  std::string ToString() const;
144 
145  void Swap(Status* other) {
146  error::Code error_code = error_code_;
147  error_code_ = other->error_code_;
148  other->error_code_ = error_code;
149  error_message_.swap(other->error_message_);
150  }
151 
152  private:
153  error::Code error_code_;
154  std::string error_message_;
155 
156  // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
157  // generated copy constructor and assignment operator.
158 };
159 
160 std::ostream& operator<<(std::ostream& os, const Status& x);
161 
162 } // namespace media
163 } // namespace edash_packager
164 
165 #endif // MEDIA_BASE_STATUS_H_
void Update(const Status &new_status)
Definition: status.h:117
Status()
Creates a "successful" status.
Definition: status.h:78
void Clear()
Clear this status object to contain the OK code and no error message.
Definition: status.h:123
Status(error::Code error_code, const std::string &error_message)
Definition: status.h:83
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:100
std::string ToString() const
Definition: status.cc:60
bool Matches(const Status &x) const
Definition: status.h:140