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

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

Issue 507173003: Change TypeConverter<X,Y>::ConvertFrom and ConvertTo into a single symmetric (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: compile for real Created 6 years, 3 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/array.h ('k') | mojo/public/cpp/bindings/struct_ptr.h » ('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"
(...skipping 14 matching lines...) Expand all
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 template <size_t N> 30 template <size_t N>
31 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {} 31 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {}
32 32
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>::ConvertFrom(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<String, U>::ConvertTo(*this); 40 return TypeConverter<U, String>::Convert(*this);
41 } 41 }
42 42
43 String& operator=(const std::string& str) { 43 String& operator=(const std::string& str) {
44 value_ = str; 44 value_ = str;
45 is_null_ = false; 45 is_null_ = false;
46 return *this; 46 return *this;
47 } 47 }
48 String& operator=(const char* chars) { 48 String& operator=(const char* chars) {
49 is_null_ = !chars; 49 is_null_ = !chars;
50 if (chars) { 50 if (chars) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 inline bool operator!=(const char* a, const String& b) { return !(a == b); } 106 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); } 107 inline bool operator!=(const String& a, const char* b) { return !(a == b); }
108 108
109 inline std::ostream& operator<<(std::ostream& out, const String& s) { 109 inline std::ostream& operator<<(std::ostream& out, const String& s) {
110 return out << s.get(); 110 return out << s.get();
111 } 111 }
112 112
113 // TODO(darin): Add similar variants of operator<,<=,>,>= 113 // TODO(darin): Add similar variants of operator<,<=,>,>=
114 114
115 template <> 115 template <>
116 class TypeConverter<String, std::string> { 116 struct TypeConverter<String, std::string> {
117 public: 117 static String Convert(const std::string& input) { return String(input); }
118 static String ConvertFrom(const std::string& input) { 118 };
119 return String(input); 119
120 } 120 template <>
121 static std::string ConvertTo(const String& input) { 121 struct TypeConverter<std::string, String> {
122 return input; 122 static std::string Convert(const String& input) { return input; }
123 }
124 }; 123 };
125 124
126 template <size_t N> 125 template <size_t N>
127 class TypeConverter<String, char[N]> { 126 struct TypeConverter<String, char[N]> {
128 public: 127 static String Convert(const char input[N]) {
129 static String ConvertFrom(const char input[N]) {
130 MOJO_DCHECK(input); 128 MOJO_DCHECK(input);
131 return String(input, N-1); 129 return String(input, N-1);
132 } 130 }
133 }; 131 };
134 132
135 // Appease MSVC. 133 // Appease MSVC.
136 template <size_t N> 134 template <size_t N>
137 class TypeConverter<String, const char[N]> { 135 struct TypeConverter<String, const char[N]> {
138 public: 136 static String Convert(const char input[N]) {
139 static String ConvertFrom(const char input[N]) {
140 MOJO_DCHECK(input); 137 MOJO_DCHECK(input);
141 return String(input, N-1); 138 return String(input, N-1);
142 } 139 }
143 }; 140 };
144 141
145 template <> 142 template <>
146 class TypeConverter<String, const char*> { 143 struct TypeConverter<String, const char*> {
147 public:
148 // |input| may be null, in which case a null String will be returned. 144 // |input| may be null, in which case a null String will be returned.
149 static String ConvertFrom(const char* input) { 145 static String Convert(const char* input) { return String(input); }
150 return String(input);
151 }
152 }; 146 };
153 147
154 } // namespace mojo 148 } // namespace mojo
155 149
156 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ 150 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/array.h ('k') | mojo/public/cpp/bindings/struct_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698