From 704a902182dd1ba3b315563a6a65a7eb107ece79 Mon Sep 17 00:00:00 2001 From: limasdf Date: Thu, 17 Dec 2015 17:36:16 -0800 Subject: Use rvalue reference instead of ExtensionBuilder::pass() C++ 11 enables rvalue reference with std::move() so that removing legacy ListBuilder::Pass(). BUG=563649 TBR=thankis@chromium.org Review URL: https://codereview.chromium.org/1511103003 Cr-Commit-Position: refs/heads/master@{#365969} --- extensions/common/extension_builder.cc | 18 ++++++++++++++++++ extensions/common/extension_builder.h | 10 ++++++---- extensions/common/test_util.cc | 17 +++++++++-------- extensions/common/test_util.h | 4 ++-- 4 files changed, 35 insertions(+), 14 deletions(-) (limited to 'extensions/common') diff --git a/extensions/common/extension_builder.cc b/extensions/common/extension_builder.cc index f993e94..b573128 100644 --- a/extensions/common/extension_builder.cc +++ b/extensions/common/extension_builder.cc @@ -4,6 +4,8 @@ #include "extensions/common/extension_builder.h" +#include + #include "extensions/common/extension.h" namespace extensions { @@ -14,6 +16,22 @@ ExtensionBuilder::ExtensionBuilder() } ExtensionBuilder::~ExtensionBuilder() {} +ExtensionBuilder::ExtensionBuilder(ExtensionBuilder&& other) + : path_(std::move(other.path_)), + location_(other.location_), + manifest_(std::move(other.manifest_)), + flags_(other.flags_), + id_(std::move(other.id_)) {} + +ExtensionBuilder& ExtensionBuilder::operator=(ExtensionBuilder&& other) { + path_ = std::move(other.path_); + location_ = other.location_; + manifest_ = std::move(other.manifest_); + flags_ = other.flags_; + id_ = std::move(other.id_); + return *this; +} + scoped_refptr ExtensionBuilder::Build() { std::string error; scoped_refptr extension = Extension::Create( diff --git a/extensions/common/extension_builder.h b/extensions/common/extension_builder.h index 4b40178..96c81d2 100644 --- a/extensions/common/extension_builder.h +++ b/extensions/common/extension_builder.h @@ -24,14 +24,14 @@ class ExtensionBuilder { ExtensionBuilder(); ~ExtensionBuilder(); + // Move constructor and operator=. + ExtensionBuilder(ExtensionBuilder&& other); + ExtensionBuilder& operator=(ExtensionBuilder&& other); + // Can only be called once, after which it's invalid to use the builder. // CHECKs that the extension was created successfully. scoped_refptr Build(); - // Workaround to allow you to pass rvalue ExtensionBuilders by reference to - // other functions, e.g. UseBuilder(ExtensionBuilder().Pass()) - ExtensionBuilder& Pass() { return *this; } - // Defaults to FilePath(). ExtensionBuilder& SetPath(const base::FilePath& path); @@ -58,6 +58,8 @@ class ExtensionBuilder { scoped_ptr manifest_; int flags_; std::string id_; + + DISALLOW_COPY_AND_ASSIGN(ExtensionBuilder); }; } // namespace extensions diff --git a/extensions/common/test_util.cc b/extensions/common/test_util.cc index 9a9312a..eb857fd 100644 --- a/extensions/common/test_util.cc +++ b/extensions/common/test_util.cc @@ -13,16 +13,16 @@ namespace extensions { namespace test_util { -ExtensionBuilder& BuildExtension(ExtensionBuilder& builder) { - return builder - .SetManifest(DictionaryBuilder() - .Set("name", "Test extension") - .Set("version", "1.0") - .Set("manifest_version", 2)); +ExtensionBuilder BuildExtension(ExtensionBuilder builder) { + builder.SetManifest(DictionaryBuilder() + .Set("name", "Test extension") + .Set("version", "1.0") + .Set("manifest_version", 2)); + return builder; } -ExtensionBuilder& BuildApp(ExtensionBuilder& builder) { - return builder.SetManifest( +ExtensionBuilder BuildApp(ExtensionBuilder builder) { + builder.SetManifest( DictionaryBuilder() .Set("name", "Test extension") .Set("version", "1.0") @@ -33,6 +33,7 @@ ExtensionBuilder& BuildApp(ExtensionBuilder& builder) { extensions::DictionaryBuilder().Set( "scripts", std::move(extensions::ListBuilder().Append( "background.js")))))); + return builder; } scoped_refptr CreateEmptyExtension() { diff --git a/extensions/common/test_util.h b/extensions/common/test_util.h index 0f02ac0..7220399 100644 --- a/extensions/common/test_util.h +++ b/extensions/common/test_util.h @@ -16,10 +16,10 @@ class ExtensionBuilder; namespace test_util { // Adds an extension manifest to a builder. -ExtensionBuilder& BuildExtension(ExtensionBuilder& builder); +ExtensionBuilder BuildExtension(ExtensionBuilder builder); // Adds an app manifest to a builder. -ExtensionBuilder& BuildApp(ExtensionBuilder& builder); +ExtensionBuilder BuildApp(ExtensionBuilder builder); // Creates an extension instance that can be attached to an ExtensionFunction // before running it. -- cgit v1.1