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

Side by Side Diff: mojo/common/common_type_converters.cc

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "mojo/common/common_type_converters.h" 5 #include "mojo/common/common_type_converters.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 10
11 namespace mojo { 11 namespace mojo {
12 12
13 // static 13 // static
14 String TypeConverter<String, base::StringPiece>::ConvertFrom( 14 String TypeConverter<String, base::StringPiece>::ConvertFrom(
15 const base::StringPiece& input, 15 const base::StringPiece& input) {
16 Buffer* buf) { 16 if (input.empty()) {
17 if (input.empty()) 17 char c = 0;
18 return String(); 18 return String(&c, 0).Pass();
19 String::Builder result(input.size(), buf); 19 }
20 memcpy(&result[0], input.data(), input.size()); 20 return String(input.data(), input.size()).Pass();
21 return result.Finish();
22 } 21 }
23 // static 22 // static
24 base::StringPiece TypeConverter<String, base::StringPiece>::ConvertTo( 23 base::StringPiece TypeConverter<String, base::StringPiece>::ConvertTo(
25 const String& input) { 24 const String& input) {
26 return input.is_null() ? base::StringPiece() : 25 return input.get();
27 base::StringPiece(&input[0], input.size());
28 } 26 }
29 27
30 // static 28 // static
31 String TypeConverter<String, base::string16>::ConvertFrom( 29 String TypeConverter<String, base::string16>::ConvertFrom(
32 const base::string16& input, 30 const base::string16& input) {
33 Buffer* buf) {
34 return TypeConverter<String, base::StringPiece>::ConvertFrom( 31 return TypeConverter<String, base::StringPiece>::ConvertFrom(
35 base::UTF16ToUTF8(input), buf); 32 base::UTF16ToUTF8(input));
36 } 33 }
37 // static 34 // static
38 base::string16 TypeConverter<String, base::string16>::ConvertTo( 35 base::string16 TypeConverter<String, base::string16>::ConvertTo(
39 const String& input) { 36 const String& input) {
40 return input.is_null() ? base::string16() : 37 return base::UTF8ToUTF16(TypeConverter<String, base::StringPiece>::ConvertTo(
41 base::UTF8ToUTF16(base::StringPiece(&input[0], input.size())); 38 input));
42 } 39 }
43 40
44 } // namespace mojo 41 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698