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 <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 value_ = chars; | |
49 is_null_ = false; | |
yzshen1
2014/05/28 19:45:51
nit: shall we use is_null_ = !chars?
| |
50 return *this; | |
51 } | |
52 | |
53 void reset() { | |
54 value_.clear(); | |
55 is_null_ = true; | |
56 } | |
57 | |
58 bool is_null() const { return is_null_; } | |
59 | |
60 size_t size() const { return value_.size(); } | |
61 | |
62 const char* data() const { return value_.data(); } | |
63 | |
64 const char& at(size_t offset) const { return value_.at(offset); } | |
65 const char& operator[](size_t offset) const { return value_[offset]; } | |
66 | |
67 const std::string& get() const { return value_; } | |
68 operator const std::string&() const { return value_; } | |
69 | |
70 void Swap(String* other) { | |
71 std::swap(is_null_, other->is_null_); | |
72 value_.swap(other->value_); | |
73 } | |
74 | |
75 void Swap(std::string* other) { | |
76 is_null_ = false; | |
77 value_.swap(*other); | |
78 } | |
79 | |
80 private: | |
81 typedef std::string String::*Testable; | |
82 | |
83 public: | |
84 operator Testable() const { return is_null_ ? 0 : &String::value_; } | |
85 | |
86 private: | |
87 std::string value_; | |
88 bool is_null_; | |
89 }; | |
90 | |
91 inline bool operator==(const String& a, const String& b) { | |
92 return a.is_null() == b.is_null() && a.get() == b.get(); | |
93 } | |
94 inline bool operator==(const char* a, const String& b) { | |
95 return !b.is_null() && a == b.get(); | |
96 } | |
97 inline bool operator==(const String& a, const char* b) { | |
98 return !a.is_null() && a.get() == b; | |
99 } | |
100 inline bool operator!=(const String& a, const String& b) { return !(a == b); } | |
101 inline bool operator!=(const char* a, const String& b) { return !(a == b); } | |
102 inline bool operator!=(const String& a, const char* b) { return !(a == b); } | |
103 | |
104 // TODO(darin): Add similar variants of operator<,<=,>,>= | |
105 | |
106 template <> | |
107 class TypeConverter<String, std::string> { | |
108 public: | |
109 static String ConvertFrom(const std::string& input) { | |
110 return String(input); | |
111 } | |
112 static std::string ConvertTo(const String& input) { | |
113 return input; | |
114 } | |
115 }; | |
116 | |
117 template <size_t N> | |
118 class TypeConverter<String, char[N]> { | |
119 public: | |
120 static String ConvertFrom(const char input[N]) { | |
121 assert(input); | |
122 return String(input, N-1); | |
123 } | |
124 }; | |
125 | |
126 // Appease MSVC. | |
127 template <size_t N> | |
128 class TypeConverter<String, const char[N]> { | |
129 public: | |
130 static String ConvertFrom(const char input[N]) { | |
131 assert(input); | |
132 return String(input, N-1); | |
133 } | |
134 }; | |
135 | |
136 template <> | |
137 class TypeConverter<String, const char*> { | |
138 public: | |
139 // |input| may be null, in which case a null String will be returned. | |
140 static String ConvertFrom(const char* input) { | |
141 return String(input); | |
142 } | |
143 }; | |
144 | |
145 } // namespace mojo | |
146 | |
147 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ | |
OLD | NEW |