aboutsummaryrefslogtreecommitdiff
path: root/cc/util
diff options
context:
space:
mode:
authorlizatretyakova <lizatretyakova@google.com>2022-02-22 04:59:40 -0800
committerCopybara-Service <copybara-worker@google.com>2022-02-22 05:00:25 -0800
commitb040bfc774a91b48442fb60699f4b12ad5de54bf (patch)
tree375ad3073f9384de49a3376f32033dddc23e38f6 /cc/util
parent23cf122de64dde04926d8fc405d35e8c09dc9c3b (diff)
downloadtink-b040bfc774a91b48442fb60699f4b12ad5de54bf.tar.gz
Hide crypto::tink::util::Status behind a compile-time option.
This CL is a part of Tink migration to using absl::Status{,Or} and absl::StatusCode. NOKEYCHECK=True PiperOrigin-RevId: 430184794
Diffstat (limited to 'cc/util')
-rw-r--r--cc/util/status.cc9
-rw-r--r--cc/util/status.h20
-rw-r--r--cc/util/status_test.cc2
3 files changed, 11 insertions, 20 deletions
diff --git a/cc/util/status.cc b/cc/util/status.cc
index 3b6220da8..bc7b38914 100644
--- a/cc/util/status.cc
+++ b/cc/util/status.cc
@@ -50,7 +50,6 @@ const Status& GetOk() {
}
} // namespace
-#endif
Status::Status(const ::absl::Status& status)
: code_(absl::StatusCode::kOk) {
@@ -67,7 +66,6 @@ Status::operator ::absl::Status() const {
Status::Status() : code_(absl::StatusCode::kOk), message_("") {
}
-#ifndef TINK_USE_ABSL_STATUS
Status::Status(::crypto::tink::util::error::Code error,
const std::string& error_message)
: code_(static_cast<absl::StatusCode>(error)), message_(error_message) {
@@ -75,7 +73,6 @@ Status::Status(::crypto::tink::util::error::Code error,
message_.clear();
}
}
-#endif
Status::Status(absl::StatusCode code, absl::string_view error_message)
: code_(code),
@@ -91,11 +88,9 @@ Status& Status::operator=(const Status& other) {
return *this;
}
-#ifndef TINK_USE_ABSL_STATUS
const Status& Status::CANCELLED = GetCancelled();
const Status& Status::UNKNOWN = GetUnknown();
const Status& Status::OK = GetOk();
-#endif
std::string Status::ToString() const {
if (code_ == absl::StatusCode::kOk) {
@@ -107,7 +102,6 @@ std::string Status::ToString() const {
return oss.str();
}
-#ifndef TINK_USE_ABSL_STATUS
std::string ErrorCodeString(crypto::tink::util::error::Code error) {
switch (error) {
case crypto::tink::util::error::OK:
@@ -154,13 +148,14 @@ extern ostream& operator<<(ostream& os, crypto::tink::util::error::Code code) {
os << ErrorCodeString(code);
return os;
}
-#endif
extern ostream& operator<<(ostream& os, const Status& other) {
os << other.ToString();
return os;
}
+#endif // TINK_USE_ABSL_STATUS
+
} // namespace util
} // namespace tink
diff --git a/cc/util/status.h b/cc/util/status.h
index f1fb79b2b..a130e2ee1 100644
--- a/cc/util/status.h
+++ b/cc/util/status.h
@@ -30,10 +30,10 @@ namespace crypto {
namespace tink {
namespace util {
-namespace error {
-
#ifndef TINK_USE_ABSL_STATUS
+namespace error {
+
// These values match the error codes in the codes.proto file of the original.
enum ABSL_DEPRECATED("Prefer using absl::StatusCode instead.") Code {
// Not an error; returned on success
@@ -127,8 +127,6 @@ enum ABSL_DEPRECATED("Prefer using absl::StatusCode instead.") Code {
UNAUTHENTICATED = 16,
};
-#endif
-
} // namespace error
// TODO(tholenst) Remove this compile time flag in Tink 1.5. This should not be
@@ -144,11 +142,9 @@ class Status {
// Creates an OK status
Status();
- #ifndef TINK_USE_ABSL_STATUS
// Make a Status from the specified error and message.
Status(::crypto::tink::util::error::Code error,
const std::string& error_message);
- #endif
// Abseil-compatible constructor from an error and a message
Status(absl::StatusCode code, absl::string_view error_message);
@@ -156,7 +152,6 @@ class Status {
Status& operator=(const Status& other);
- #ifndef TINK_USE_ABSL_STATUS
// Some pre-defined Status objects
ABSL_DEPRECATED("Use OkStatus() instead.")
static const Status& OK; // Identical to 0-arg constructor
@@ -164,13 +159,11 @@ class Status {
static const Status& CANCELLED;
ABSL_DEPRECATED("Use Status(absl::StatusCode::kUnknown, "") instead.")
static const Status& UNKNOWN;
- #endif
// Accessors
bool ok() const {
return code_ == absl::StatusCode::kOk;
}
- #ifndef TINK_USE_ABSL_STATUS
ABSL_DEPRECATED("Use its absl-compatible version code() instead.")
int error_code() const {
return static_cast<int>(code_);
@@ -179,7 +172,6 @@ class Status {
::crypto::tink::util::error::Code CanonicalCode() const {
return static_cast<::crypto::tink::util::error::Code>(code_);
}
- #endif
ABSL_DEPRECATED("Use its absl-compatible version message() instead.")
const std::string& error_message() const { return message_; }
@@ -216,14 +208,18 @@ inline bool Status::operator!=(const Status& other) const {
return !(*this == other);
}
-#ifndef TINK_USE_ABSL_STATUS
extern std::string ErrorCodeString(crypto::tink::util::error::Code error);
extern ::std::ostream& operator<<(::std::ostream& os,
::crypto::tink::util::error::Code code);
-#endif
extern ::std::ostream& operator<<(::std::ostream& os, const Status& other);
+#else
+
+using Status = absl::Status;
+
+#endif // TINK_USE_ABSL_STATUS
+
// Returns an OK status, equivalent to a default constructed instance.
inline Status OkStatus() { return Status(); }
diff --git a/cc/util/status_test.cc b/cc/util/status_test.cc
index 751593a52..4be75422c 100644
--- a/cc/util/status_test.cc
+++ b/cc/util/status_test.cc
@@ -45,7 +45,6 @@ TEST(StatusTest, ConvertNonOkStatus) {
ASSERT_EQ(util_status.code(), absl_status.code());
ASSERT_EQ(util_status.message(), absl_status.message());
}
-#endif
TEST(StatusTest, ConvertOkStatus) {
Status util_status = OkStatus();
@@ -53,6 +52,7 @@ TEST(StatusTest, ConvertOkStatus) {
ASSERT_TRUE(absl_status.ok());
ASSERT_EQ(absl_status.message(), "");
}
+#endif
} // namespace
} // namespace util