Make SkString movable.
This adds a move constructor and move assignment to SkString. This
allows elision of atomic increments and decrements on the fRec.
TBR=reed
Already agreed that moving is good.
Review URL: https://codereview.chromium.org/1672123002
diff --git a/include/core/SkString.h b/include/core/SkString.h
index 93514f2..4625dd1 100644
--- a/include/core/SkString.h
+++ b/include/core/SkString.h
@@ -126,6 +126,7 @@
explicit SkString(const char text[]);
SkString(const char text[], size_t len);
SkString(const SkString&);
+ SkString(SkString&&);
~SkString();
bool isEmpty() const { return 0 == fRec->fLength; }
@@ -172,6 +173,7 @@
// these methods edit the string
SkString& operator=(const SkString&);
+ SkString& operator=(SkString&&);
SkString& operator=(const char text[]);
char* writable_str();
diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp
index b5655e0..8ac5674 100644
--- a/src/core/SkString.cpp
+++ b/src/core/SkString.cpp
@@ -275,6 +275,13 @@
fRec = RefRec(src.fRec);
}
+SkString::SkString(SkString&& src) {
+ src.validate();
+
+ fRec = src.fRec;
+ src.fRec = const_cast<Rec*>(&gEmptyRec);
+}
+
SkString::~SkString() {
this->validate();
@@ -310,6 +317,15 @@
return *this;
}
+SkString& SkString::operator=(SkString&& src) {
+ this->validate();
+
+ if (fRec != src.fRec) {
+ this->swap(src);
+ }
+ return *this;
+}
+
SkString& SkString::operator=(const char text[]) {
this->validate();