Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Unified Diff: mojo/public/cpp/bindings/string.h

Issue 2006823002: Mojo C++ bindings: move support for mojo::String. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/string_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/string.h
diff --git a/mojo/public/cpp/bindings/string.h b/mojo/public/cpp/bindings/string.h
index e02f089a7cd98144d1396b7421b593b898818de9..5f22b4cec60e50a3dea525fbd2770a51a93cbd6b 100644
--- a/mojo/public/cpp/bindings/string.h
+++ b/mojo/public/cpp/bindings/string.h
@@ -38,6 +38,9 @@ class String {
String(const char chars[N])
: value_(chars, N - 1), is_null_(false) {}
+ String(std::string&& other) : value_(std::move(other)), is_null_(false) {}
+ String(String&& other) : is_null_(true) { Swap(&other); }
+
template <typename U>
static String From(const U& other) {
return TypeConverter<String, U>::Convert(other);
@@ -68,6 +71,18 @@ class String {
return *this;
}
+ String& operator=(std::string&& other) {
+ value_ = std::move(other);
+ is_null_ = false;
+ return *this;
+ }
+ String& operator=(String&& other) {
+ is_null_ = true;
+ value_.clear();
+ Swap(&other);
+ return *this;
+ }
+
bool is_null() const { return is_null_; }
size_t size() const { return value_.size(); }
@@ -80,6 +95,16 @@ class String {
const std::string& get() const { return value_; }
operator const std::string&() const { return value_; }
+ // Returns a const reference to the |std::string| managed by this class. If
+ // the string is null, this will be an empty std::string.
+ const std::string& storage() const { return value_; }
+
+ // Passes the underlying storage and resets this string to null.
+ std::string PassStorage() {
+ is_null_ = true;
+ return std::move(value_);
+ }
+
void Swap(String* other) {
std::swap(is_null_, other->is_null_);
value_.swap(other->value_);
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/string_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698