kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [3/3] incubator-kudu git commit: Support move construction and assignment for Status
Date Fri, 22 Jan 2016 23:05:23 GMT
Support move construction and assignment for Status

This ought to avoid some extra allocations and copies in
the common case of returning Status from a function.

The new support is enabled only in C++11, since status.h
is also sometimes included by non-C++11 client users.

Change-Id: Id91aff98764d6df4857c0123687dc417c3d97281
Reviewed-on: http://gerrit.cloudera.org:8080/1871
Reviewed-by: Dan Burkert <dan@cloudera.com>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/05add04e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/05add04e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/05add04e

Branch: refs/heads/master
Commit: 05add04e1b560c537ccb759f3ea272dfef7bf2d0
Parents: 0e88646
Author: Todd Lipcon <todd@apache.org>
Authored: Thu Jan 21 19:13:14 2016 -0800
Committer: Todd Lipcon <todd@apache.org>
Committed: Fri Jan 22 23:05:01 2016 +0000

----------------------------------------------------------------------
 src/kudu/util/status-test.cc | 51 +++++++++++++++++++++++++++++++++++++++
 src/kudu/util/status.h       | 20 +++++++++++++++
 2 files changed, 71 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/05add04e/src/kudu/util/status-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/status-test.cc b/src/kudu/util/status-test.cc
index 109767a..afbecd2 100644
--- a/src/kudu/util/status-test.cc
+++ b/src/kudu/util/status-test.cc
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <vector>
 #include "kudu/util/status.h"
+#include "kudu/util/test_util.h"
 
 using std::string;
 
@@ -61,4 +62,54 @@ TEST(StatusTest, TestMemoryUsage) {
       "file error", "some other thing", ENOTDIR).memory_footprint_excluding_this(), 0);
 }
 
+TEST(StatusTest, TestMoveConstructor) {
+  // OK->OK move should do nothing.
+  {
+    Status src = Status::OK();
+    Status dst = std::move(src);
+    ASSERT_OK(src);
+    ASSERT_OK(dst);
+  }
+
+  // Moving a not-OK status into a new one should make the moved status
+  // "OK".
+  {
+    Status src = Status::NotFound("foo");
+    Status dst = std::move(src);
+    ASSERT_OK(src);
+    ASSERT_EQ("Not found: foo", dst.ToString());
+  }
+}
+
+TEST(StatusTest, TestMoveAssignment) {
+  // OK->Bad move should clear the source status and also make the
+  // destination status OK.
+  {
+    Status src = Status::OK();
+    Status dst = Status::NotFound("orig dst");
+    dst = std::move(src);
+    ASSERT_OK(src);
+    ASSERT_OK(dst);
+  }
+
+  // Bad->Bad move.
+  {
+    Status src = Status::NotFound("orig src");
+    Status dst = Status::NotFound("orig dst");
+    dst = std::move(src);
+    ASSERT_OK(src);
+    ASSERT_EQ("Not found: orig src", dst.ToString());
+  }
+
+  // Bad->OK move
+  {
+    Status src = Status::NotFound("orig src");
+    Status dst = Status::OK();
+    dst = std::move(src);
+    ASSERT_OK(src);
+    ASSERT_EQ("Not found: orig src", dst.ToString());
+  }
+}
+
+
 }  // namespace kudu

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/05add04e/src/kudu/util/status.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/status.h b/src/kudu/util/status.h
index d4f5755..5003be3 100644
--- a/src/kudu/util/status.h
+++ b/src/kudu/util/status.h
@@ -110,6 +110,12 @@ class KUDU_EXPORT Status {
   Status(const Status& s);
   void operator=(const Status& s);
 
+#if __cplusplus >= 201103L
+  // Move the specified status.
+  Status(Status&& s);
+  void operator=(Status&& s);
+#endif
+
   // Return a success status.
   static Status OK() { return Status(); }
 
@@ -334,6 +340,20 @@ inline void Status::operator=(const Status& s) {
   }
 }
 
+#if __cplusplus >= 201103L
+inline Status::Status(Status&& s) : state_(s.state_) {
+  s.state_ = nullptr;
+}
+
+inline void Status::operator=(Status&& s) {
+  if (state_ != s.state_) {
+    delete[] state_;
+    state_ = s.state_;
+    s.state_ = nullptr;
+  }
+}
+#endif
+
 }  // namespace kudu
 
 #endif  // KUDU_UTIL_STATUS_H_


Mime
View raw message