diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bf3df3..d49c31e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -277,6 +277,7 @@ if(LEVELDB_BUILD_TESTS) leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue200_test.cc") leveldb_test("${PROJECT_SOURCE_DIR}/util/env_test.cc") + leveldb_test("${PROJECT_SOURCE_DIR}/util/status_test.cc") if(NOT BUILD_SHARED_LIBS) leveldb_test("${PROJECT_SOURCE_DIR}/db/autocompact_test.cc") diff --git a/include/leveldb/status.h b/include/leveldb/status.h index 39d692d..ee9fac2 100644 --- a/include/leveldb/status.h +++ b/include/leveldb/status.h @@ -13,6 +13,7 @@ #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_ #define STORAGE_LEVELDB_INCLUDE_STATUS_H_ +#include #include #include "leveldb/export.h" #include "leveldb/slice.h" @@ -22,12 +23,14 @@ namespace leveldb { class LEVELDB_EXPORT Status { public: // Create a success status. - Status() : state_(nullptr) { } + Status() noexcept : state_(nullptr) { } ~Status() { delete[] state_; } - // Copy the specified status. - Status(const Status& s); - void operator=(const Status& s); + Status(const Status& rhs); + Status& operator=(const Status& rhs); + + Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; } + Status& operator=(Status&& rhs) noexcept; // Return a success status. static Status OK() { return Status(); } @@ -96,16 +99,21 @@ class LEVELDB_EXPORT Status { static const char* CopyState(const char* s); }; -inline Status::Status(const Status& s) { - state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_); +inline Status::Status(const Status& rhs) { + state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_); } -inline void Status::operator=(const Status& s) { - // The following condition catches both aliasing (when this == &s), - // and the common case where both s and *this are ok. - if (state_ != s.state_) { +inline Status& Status::operator=(const Status& rhs) { + // The following condition catches both aliasing (when this == &rhs), + // and the common case where both rhs and *this are ok. + if (state_ != rhs.state_) { delete[] state_; - state_ = (s.state_ == nullptr) ? nullptr : CopyState(s.state_); + state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_); } + return *this; +} +inline Status& Status::operator=(Status&& rhs) noexcept { + std::swap(state_, rhs.state_); + return *this; } } // namespace leveldb diff --git a/util/status_test.cc b/util/status_test.cc new file mode 100644 index 0000000..7ed3b9e --- /dev/null +++ b/util/status_test.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2018 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#include + +#include "leveldb/slice.h" +#include "leveldb/status.h" +#include "util/testharness.h" + +namespace leveldb { + +TEST(Status, MoveConstructor) { + { + Status ok = Status::OK(); + Status ok2 = std::move(ok); + + ASSERT_TRUE(ok2.ok()); + } + + { + Status status = Status::NotFound("custom NotFound status message"); + Status status2 = std::move(status); + + ASSERT_TRUE(status2.IsNotFound()); + ASSERT_EQ("NotFound: custom NotFound status message", status2.ToString()); + } + + { + Status self_moved = Status::IOError("custom IOError status message"); + + // Needed to bypass compiler warning about explicit move-assignment. + Status& self_moved_reference = self_moved; + self_moved_reference = std::move(self_moved); + } +} + +} // namespace leveldb + +int main(int argc, char** argv) { + return leveldb::test::RunAllTests(); +}