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

Side by Side Diff: mojo/public/cpp/bindings/string.h

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved test classes to their own shared file. Created 6 years, 2 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 | « mojo/public/cpp/bindings/map.h ('k') | mojo/public/cpp/bindings/tests/BUILD.gn » ('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 <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
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())
128 return !b.is_null();
129 if (b.is_null())
130 return false;
131
132 return a.get() < b.get();
133 }
134
118 // TODO(darin): Add similar variants of operator<,<=,>,>= 135 // TODO(darin): Add similar variants of operator<,<=,>,>=
119 136
120 template <> 137 template <>
121 struct TypeConverter<String, std::string> { 138 struct TypeConverter<String, std::string> {
122 static String Convert(const std::string& input) { return String(input); } 139 static String Convert(const std::string& input) { return String(input); }
123 }; 140 };
124 141
125 template <> 142 template <>
126 struct TypeConverter<std::string, String> { 143 struct TypeConverter<std::string, String> {
127 static std::string Convert(const String& input) { return input; } 144 static std::string Convert(const String& input) { return input; }
(...skipping 18 matching lines...) Expand all
146 163
147 template <> 164 template <>
148 struct TypeConverter<String, const char*> { 165 struct TypeConverter<String, const char*> {
149 // |input| may be null, in which case a null String will be returned. 166 // |input| may be null, in which case a null String will be returned.
150 static String Convert(const char* input) { return String(input); } 167 static String Convert(const char* input) { return String(input); }
151 }; 168 };
152 169
153 } // namespace mojo 170 } // namespace mojo
154 171
155 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ 172 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/map.h ('k') | mojo/public/cpp/bindings/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698