提交 8d699b2f 编写于 作者: G George Karpenkov 提交者: TensorFlower Gardener

[SE] Port * and -> operator for StaturOr from absl

PiperOrigin-RevId: 318190091
Change-Id: I740286eb8a71ebcf493fa13f9f7ced45be24048b
上级 ba51d7e1
......@@ -195,6 +195,27 @@ class StatusOr : private internal_statusor::StatusOrData<T>,
const T&& ValueOrDie() const &&;
T&& ValueOrDie() &&;
// Returns a reference to the current value.
//
// REQUIRES: this->ok() == true, otherwise the behavior is undefined.
//
// Use this->ok() or `operator bool()` to verify that there is a current
// value. Alternatively, see ValueOrDie() for a similar API that guarantees
// CHECK-failing if there is no current value.
const T& operator*() const&;
T& operator*() &;
const T&& operator*() const&&;
T&& operator*() &&;
// Returns a pointer to the current value.
//
// REQUIRES: this->ok() == true, otherwise the behavior is undefined.
//
// Use this->ok() or `operator bool()` to verify that there is a current
// value.
const T* operator->() const;
T* operator->();
T ConsumeValueOrDie() { return std::move(ValueOrDie()); }
// Ignores any errors. This method does nothing except potentially suppress
......@@ -303,6 +324,42 @@ T&& StatusOr<T>::ValueOrDie() && {
return std::move(this->data_);
}
template <typename T>
const T* StatusOr<T>::operator->() const {
this->EnsureOk();
return &this->data_;
}
template <typename T>
T* StatusOr<T>::operator->() {
this->EnsureOk();
return &this->data_;
}
template <typename T>
const T& StatusOr<T>::operator*() const& {
this->EnsureOk();
return this->data_;
}
template <typename T>
T& StatusOr<T>::operator*() & {
this->EnsureOk();
return this->data_;
}
template <typename T>
const T&& StatusOr<T>::operator*() const&& {
this->EnsureOk();
return std::move(this->data_);
}
template <typename T>
T&& StatusOr<T>::operator*() && {
this->EnsureOk();
return std::move(this->data_);
}
template <typename T>
void StatusOr<T>::IgnoreError() const {
// no-op
......
......@@ -413,6 +413,26 @@ TEST(StatusOr, TestPointerValueConst) {
EXPECT_EQ(&kI, thing.ValueOrDie());
}
TEST(StatusOr, TestArrowOperator) {
StatusOr<std::unique_ptr<int>> uptr = ReturnUniquePtr();
EXPECT_EQ(*uptr->get(), 0);
}
TEST(StatusOr, TestArrowOperatorNotOk) {
StatusOr<Base1> error(Status(tensorflow::error::CANCELLED, "cancelled"));
EXPECT_DEATH(error->pad_++, "cancelled");
}
TEST(StatusOr, TestStarOperator) {
StatusOr<std::unique_ptr<int>> uptr = ReturnUniquePtr();
EXPECT_EQ(**uptr, 0);
}
TEST(StatusOr, TestStarOperatorDeath) {
StatusOr<Base1> error(Status(tensorflow::error::CANCELLED, "cancelled"));
EXPECT_DEATH(*error, "cancelled");
}
// NOTE(tucker): StatusOr does not support this kind
// of resize op.
// TEST(StatusOr, StatusOrVectorOfUniquePointerCanResize) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册