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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/string_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 20 matching lines...) Expand all
31 } 31 }
32 String(const char* chars, size_t num_chars) 32 String(const char* chars, size_t num_chars)
33 : value_(chars, num_chars), is_null_(false) {} 33 : value_(chars, num_chars), is_null_(false) {}
34 String(const mojo::String& str) 34 String(const mojo::String& str)
35 : value_(str.value_), is_null_(str.is_null_) {} 35 : value_(str.value_), is_null_(str.is_null_) {}
36 36
37 template <size_t N> 37 template <size_t N>
38 String(const char chars[N]) 38 String(const char chars[N])
39 : value_(chars, N - 1), is_null_(false) {} 39 : value_(chars, N - 1), is_null_(false) {}
40 40
41 String(std::string&& other) : value_(std::move(other)), is_null_(false) {}
42 String(String&& other) : is_null_(true) { Swap(&other); }
43
41 template <typename U> 44 template <typename U>
42 static String From(const U& other) { 45 static String From(const U& other) {
43 return TypeConverter<String, U>::Convert(other); 46 return TypeConverter<String, U>::Convert(other);
44 } 47 }
45 48
46 template <typename U> 49 template <typename U>
47 U To() const { 50 U To() const {
48 return TypeConverter<U, String>::Convert(*this); 51 return TypeConverter<U, String>::Convert(*this);
49 } 52 }
50 53
(...skipping 10 matching lines...) Expand all
61 String& operator=(const char* chars) { 64 String& operator=(const char* chars) {
62 is_null_ = !chars; 65 is_null_ = !chars;
63 if (chars) { 66 if (chars) {
64 value_ = chars; 67 value_ = chars;
65 } else { 68 } else {
66 value_.clear(); 69 value_.clear();
67 } 70 }
68 return *this; 71 return *this;
69 } 72 }
70 73
74 String& operator=(std::string&& other) {
75 value_ = std::move(other);
76 is_null_ = false;
77 return *this;
78 }
79 String& operator=(String&& other) {
80 is_null_ = true;
81 value_.clear();
82 Swap(&other);
83 return *this;
84 }
85
71 bool is_null() const { return is_null_; } 86 bool is_null() const { return is_null_; }
72 87
73 size_t size() const { return value_.size(); } 88 size_t size() const { return value_.size(); }
74 89
75 const char* data() const { return value_.data(); } 90 const char* data() const { return value_.data(); }
76 91
77 const char& at(size_t offset) const { return value_.at(offset); } 92 const char& at(size_t offset) const { return value_.at(offset); }
78 const char& operator[](size_t offset) const { return value_[offset]; } 93 const char& operator[](size_t offset) const { return value_[offset]; }
79 94
80 const std::string& get() const { return value_; } 95 const std::string& get() const { return value_; }
81 operator const std::string&() const { return value_; } 96 operator const std::string&() const { return value_; }
82 97
98 // Returns a const reference to the |std::string| managed by this class. If
99 // the string is null, this will be an empty std::string.
100 const std::string& storage() const { return value_; }
101
102 // Passes the underlying storage and resets this string to null.
103 std::string PassStorage() {
104 is_null_ = true;
105 return std::move(value_);
106 }
107
83 void Swap(String* other) { 108 void Swap(String* other) {
84 std::swap(is_null_, other->is_null_); 109 std::swap(is_null_, other->is_null_);
85 value_.swap(other->value_); 110 value_.swap(other->value_);
86 } 111 }
87 112
88 void Swap(std::string* other) { 113 void Swap(std::string* other) {
89 is_null_ = false; 114 is_null_ = false;
90 value_.swap(*other); 115 value_.swap(*other);
91 } 116 }
92 117
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 189
165 template <> 190 template <>
166 struct TypeConverter<String, const char*> { 191 struct TypeConverter<String, const char*> {
167 // |input| may be null, in which case a null String will be returned. 192 // |input| may be null, in which case a null String will be returned.
168 static String Convert(const char* input) { return String(input); } 193 static String Convert(const char* input) { return String(input); }
169 }; 194 };
170 195
171 } // namespace mojo 196 } // namespace mojo
172 197
173 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ 198 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW
« 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