Cleans up Status class

- Removed SetError, Swap, Matches functions, which are not used
- Added moveable version of Update function
- Added VLOG(1) warning in Status for non-ok status
- Replaced OS_WIN with _WIN32 as OS_WIN may not have been defined.

Change-Id: Ib6b7aab6e6fee270937b150f1e4bf993e914a568
This commit is contained in:
KongQun Yang 2018-01-22 16:09:14 -08:00
parent 84f1f96f05
commit 610e112d16
4 changed files with 29 additions and 101 deletions

View File

@ -269,9 +269,9 @@ void Demuxer::ParserInitEvent(
stream_info->set_language(iter->second); stream_info->set_language(iter->second);
} }
if (stream_info->is_encrypted()) { if (stream_info->is_encrypted()) {
init_event_status_.SetError( init_event_status_.Update(Status(error::INVALID_ARGUMENT,
error::INVALID_ARGUMENT, "A decryption key source is not "
"A decryption key source is not provided for an encrypted stream."); "provided for an encrypted stream."));
} else { } else {
init_event_status_.Update( init_event_status_.Update(
DispatchStreamInfo(stream_index, stream_info)); DispatchStreamInfo(stream_index, stream_info));

View File

@ -64,6 +64,20 @@ std::string ErrorCodeToString(Code error_code) {
const Status Status::OK = Status(error::OK, ""); const Status Status::OK = Status(error::OK, "");
const Status Status::UNKNOWN = Status(error::UNKNOWN, ""); const Status Status::UNKNOWN = Status(error::UNKNOWN, "");
Status::Status(error::Code error_code, const std::string& error_message)
: error_code_(error_code) {
if (!ok()) {
error_message_ = error_message;
if (!error_message.empty())
VLOG(1) << ToString();
}
}
void Status::Update(Status new_status) {
if (ok())
*this = std::move(new_status);
}
std::string Status::ToString() const { std::string Status::ToString() const {
if (error_code_ == error::OK) if (error_code_ == error::OK)
return "OK"; return "OK";

View File

@ -11,7 +11,7 @@
#include <string> #include <string>
#if defined(SHARED_LIBRARY_BUILD) #if defined(SHARED_LIBRARY_BUILD)
#if defined(OS_WIN) #if defined(_WIN32)
#if defined(SHAKA_IMPLEMENTATION) #if defined(SHAKA_IMPLEMENTATION)
#define SHAKA_EXPORT __declspec(dllexport) #define SHAKA_EXPORT __declspec(dllexport)
@ -19,7 +19,7 @@
#define SHAKA_EXPORT __declspec(dllimport) #define SHAKA_EXPORT __declspec(dllimport)
#endif // defined(SHAKA_IMPLEMENTATION) #endif // defined(SHAKA_IMPLEMENTATION)
#else // defined(OS_WIN) #else // defined(_WIN32)
#if defined(SHAKA_IMPLEMENTATION) #if defined(SHAKA_IMPLEMENTATION)
#define SHAKA_EXPORT __attribute__((visibility("default"))) #define SHAKA_EXPORT __attribute__((visibility("default")))
@ -27,7 +27,7 @@
#define SHAKA_EXPORT #define SHAKA_EXPORT
#endif #endif
#endif // defined(OS_WIN) #endif // defined(_WIN32)
#else // defined(SHARED_LIBRARY_BUILD) #else // defined(SHARED_LIBRARY_BUILD)
#define SHAKA_EXPORT #define SHAKA_EXPORT
@ -115,13 +115,7 @@ class SHAKA_EXPORT Status {
/// Create a status with the specified code, and error message. /// Create a status with the specified code, and error message.
/// If "error_code == error::OK", error_message is ignored and a Status /// If "error_code == error::OK", error_message is ignored and a Status
/// object identical to Status::OK is constructed. /// object identical to Status::OK is constructed.
Status(error::Code error_code, const std::string& error_message) Status(error::Code error_code, const std::string& error_message);
: error_code_(error_code) {
if (!ok())
error_message_ = error_message;
}
~Status() {}
/// @name Some pre-defined Status objects. /// @name Some pre-defined Status objects.
/// @{ /// @{
@ -129,18 +123,6 @@ class SHAKA_EXPORT Status {
static const Status UNKNOWN; static const Status UNKNOWN;
/// @} /// @}
/// Store the specified error in this Status object.
/// If "error_code == error::OK", error_message is ignored and a Status
/// object identical to Status::OK is constructed.
void SetError(error::Code error_code, const std::string& error_message) {
if (error_code == error::OK) {
Clear();
} else {
error_code_ = error_code;
error_message_ = error_message;
}
}
/// If "ok()", stores "new_status" into *this. If "!ok()", preserves /// If "ok()", stores "new_status" into *this. If "!ok()", preserves
/// the current "error_code()/error_message()", /// the current "error_code()/error_message()",
/// ///
@ -149,16 +131,7 @@ class SHAKA_EXPORT Status {
/// if (overall_status.ok()) overall_status = new_status /// if (overall_status.ok()) overall_status = new_status
/// Use: /// Use:
/// overall_status.Update(new_status); /// overall_status.Update(new_status);
void Update(const Status& new_status) { void Update(Status new_status);
if (ok())
*this = new_status;
}
/// Clear this status object to contain the OK code and no error message.
void Clear() {
error_code_ = error::OK;
error_message_ = "";
}
bool ok() const { return error_code_ == error::OK; } bool ok() const { return error_code_ == error::OK; }
error::Code error_code() const { return error_code_; } error::Code error_code() const { return error_code_; }
@ -169,21 +142,9 @@ class SHAKA_EXPORT Status {
} }
bool operator!=(const Status& x) const { return !(*this == x); } bool operator!=(const Status& x) const { return !(*this == x); }
/// @return true iff this has the same error_code as "x", i.e., the two
/// Status objects are identical except possibly for the error
/// message.
bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
/// @return A combination of the error code name and message. /// @return A combination of the error code name and message.
std::string ToString() const; std::string ToString() const;
void Swap(Status* other) {
error::Code error_code = error_code_;
error_code_ = other->error_code_;
other->error_code_ = error_code;
error_message_.swap(other->error_message_);
}
private: private:
error::Code error_code_; error::Code error_code_;
std::string error_message_; std::string error_message_;

View File

@ -40,18 +40,6 @@ TEST(Status, ConstructorOK) {
CheckStatus(Status(error::OK, "msg"), error::OK, ""); CheckStatus(Status(error::OK, "msg"), error::OK, "");
} }
TEST(Status, SetError) {
Status status;
status.SetError(error::CANCELLED, "message");
CheckStatus(status, error::CANCELLED, "message");
}
TEST(Status, SetErrorOK) {
Status status(error::CANCELLED, "message");
status.SetError(error::OK, "msg");
CheckStatus(status, error::OK, "");
}
TEST(Status, Unknown) { TEST(Status, Unknown) {
CheckStatus(Status::UNKNOWN, error::UNKNOWN, ""); CheckStatus(Status::UNKNOWN, error::UNKNOWN, "");
} }
@ -60,12 +48,6 @@ TEST(Status, Filled) {
CheckStatus(Status(error::CANCELLED, "message"), error::CANCELLED, "message"); CheckStatus(Status(error::CANCELLED, "message"), error::CANCELLED, "message");
} }
TEST(Status, Clear) {
Status status(error::CANCELLED, "message");
status.Clear();
CheckStatus(status, error::OK, "");
}
TEST(Status, Copy) { TEST(Status, Copy) {
Status a(error::CANCELLED, "message"); Status a(error::CANCELLED, "message");
Status b(a); Status b(a);
@ -92,7 +74,7 @@ TEST(Status, Update) {
Status s; Status s;
s.Update(Status::OK); s.Update(Status::OK);
ASSERT_TRUE(s.ok()); ASSERT_TRUE(s.ok());
Status a(error::CANCELLED, "message"); const Status a(error::CANCELLED, "message");
s.Update(a); s.Update(a);
ASSERT_EQ(s, a); ASSERT_EQ(s, a);
Status b(error::UNIMPLEMENTED, "other message"); Status b(error::UNIMPLEMENTED, "other message");
@ -103,41 +85,12 @@ TEST(Status, Update) {
ASSERT_FALSE(s.ok()); ASSERT_FALSE(s.ok());
} }
TEST(Status, Swap) { // Will trigger copy ellision.
Status a(error::CANCELLED, "message"); TEST(Status, Update2) {
Status b = a; Status s;
Status c; ASSERT_TRUE(s.ok());
c.Swap(&a); s.Update(Status(error::INVALID_ARGUMENT, "some message"));
ASSERT_EQ(c, b); ASSERT_EQ(error::INVALID_ARGUMENT, s.error_code());
ASSERT_EQ(a, Status::OK);
}
TEST(Status, MatchOK) {
ASSERT_TRUE(Status().Matches(Status::OK));
}
TEST(Status, MatchSame) {
Status a(error::UNKNOWN, "message");
Status b(error::UNKNOWN, "message");
ASSERT_TRUE(a.Matches(b));
}
TEST(Status, MatchCopy) {
Status a(error::UNKNOWN, "message");
Status b = a;
ASSERT_TRUE(a.Matches(b));
}
TEST(Status, MatchDifferentCode) {
Status a(error::UNKNOWN, "message");
Status b(error::CANCELLED, "message");
ASSERT_TRUE(!a.Matches(b));
}
TEST(Status, MatchDifferentMessage) {
Status a(error::UNKNOWN, "message");
Status b(error::UNKNOWN, "another");
ASSERT_TRUE(a.Matches(b));
} }
TEST(Status, EqualsOK) { TEST(Status, EqualsOK) {