Skip to content

Commit

Permalink
Add move constructors and assignment operators to GURL
Browse files Browse the repository at this point in the history
This CL implements a cheap move constructor and assignment operator for
GURL, so that code that moves around GURLs a lot (e.g. an
std::vector<GURL>) doesn't need to perform so many copies.

BUG=698978

Review-Url: https://codereview.chromium.org/2737693003
Cr-Commit-Position: refs/heads/master@{#456828}
  • Loading branch information
scott-little authored and Commit bot committed Mar 14, 2017
1 parent 84583c0 commit 376085b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 32 additions & 2 deletions url/gurl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ GURL::GURL(const GURL& other)
DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_);
}

GURL::GURL(GURL&& other)
: spec_(std::move(other.spec_)),
is_valid_(other.is_valid_),
parsed_(other.parsed_),
inner_url_(std::move(other.inner_url_)) {
other.is_valid_ = false;
other.parsed_ = url::Parsed();
}

GURL::GURL(base::StringPiece url_string) {
InitCanonical(url_string, true);
}
Expand Down Expand Up @@ -168,8 +177,29 @@ void GURL::InitializeFromCanonicalSpec() {
GURL::~GURL() {
}

GURL& GURL::operator=(GURL other) {
Swap(&other);
GURL& GURL::operator=(const GURL& other) {
spec_ = other.spec_;
is_valid_ = other.is_valid_;
parsed_ = other.parsed_;

if (!other.inner_url_)
inner_url_.reset();
else if (inner_url_)
*inner_url_ = *other.inner_url_;
else
inner_url_.reset(new GURL(*other.inner_url_));

return *this;
}

GURL& GURL::operator=(GURL&& other) {
spec_ = std::move(other.spec_);
is_valid_ = other.is_valid_;
parsed_ = other.parsed_;
inner_url_ = std::move(other.inner_url_);

other.is_valid_ = false;
other.parsed_ = url::Parsed();
return *this;
}

Expand Down
4 changes: 3 additions & 1 deletion url/gurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class URL_EXPORT GURL {
// Copy construction is relatively inexpensive, with most of the time going
// to reallocating the string. It does not re-parse.
GURL(const GURL& other);
GURL(GURL&& other);

// The strings to this contructor should be UTF-8 / UTF-16.
explicit GURL(base::StringPiece url_string);
Expand All @@ -76,7 +77,8 @@ class URL_EXPORT GURL {

~GURL();

GURL& operator=(GURL other);
GURL& operator=(const GURL& other);
GURL& operator=(GURL&& other);

// Returns true when this object represents a valid parsed URL. When not
// valid, other functions will still succeed, but you will not get canonical
Expand Down

0 comments on commit 376085b

Please sign in to comment.