OLD | NEW |
---|---|
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 <string> | 8 #include <string> |
9 | 9 |
10 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 10 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 template <typename U> | 33 template <typename U> |
34 static String From(const U& other) { | 34 static String From(const U& other) { |
35 return TypeConverter<String, U>::Convert(other); | 35 return TypeConverter<String, U>::Convert(other); |
36 } | 36 } |
37 | 37 |
38 template <typename U> | 38 template <typename U> |
39 U To() const { | 39 U To() const { |
40 return TypeConverter<U, String>::Convert(*this); | 40 return TypeConverter<U, String>::Convert(*this); |
41 } | 41 } |
42 | 42 |
43 String& operator=(const mojo::String& str) { | |
yzshen1
2014/09/27 00:01:58
Do you also want to add a copy constructor?
Elliot Glaysher
2014/10/02 19:54:03
Done.
| |
44 value_ = str.value_; | |
45 is_null_ = str.is_null_; | |
46 return *this; | |
47 } | |
43 String& operator=(const std::string& str) { | 48 String& operator=(const std::string& str) { |
44 value_ = str; | 49 value_ = str; |
45 is_null_ = false; | 50 is_null_ = false; |
46 return *this; | 51 return *this; |
47 } | 52 } |
48 String& operator=(const char* chars) { | 53 String& operator=(const char* chars) { |
49 is_null_ = !chars; | 54 is_null_ = !chars; |
50 if (chars) { | 55 if (chars) { |
51 value_ = chars; | 56 value_ = chars; |
52 } else { | 57 } else { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 return !a.is_null() && a.get() == b; | 108 return !a.is_null() && a.get() == b; |
104 } | 109 } |
105 inline bool operator!=(const String& a, const String& b) { return !(a == b); } | 110 inline bool operator!=(const String& a, const String& b) { return !(a == b); } |
106 inline bool operator!=(const char* a, const String& b) { return !(a == b); } | 111 inline bool operator!=(const char* a, const String& b) { return !(a == b); } |
107 inline bool operator!=(const String& a, const char* b) { return !(a == b); } | 112 inline bool operator!=(const String& a, const char* b) { return !(a == b); } |
108 | 113 |
109 inline std::ostream& operator<<(std::ostream& out, const String& s) { | 114 inline std::ostream& operator<<(std::ostream& out, const String& s) { |
110 return out << s.get(); | 115 return out << s.get(); |
111 } | 116 } |
112 | 117 |
118 inline bool operator<(const String& a, const String& b) { | |
119 // TODO(erg): This needs a test. | |
120 if (a.is_null() && !b.is_null()) { | |
121 // The null string is less than a non-NULL string. | |
122 return true; | |
123 } else if (a.is_null() || b.is_null()) { | |
124 // in all other cases where A or B is NULL, |a| is not less than |b|. | |
125 return false; | |
126 } | |
127 | |
128 return a.get() < b.get(); | |
129 } | |
130 | |
113 // TODO(darin): Add similar variants of operator<,<=,>,>= | 131 // TODO(darin): Add similar variants of operator<,<=,>,>= |
114 | 132 |
115 template <> | 133 template <> |
116 struct TypeConverter<String, std::string> { | 134 struct TypeConverter<String, std::string> { |
117 static String Convert(const std::string& input) { return String(input); } | 135 static String Convert(const std::string& input) { return String(input); } |
118 }; | 136 }; |
119 | 137 |
120 template <> | 138 template <> |
121 struct TypeConverter<std::string, String> { | 139 struct TypeConverter<std::string, String> { |
122 static std::string Convert(const String& input) { return input; } | 140 static std::string Convert(const String& input) { return input; } |
(...skipping 18 matching lines...) Expand all Loading... | |
141 | 159 |
142 template <> | 160 template <> |
143 struct TypeConverter<String, const char*> { | 161 struct TypeConverter<String, const char*> { |
144 // |input| may be null, in which case a null String will be returned. | 162 // |input| may be null, in which case a null String will be returned. |
145 static String Convert(const char* input) { return String(input); } | 163 static String Convert(const char* input) { return String(input); } |
146 }; | 164 }; |
147 | 165 |
148 } // namespace mojo | 166 } // namespace mojo |
149 | 167 |
150 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ | 168 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ |
OLD | NEW |