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

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: fix windows bustage 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 <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 {
yzshen1 2014/05/27 22:16:59 I think you mentioned that you would like to add o
18 public:
19 String() : is_null_(true) {}
20 String(const std::string& str) : value_(str), is_null_(false) {}
21 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 template <size_t N>
27 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {}
28
29 template <typename U>
30 static String From(const U& other) {
31 return TypeConverter<String, U>::ConvertFrom(other);
32 }
33
34 template <typename U>
35 U To() const {
36 return TypeConverter<String, U>::ConvertTo(*this);
37 }
38
39 void reset() {
40 value_.clear();
41 is_null_ = true;
42 }
43
44 bool is_null() const { return is_null_; }
45
46 size_t size() const { return value_.size(); }
47
48 const char* data() const { return value_.data(); }
49
50 const char& at(size_t offset) const { return value_.at(offset); }
51 const char& operator[](size_t offset) const { return value_[offset]; }
52
53 const std::string& get() const { return value_; }
54 operator const std::string&() const { return value_; }
55
56 void Swap(String* other) {
57 std::swap(is_null_, other->is_null_);
58 value_.swap(other->value_);
59 }
60
61 void Swap(std::string* other) {
62 is_null_ = false;
63 value_.swap(*other);
64 }
65
66 private:
67 typedef std::string String::*Testable;
68
69 public:
70 operator Testable() const { return is_null_ ? 0 : &String::value_; }
71
72 private:
73 std::string value_;
74 bool is_null_;
75 };
76
77 inline bool operator==(const String& a, const String& b) {
78 return a.is_null() == b.is_null() && a.get() == b.get();
79 }
80 inline bool operator==(const char* a, const String& b) {
81 return !b.is_null() && a == b.get();
82 }
83 inline bool operator==(const String& a, const char* b) {
84 return !a.is_null() && a.get() == b;
85 }
86 inline bool operator!=(const String& a, const String& b) { return !(a == b); }
87 inline bool operator!=(const char* a, const String& b) { return !(a == b); }
88 inline bool operator!=(const String& a, const char* b) { return !(a == b); }
89
90 // TODO(darin): Add similar variants of operator<,<=,>,>=
91
92 template <>
93 class TypeConverter<String, std::string> {
94 public:
95 static String ConvertFrom(const std::string& input) {
96 return String(input);
97 }
98 static std::string ConvertTo(const String& input) {
99 return input;
100 }
101 };
102
103 template <size_t N>
104 class TypeConverter<String, char[N]> {
105 public:
106 static String ConvertFrom(const char input[N]) {
107 assert(input);
108 return String(input, N-1);
109 }
110 };
111
112 // Appease MSVC.
113 template <size_t N>
114 class TypeConverter<String, const char[N]> {
115 public:
116 static String ConvertFrom(const char input[N]) {
117 assert(input);
118 return String(input, N-1);
119 }
120 };
121
122 template <>
123 class TypeConverter<String, const char*> {
124 public:
125 static String ConvertFrom(const char* input) {
126 return input ? String(input) : String();
yzshen1 2014/05/27 22:16:59 Do we want the null-handling behavior of String(co
127 }
128 };
129
130 } // namespace mojo
131
132 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698