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

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: rebase Created 6 years, 6 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
« no previous file with comments | « mojo/public/cpp/bindings/passable.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
(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 <assert.h>
9
10 #include <string>
11
12 #include "mojo/public/cpp/bindings/type_converter.h"
13 #include "mojo/public/cpp/system/macros.h"
14
15 namespace mojo {
16
17 class String {
18 public:
19 String() : is_null_(true) {}
20 String(const std::string& str) : value_(str), is_null_(false) {}
21 String(const char* chars) : is_null_(!chars) {
22 if (chars)
23 value_ = chars;
24 }
25 String(const char* chars, size_t num_chars)
26 : value_(chars, num_chars),
27 is_null_(false) {
28 }
29 template <size_t N>
30 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {}
31
32 template <typename U>
33 static String From(const U& other) {
34 return TypeConverter<String, U>::ConvertFrom(other);
35 }
36
37 template <typename U>
38 U To() const {
39 return TypeConverter<String, U>::ConvertTo(*this);
40 }
41
42 String& operator=(const std::string& str) {
43 value_ = str;
44 is_null_ = false;
45 return *this;
46 }
47 String& operator=(const char* chars) {
48 is_null_ = !chars;
49 if (chars) {
50 value_ = chars;
51 } else {
52 value_.clear();
53 }
54 return *this;
55 }
56
57 void reset() {
58 value_.clear();
59 is_null_ = true;
60 }
61
62 bool is_null() const { return is_null_; }
63
64 size_t size() const { return value_.size(); }
65
66 const char* data() const { return value_.data(); }
67
68 const char& at(size_t offset) const { return value_.at(offset); }
69 const char& operator[](size_t offset) const { return value_[offset]; }
70
71 const std::string& get() const { return value_; }
72 operator const std::string&() const { return value_; }
73
74 void Swap(String* other) {
75 std::swap(is_null_, other->is_null_);
76 value_.swap(other->value_);
77 }
78
79 void Swap(std::string* other) {
80 is_null_ = false;
81 value_.swap(*other);
82 }
83
84 private:
85 typedef std::string String::*Testable;
86
87 public:
88 operator Testable() const { return is_null_ ? 0 : &String::value_; }
89
90 private:
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);
115 }
116 static std::string ConvertTo(const String& input) {
117 return input;
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 assert(input);
126 return String(input, N-1);
127 }
128 };
129
130 // Appease MSVC.
131 template <size_t N>
132 class TypeConverter<String, const char[N]> {
133 public:
134 static String ConvertFrom(const char input[N]) {
135 assert(input);
136 return String(input, N-1);
137 }
138 };
139
140 template <>
141 class TypeConverter<String, const char*> {
142 public:
143 // |input| may be null, in which case a null String will be returned.
144 static String ConvertFrom(const char* input) {
145 return String(input);
146 }
147 };
148
149 } // namespace mojo
150
151 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/passable.h ('k') | mojo/public/cpp/bindings/struct_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698