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

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: Fix gn build. 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
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), 27 : value_(chars, num_chars),
28 is_null_(false) { 28 is_null_(false) {
29 } 29 }
30 String(const mojo::String& str)
31 : value_(str.value_),
32 is_null_(str.is_null_) {
33 }
30 template <size_t N> 34 template <size_t N>
31 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {} 35 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {}
32 36
33 template <typename U> 37 template <typename U>
34 static String From(const U& other) { 38 static String From(const U& other) {
35 return TypeConverter<String, U>::Convert(other); 39 return TypeConverter<String, U>::Convert(other);
36 } 40 }
37 41
38 template <typename U> 42 template <typename U>
39 U To() const { 43 U To() const {
40 return TypeConverter<U, String>::Convert(*this); 44 return TypeConverter<U, String>::Convert(*this);
41 } 45 }
42 46
47 String& operator=(const mojo::String& str) {
48 value_ = str.value_;
49 is_null_ = str.is_null_;
50 return *this;
51 }
43 String& operator=(const std::string& str) { 52 String& operator=(const std::string& str) {
44 value_ = str; 53 value_ = str;
45 is_null_ = false; 54 is_null_ = false;
46 return *this; 55 return *this;
47 } 56 }
48 String& operator=(const char* chars) { 57 String& operator=(const char* chars) {
49 is_null_ = !chars; 58 is_null_ = !chars;
50 if (chars) { 59 if (chars) {
51 value_ = chars; 60 value_ = chars;
52 } else { 61 } else {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 return !a.is_null() && a.get() == b; 112 return !a.is_null() && a.get() == b;
104 } 113 }
105 inline bool operator!=(const String& a, const String& b) { return !(a == b); } 114 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); } 115 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); } 116 inline bool operator!=(const String& a, const char* b) { return !(a == b); }
108 117
109 inline std::ostream& operator<<(std::ostream& out, const String& s) { 118 inline std::ostream& operator<<(std::ostream& out, const String& s) {
110 return out << s.get(); 119 return out << s.get();
111 } 120 }
112 121
122 inline bool operator<(const String& a, const String& b) {
123 if (a.is_null() && !b.is_null()) {
124 // The null string is less than a non-NULL string.
125 return true;
126 } else if (a.is_null() || b.is_null()) {
127 // in all other cases where A or B is NULL, |a| is not less than |b|.
128 return false;
129 }
130
131 return a.get() < b.get();
132 }
133
113 // TODO(darin): Add similar variants of operator<,<=,>,>= 134 // TODO(darin): Add similar variants of operator<,<=,>,>=
114 135
115 template <> 136 template <>
116 struct TypeConverter<String, std::string> { 137 struct TypeConverter<String, std::string> {
117 static String Convert(const std::string& input) { return String(input); } 138 static String Convert(const std::string& input) { return String(input); }
118 }; 139 };
119 140
120 template <> 141 template <>
121 struct TypeConverter<std::string, String> { 142 struct TypeConverter<std::string, String> {
122 static std::string Convert(const String& input) { return input; } 143 static std::string Convert(const String& input) { return input; }
(...skipping 18 matching lines...) Expand all
141 162
142 template <> 163 template <>
143 struct TypeConverter<String, const char*> { 164 struct TypeConverter<String, const char*> {
144 // |input| may be null, in which case a null String will be returned. 165 // |input| may be null, in which case a null String will be returned.
145 static String Convert(const char* input) { return String(input); } 166 static String Convert(const char* input) { return String(input); }
146 }; 167 };
147 168
148 } // namespace mojo 169 } // namespace mojo
149 170
150 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ 171 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698