You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by ti...@apache.org on 2022/10/03 01:51:20 UTC
[incubator-kvrocks] branch unstable updated: feat: add StatusOr#ValueOr (#936)
This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 648df4b feat: add StatusOr#ValueOr (#936)
648df4b is described below
commit 648df4ba27914041ddd3a26765c9e2d543633e8f
Author: Twice <tw...@gmail.com>
AuthorDate: Mon Oct 3 09:51:14 2022 +0800
feat: add StatusOr#ValueOr (#936)
---
src/status.h | 32 ++++++++++++++++++++++++++++++++
tests/cppunit/status_test.cc | 20 ++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/src/status.h b/src/status.h
index a742b4d..89b0872 100644
--- a/src/status.h
+++ b/src/status.h
@@ -209,6 +209,38 @@ struct StatusOr {
return std::move(value_);
}
+ const value_type& ValueOr(const value_type& v) const& {
+ if (IsOK()) {
+ return GetValue();
+ } else {
+ return v;
+ }
+ }
+
+ value_type ValueOr(value_type&& v) const& {
+ if (IsOK()) {
+ return GetValue();
+ } else {
+ return std::move(v);
+ }
+ }
+
+ value_type ValueOr(const T& v) && {
+ if (IsOK()) {
+ return std::move(*this).GetValue();
+ } else {
+ return v;
+ }
+ }
+
+ value_type&& ValueOr(T&& v) && {
+ if (IsOK()) {
+ return std::move(*this).GetValue();
+ } else {
+ return std::move(v);
+ }
+ }
+
const value_type& GetValue() const& {
CHECK(*this);
return value_;
diff --git a/tests/cppunit/status_test.cc b/tests/cppunit/status_test.cc
index dac3683..b039086 100644
--- a/tests/cppunit/status_test.cc
+++ b/tests/cppunit/status_test.cc
@@ -144,3 +144,23 @@ TEST(StatusOr, SharedPtr) {
ASSERT_EQ(val, 0);
}
+
+TEST(StatusOr, UniquePtr) {
+ StatusOr<std::unique_ptr<int>> x(new int(1));
+
+ ASSERT_EQ(**x, 1);
+}
+
+TEST(StatusOr, ValueOr) {
+ StatusOr<int> a(1), b(Status::NotOK, "err");
+ ASSERT_EQ(a.ValueOr(0), 1);
+ ASSERT_EQ(b.ValueOr(233), 233);
+ ASSERT_EQ(StatusOr<int>(1).ValueOr(0), 1);
+
+ StatusOr<std::string> c("hello"), d(Status::NotOK, "err");
+ ASSERT_EQ(c.ValueOr("hi"), "hello");
+ ASSERT_EQ(d.ValueOr("hi"), "hi");
+ ASSERT_EQ(StatusOr<std::string>("hello").ValueOr("hi"), "hello");
+ std::string s = "hi";
+ ASSERT_EQ(StatusOr<std::string>(Status::NotOK, "").ValueOr(s), "hi");
+}