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" |
11 #include "mojo/public/cpp/bindings/type_converter.h" | 11 #include "mojo/public/cpp/bindings/type_converter.h" |
12 #include "mojo/public/cpp/environment/logging.h" | 12 #include "mojo/public/cpp/environment/logging.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 | 15 |
16 class String { | 16 class String { |
17 public: | 17 public: |
18 typedef internal::String_Data Data_; | 18 typedef internal::String_Data Data_; |
19 | 19 |
20 String() : is_null_(true) {} | 20 String() : is_null_(true) {} |
21 String(const std::string& str) : value_(str), is_null_(false) {} | 21 String(const std::string& str) : value_(str), is_null_(false) {} |
22 String(const char* chars) : is_null_(!chars) { | 22 String(const char* chars) : is_null_(!chars) { |
23 if (chars) | 23 if (chars) |
24 value_ = chars; | 24 value_ = chars; |
25 } | 25 } |
26 String(const char* chars, size_t num_chars) | 26 String(const char* chars, size_t num_chars) |
27 : value_(chars, num_chars), is_null_(false) {} | 27 : value_(chars, num_chars), is_null_(false) {} |
28 String(const mojo::String& str) | |
29 : value_(str.value_), is_null_(str.is_null_) {} | |
30 | |
28 template <size_t N> | 31 template <size_t N> |
29 String(const char chars[N]) | 32 String(const char chars[N]) |
30 : value_(chars, N - 1), is_null_(false) {} | 33 : value_(chars, N - 1), is_null_(false) {} |
31 | 34 |
32 template <typename U> | 35 template <typename U> |
33 static String From(const U& other) { | 36 static String From(const U& other) { |
34 return TypeConverter<String, U>::Convert(other); | 37 return TypeConverter<String, U>::Convert(other); |
35 } | 38 } |
36 | 39 |
37 template <typename U> | 40 template <typename U> |
38 U To() const { | 41 U To() const { |
39 return TypeConverter<U, String>::Convert(*this); | 42 return TypeConverter<U, String>::Convert(*this); |
40 } | 43 } |
41 | 44 |
45 String& operator=(const mojo::String& str) { | |
46 value_ = str.value_; | |
47 is_null_ = str.is_null_; | |
48 return *this; | |
49 } | |
42 String& operator=(const std::string& str) { | 50 String& operator=(const std::string& str) { |
43 value_ = str; | 51 value_ = str; |
44 is_null_ = false; | 52 is_null_ = false; |
45 return *this; | 53 return *this; |
46 } | 54 } |
47 String& operator=(const char* chars) { | 55 String& operator=(const char* chars) { |
48 is_null_ = !chars; | 56 is_null_ = !chars; |
49 if (chars) { | 57 if (chars) { |
50 value_ = chars; | 58 value_ = chars; |
51 } else { | 59 } else { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 return !(a == b); | 116 return !(a == b); |
109 } | 117 } |
110 inline bool operator!=(const String& a, const char* b) { | 118 inline bool operator!=(const String& a, const char* b) { |
111 return !(a == b); | 119 return !(a == b); |
112 } | 120 } |
113 | 121 |
114 inline std::ostream& operator<<(std::ostream& out, const String& s) { | 122 inline std::ostream& operator<<(std::ostream& out, const String& s) { |
115 return out << s.get(); | 123 return out << s.get(); |
116 } | 124 } |
117 | 125 |
126 inline bool operator<(const String& a, const String& b) { | |
127 if (a.is_null() && !b.is_null()) { | |
128 // The null string is less than a non-NULL string. | |
viettrungluu
2014/10/09 22:22:35
nit: lowercase "null" please
| |
129 return true; | |
130 } else if (a.is_null() || b.is_null()) { | |
viettrungluu
2014/10/09 22:22:35
nit: no "else"
| |
131 // in all other cases where A or B is NULL, |a| is not less than |b|. | |
viettrungluu
2014/10/09 22:22:35
nit: Capitalize "In" and lowercase "null".
I migh
| |
132 return false; | |
133 } | |
134 | |
135 return a.get() < b.get(); | |
136 } | |
137 | |
118 // TODO(darin): Add similar variants of operator<,<=,>,>= | 138 // TODO(darin): Add similar variants of operator<,<=,>,>= |
119 | 139 |
120 template <> | 140 template <> |
121 struct TypeConverter<String, std::string> { | 141 struct TypeConverter<String, std::string> { |
122 static String Convert(const std::string& input) { return String(input); } | 142 static String Convert(const std::string& input) { return String(input); } |
123 }; | 143 }; |
124 | 144 |
125 template <> | 145 template <> |
126 struct TypeConverter<std::string, String> { | 146 struct TypeConverter<std::string, String> { |
127 static std::string Convert(const String& input) { return input; } | 147 static std::string Convert(const String& input) { return input; } |
(...skipping 18 matching lines...) Expand all Loading... | |
146 | 166 |
147 template <> | 167 template <> |
148 struct TypeConverter<String, const char*> { | 168 struct TypeConverter<String, const char*> { |
149 // |input| may be null, in which case a null String will be returned. | 169 // |input| may be null, in which case a null String will be returned. |
150 static String Convert(const char* input) { return String(input); } | 170 static String Convert(const char* input) { return String(input); } |
151 }; | 171 }; |
152 | 172 |
153 } // namespace mojo | 173 } // namespace mojo |
154 | 174 |
155 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ | 175 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ |
OLD | NEW |