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

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

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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
7
8 #include <string>
9
10 #include "mojo/public/cpp/bindings/type_converter.h"
11 #include "mojo/public/cpp/system/macros.h"
12
13 namespace mojo {
14
15 class String {
16 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(String, RValue);
17 public:
18 String() : is_null_(true) {}
19
20 explicit String(const std::string& str) : value_(str), is_null_(false) {}
21 explicit String(const char* chars) : value_(chars), is_null_(false) {}
22 String(const char* chars, size_t num_chars)
23 : value_(chars, num_chars),
24 is_null_(false) {
25 }
26
27 template <size_t N>
28 explicit String(const char chars[N])
yzshen1 2014/05/22 17:13:53 I think this is probably not needed, we could rely
29 : value_(chars, N-1),
30 is_null_(false) {
31 }
32
33 template <typename U>
34 explicit String(const U& other) : is_null_(true) {
35 *this = TypeConverter<String, U>::ConvertFrom(other);
36 }
37
38 String(RValue other) : is_null_(true) { Take(other.object); }
39 String& operator=(RValue other) {
40 Take(other.object);
41 return *this;
42 }
43
44 template <typename U>
45 static String From(const U& other) {
46 return TypeConverter<String, U>::ConvertFrom(other);
47 }
48
49 template <typename U>
50 U To() const {
51 return TypeConverter<String, U>::ConvertTo(*this);
52 }
53
54 void reset() {
55 value_.clear();
56 is_null_ = true;
57 }
58
59 bool is_null() const { return is_null_; }
60
61 size_t size() const { return value_.size(); }
62
63 char at(size_t offset) const { return value_.at(offset); }
64 char operator[](size_t offset) const { return value_[offset]; }
65
66 const std::string& get() const { return value_; }
67 operator const std::string&() const { return value_; }
68
69 void Swap(String* other) {
70 std::swap(is_null_, other->is_null_);
71 value_.swap(other->value_);
72 }
73
74 void Swap(std::string* other) {
75 is_null_ = false;
76 value_.swap(*other);
77 }
78
79 private:
80 typedef std::string String::*Testable;
81
82 public:
83 operator Testable() const { return is_null_ ? 0 : &String::value_; }
84
85 private:
86 void Take(String* other) {
87 reset();
88 Swap(other);
89 }
90
91 std::string value_;
92 bool is_null_;
93 };
94
95 inline bool operator==(const String& a, const String& b) {
96 return a.is_null() == b.is_null() && a.get() == b.get();
97 }
98 inline bool operator==(const char* a, const String& b) {
99 return !b.is_null() && a == b.get();
100 }
101 inline bool operator==(const String& a, const char* b) {
102 return !a.is_null() && a.get() == b;
103 }
104 inline bool operator!=(const String& a, const String& b) { return !(a == b); }
105 inline bool operator!=(const char* a, const String& b) { return !(a == b); }
106 inline bool operator!=(const String& a, const char* b) { return !(a == b); }
107
108 // TODO(darin): Add similar variants of operator<,<=,>,>=
109
110 template <>
111 class TypeConverter<String, std::string> {
112 public:
113 static String ConvertFrom(const std::string& input) {
114 return String(input).Pass();
115 }
116 static std::string ConvertTo(const String& input) {
117 return input.get();
118 }
119 };
120
121 template <size_t N>
122 class TypeConverter<String, char[N]> {
123 public:
124 static String ConvertFrom(const char input[N]) {
125 return String(input, N-1).Pass();
126 }
127 };
128
129 // Appease MSVC.
130 template <size_t N>
131 class TypeConverter<String, const char[N]> {
132 public:
133 static String ConvertFrom(const char input[N]) {
134 return String(input, N-1).Pass();
135 }
136 };
137
138 template <>
139 class TypeConverter<String, const char*> {
140 public:
141 static String ConvertFrom(const char* input) {
142 return String(input).Pass();
143 }
144 };
145
146 } // namespace mojo
147
148 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698