OLD | NEW |
| (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/lib/array_internal.h" | |
11 #include "mojo/public/cpp/bindings/type_converter.h" | |
12 #include "mojo/public/cpp/environment/logging.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 class String { | |
17 public: | |
18 typedef internal::String_Data Data_; | |
19 | |
20 String() : is_null_(true) {} | |
21 String(const std::string& str) : value_(str), is_null_(false) {} | |
22 String(const char* chars) : is_null_(!chars) { | |
23 if (chars) | |
24 value_ = chars; | |
25 } | |
26 String(const char* chars, size_t num_chars) | |
27 : value_(chars, num_chars), is_null_(false) {} | |
28 String(const mojo::String& str) | |
29 : value_(str.value_), is_null_(str.is_null_) {} | |
30 | |
31 template <size_t N> | |
32 String(const char chars[N]) | |
33 : value_(chars, N - 1), is_null_(false) {} | |
34 | |
35 template <typename U> | |
36 static String From(const U& other) { | |
37 return TypeConverter<String, U>::Convert(other); | |
38 } | |
39 | |
40 template <typename U> | |
41 U To() const { | |
42 return TypeConverter<U, String>::Convert(*this); | |
43 } | |
44 | |
45 String& operator=(const mojo::String& str) { | |
46 value_ = str.value_; | |
47 is_null_ = str.is_null_; | |
48 return *this; | |
49 } | |
50 String& operator=(const std::string& str) { | |
51 value_ = str; | |
52 is_null_ = false; | |
53 return *this; | |
54 } | |
55 String& operator=(const char* chars) { | |
56 is_null_ = !chars; | |
57 if (chars) { | |
58 value_ = chars; | |
59 } else { | |
60 value_.clear(); | |
61 } | |
62 return *this; | |
63 } | |
64 | |
65 void reset() { | |
66 value_.clear(); | |
67 is_null_ = true; | |
68 } | |
69 | |
70 bool is_null() const { return is_null_; } | |
71 | |
72 size_t size() const { return value_.size(); } | |
73 | |
74 const char* data() const { return value_.data(); } | |
75 | |
76 const char& at(size_t offset) const { return value_.at(offset); } | |
77 const char& operator[](size_t offset) const { return value_[offset]; } | |
78 | |
79 const std::string& get() const { return value_; } | |
80 operator const std::string&() const { return value_; } | |
81 | |
82 void Swap(String* other) { | |
83 std::swap(is_null_, other->is_null_); | |
84 value_.swap(other->value_); | |
85 } | |
86 | |
87 void Swap(std::string* other) { | |
88 is_null_ = false; | |
89 value_.swap(*other); | |
90 } | |
91 | |
92 private: | |
93 typedef std::string String::*Testable; | |
94 | |
95 public: | |
96 operator Testable() const { return is_null_ ? 0 : &String::value_; } | |
97 | |
98 private: | |
99 std::string value_; | |
100 bool is_null_; | |
101 }; | |
102 | |
103 inline bool operator==(const String& a, const String& b) { | |
104 return a.is_null() == b.is_null() && a.get() == b.get(); | |
105 } | |
106 inline bool operator==(const char* a, const String& b) { | |
107 return !b.is_null() && a == b.get(); | |
108 } | |
109 inline bool operator==(const String& a, const char* b) { | |
110 return !a.is_null() && a.get() == b; | |
111 } | |
112 inline bool operator!=(const String& a, const String& b) { | |
113 return !(a == b); | |
114 } | |
115 inline bool operator!=(const char* a, const String& b) { | |
116 return !(a == b); | |
117 } | |
118 inline bool operator!=(const String& a, const char* b) { | |
119 return !(a == b); | |
120 } | |
121 | |
122 inline std::ostream& operator<<(std::ostream& out, const String& s) { | |
123 return out << s.get(); | |
124 } | |
125 | |
126 inline bool operator<(const String& a, const String& b) { | |
127 if (a.is_null()) | |
128 return !b.is_null(); | |
129 if (b.is_null()) | |
130 return false; | |
131 | |
132 return a.get() < b.get(); | |
133 } | |
134 | |
135 // TODO(darin): Add similar variants of operator<,<=,>,>= | |
136 | |
137 template <> | |
138 struct TypeConverter<String, std::string> { | |
139 static String Convert(const std::string& input) { return String(input); } | |
140 }; | |
141 | |
142 template <> | |
143 struct TypeConverter<std::string, String> { | |
144 static std::string Convert(const String& input) { return input; } | |
145 }; | |
146 | |
147 template <size_t N> | |
148 struct TypeConverter<String, char[N]> { | |
149 static String Convert(const char input[N]) { | |
150 MOJO_DCHECK(input); | |
151 return String(input, N - 1); | |
152 } | |
153 }; | |
154 | |
155 // Appease MSVC. | |
156 template <size_t N> | |
157 struct TypeConverter<String, const char[N]> { | |
158 static String Convert(const char input[N]) { | |
159 MOJO_DCHECK(input); | |
160 return String(input, N - 1); | |
161 } | |
162 }; | |
163 | |
164 template <> | |
165 struct TypeConverter<String, const char*> { | |
166 // |input| may be null, in which case a null String will be returned. | |
167 static String Convert(const char* input) { return String(input); } | |
168 }; | |
169 | |
170 } // namespace mojo | |
171 | |
172 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ | |
OLD | NEW |