diff options
author | lizatretyakova <lizatretyakova@google.com> | 2022-03-17 05:40:21 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-03-17 05:41:09 -0700 |
commit | 8529e2451d92fb88f521f05edc8deb9596a1c5f6 (patch) | |
tree | f73ffe16364dcbf52df39c5387fdc42dc010cd27 /cc/util | |
parent | 6cd4a08f949bf7387a4d6646794f8557dec3eb94 (diff) | |
download | tink-8529e2451d92fb88f521f05edc8deb9596a1c5f6.tar.gz |
Add value() method to crypto::tink::util::StatusOr, in order to make it compatible with absl::StatusOr. Part of the Tink migration towards absl::Status{,Or}.
PiperOrigin-RevId: 435321035
Diffstat (limited to 'cc/util')
-rw-r--r-- | cc/util/statusor.h | 26 | ||||
-rw-r--r-- | cc/util/statusor_test.cc | 11 |
2 files changed, 36 insertions, 1 deletions
diff --git a/cc/util/statusor.h b/cc/util/statusor.h index ecd1f1be3..1ed2716c7 100644 --- a/cc/util/statusor.h +++ b/cc/util/statusor.h @@ -101,6 +101,25 @@ class StatusOr { return *std::move(value_); } + // Returns value if ok(), otherwise crashes if exceptions are disabled OR + // throws if exceptions are enabled. + inline const T& value() const& { + if (!ok()) AbortWithMessageFrom(status_); + return *value_; + } + inline T& value() & { + if (!ok()) AbortWithMessageFrom(status_); + return *value_; + } + inline const T&& value() const&& { + if (!ok()) AbortWithMessageFrom(std::move(status_)); + return *std::move(value_); + } + inline T&& value() && { + if (!ok()) AbortWithMessageFrom(std::move(status_)); + return *std::move(value_); + } + // Implicitly convertible to absl::StatusOr. Implicit conversions explicitly // allowed by style arbiter waiver in cl/351594378. operator ::absl::StatusOr<T>() const&; // NOLINT @@ -150,6 +169,13 @@ class StatusOr { } } + void AbortWithMessageFrom(crypto::tink::util::Status status) { + std::cerr << "Attempting to fetch value instead of handling error\n"; + std::cerr << status.ToString(); + std::abort(); + } + + Status status_; absl::optional<T> value_; }; diff --git a/cc/util/statusor_test.cc b/cc/util/statusor_test.cc index f49086898..5c1ee68c4 100644 --- a/cc/util/statusor_test.cc +++ b/cc/util/statusor_test.cc @@ -100,6 +100,7 @@ TEST(StatusOrTest, AssignToErrorStatus) { error_initially = ok_initially; ASSERT_THAT(error_initially.status(), IsOk()); ASSERT_THAT(error_initially.ValueOrDie(), Eq("Hi")); + ASSERT_THAT(error_initially.value(), Eq("Hi")); } // This tests that when we assign to something which is previously an error and @@ -114,15 +115,23 @@ TEST(StatusOrTest, AssignToErrorStatusImplicitConvertible) { error_initially = ok_initially; ASSERT_THAT(error_initially.status(), IsOk()); ASSERT_THAT(error_initially.ValueOrDie(), Eq("Hi")); + ASSERT_THAT(error_initially.value(), Eq("Hi")); } -TEST(StatusOrTest, MoveOutMoveOnly) { +TEST(StatusOrTest, MoveOutMoveOnlyValueOrDie) { StatusOr<std::unique_ptr<int>> status_or_unique_ptr_int = absl::make_unique<int>(10); std::unique_ptr<int> ten = std::move(status_or_unique_ptr_int.ValueOrDie()); ASSERT_THAT(*ten, Eq(10)); } +TEST(StatusOrTest, MoveOutMoveOnlyValue) { + StatusOr<std::unique_ptr<int>> status_or_unique_ptr_int = + absl::make_unique<int>(10); + std::unique_ptr<int> ten = std::move(status_or_unique_ptr_int.value()); + ASSERT_THAT(*ten, Eq(10)); +} + TEST(StatusOrTest, TestValueConst) { const int kI = 4; const absl::StatusOr<int> thing(kI); |