Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 PACKAGER_STATUS_H_
8 #define PACKAGER_STATUS_H_
9 
10 #include <iostream>
11 #include <string>
12 
13 #if defined(SHARED_LIBRARY_BUILD)
14 #if defined(OS_WIN)
15 
16 #if defined(SHAKA_IMPLEMENTATION)
17 #define SHAKA_EXPORT __declspec(dllexport)
18 #else
19 #define SHAKA_EXPORT __declspec(dllimport)
20 #endif // defined(SHAKA_IMPLEMENTATION)
21 
22 #else // defined(OS_WIN)
23 
24 #if defined(SHAKA_IMPLEMENTATION)
25 #define SHAKA_EXPORT __attribute__((visibility("default")))
26 #else
27 #define SHAKA_EXPORT
28 #endif
29 
30 #endif // defined(OS_WIN)
31 
32 #else // defined(SHARED_LIBRARY_BUILD)
33 #define SHAKA_EXPORT
34 #endif // defined(SHARED_LIBRARY_BUILD)
35 
36 namespace shaka {
37 
38 namespace error {
39 
41 enum Code {
42  // Not an error; returned on success
43  OK,
44 
45  // Unknown error. An example of where this error may be returned is
46  // errors raised by APIs that do not return enough error information
47  // may be converted to this error.
48  UNKNOWN,
49 
50  // The operation was cancelled (typically by the caller).
51  CANCELLED,
52 
53  // Client specified an invalid argument. INVALID_ARGUMENT indicates
54  // arguments that are problematic regardless of the state of the system
55  // (e.g. a malformed file name).
56  INVALID_ARGUMENT,
57 
58  // Operation is not implemented or not supported/enabled.
59  UNIMPLEMENTED,
60 
61  // Cannot open file.
62  FILE_FAILURE,
63 
64  // End of stream.
65  END_OF_STREAM,
66 
67  // Failure to get HTTP response successfully,
68  HTTP_FAILURE,
69 
70  // Unable to parse the media file.
71  PARSER_FAILURE,
72 
73  // Failed to do the encryption.
74  ENCRYPTION_FAILURE,
75 
76  // Error when trying to do chunking.
77  CHUNKING_ERROR,
78 
79  // Fail to mux the media file.
80  MUXER_FAILURE,
81 
82  // This track fragment is finalized.
83  FRAGMENT_FINALIZED,
84 
85  // Server errors. Receives malformed response from server.
86  SERVER_ERROR,
87 
88  // Internal errors. Some invariants have been broken.
89  INTERNAL_ERROR,
90 
91  // The operation was stopped.
92  STOPPED,
93 
94  // The operation timed out.
95  TIME_OUT,
96 
97  // Value was not found.
98  NOT_FOUND,
99 
100  // The entity that a client attempted to create (e.g., file or directory)
101  // already exists.
102  ALREADY_EXISTS,
103 
104  // Error when trying to generate trick play stream.
105  TRICK_PLAY_ERROR,
106 };
107 
108 } // namespace error
109 
110 class SHAKA_EXPORT Status {
111  public:
113  Status() : error_code_(error::OK) {}
114 
118  Status(error::Code error_code, const std::string& error_message)
119  : error_code_(error_code) {
120  if (!ok())
121  error_message_ = error_message;
122  }
123 
124  ~Status() {}
125 
128  static const Status OK; // Identical to 0-arg constructor.
129  static const Status UNKNOWN;
131 
135  void SetError(error::Code error_code, const std::string& error_message) {
136  if (error_code == error::OK) {
137  Clear();
138  } else {
139  error_code_ = error_code;
140  error_message_ = error_message;
141  }
142  }
143 
152  void Update(const Status& new_status) {
153  if (ok())
154  *this = new_status;
155  }
156 
158  void Clear() {
159  error_code_ = error::OK;
160  error_message_ = "";
161  }
162 
163  bool ok() const { return error_code_ == error::OK; }
164  error::Code error_code() const { return error_code_; }
165  const std::string& error_message() const { return error_message_; }
166 
167  bool operator==(const Status& x) const {
168  return error_code_ == x.error_code() && error_message_ == x.error_message();
169  }
170  bool operator!=(const Status& x) const { return !(*this == x); }
171 
175  bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
176 
178  std::string ToString() const;
179 
180  void Swap(Status* other) {
181  error::Code error_code = error_code_;
182  error_code_ = other->error_code_;
183  other->error_code_ = error_code;
184  error_message_.swap(other->error_message_);
185  }
186 
187  private:
188  error::Code error_code_;
189  std::string error_message_;
190 
191  // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
192  // generated copy constructor and assignment operator.
193 };
194 
195 SHAKA_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);
196 
197 } // namespace shaka
198 
199 #endif // PACKAGER_STATUS_H_
void Clear()
Clear this status object to contain the OK code and no error message.
Definition: status.h:158
bool Matches(const Status &x) const
Definition: status.h:175
void Update(const Status &new_status)
Definition: status.h:152
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:135
Status()
Creates a "successful" status.
Definition: status.h:113
Status(error::Code error_code, const std::string &error_message)
Definition: status.h:118