Index: mojo/public/cpp/bindings/string.h |
diff --git a/mojo/public/cpp/bindings/string.h b/mojo/public/cpp/bindings/string.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e3359b2730697bfe222b698b0aac17d2b00b6f2e |
--- /dev/null |
+++ b/mojo/public/cpp/bindings/string.h |
@@ -0,0 +1,148 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ |
+#define MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ |
+ |
+#include <string> |
+ |
+#include "mojo/public/cpp/bindings/type_converter.h" |
+#include "mojo/public/cpp/system/macros.h" |
+ |
+namespace mojo { |
+ |
+class String { |
+ MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(String, RValue); |
+ public: |
+ String() : is_null_(true) {} |
+ |
+ explicit String(const std::string& str) : value_(str), is_null_(false) {} |
+ explicit String(const char* chars) : value_(chars), is_null_(false) {} |
+ String(const char* chars, size_t num_chars) |
+ : value_(chars, num_chars), |
+ is_null_(false) { |
+ } |
+ |
+ template <size_t N> |
+ explicit String(const char chars[N]) |
yzshen1
2014/05/22 17:13:53
I think this is probably not needed, we could rely
|
+ : value_(chars, N-1), |
+ is_null_(false) { |
+ } |
+ |
+ template <typename U> |
+ explicit String(const U& other) : is_null_(true) { |
+ *this = TypeConverter<String, U>::ConvertFrom(other); |
+ } |
+ |
+ String(RValue other) : is_null_(true) { Take(other.object); } |
+ String& operator=(RValue other) { |
+ Take(other.object); |
+ return *this; |
+ } |
+ |
+ template <typename U> |
+ static String From(const U& other) { |
+ return TypeConverter<String, U>::ConvertFrom(other); |
+ } |
+ |
+ template <typename U> |
+ U To() const { |
+ return TypeConverter<String, U>::ConvertTo(*this); |
+ } |
+ |
+ void reset() { |
+ value_.clear(); |
+ is_null_ = true; |
+ } |
+ |
+ bool is_null() const { return is_null_; } |
+ |
+ size_t size() const { return value_.size(); } |
+ |
+ char at(size_t offset) const { return value_.at(offset); } |
+ char operator[](size_t offset) const { return value_[offset]; } |
+ |
+ const std::string& get() const { return value_; } |
+ operator const std::string&() const { return value_; } |
+ |
+ void Swap(String* other) { |
+ std::swap(is_null_, other->is_null_); |
+ value_.swap(other->value_); |
+ } |
+ |
+ void Swap(std::string* other) { |
+ is_null_ = false; |
+ value_.swap(*other); |
+ } |
+ |
+ private: |
+ typedef std::string String::*Testable; |
+ |
+ public: |
+ operator Testable() const { return is_null_ ? 0 : &String::value_; } |
+ |
+ private: |
+ void Take(String* other) { |
+ reset(); |
+ Swap(other); |
+ } |
+ |
+ std::string value_; |
+ bool is_null_; |
+}; |
+ |
+inline bool operator==(const String& a, const String& b) { |
+ return a.is_null() == b.is_null() && a.get() == b.get(); |
+} |
+inline bool operator==(const char* a, const String& b) { |
+ return !b.is_null() && a == b.get(); |
+} |
+inline bool operator==(const String& a, const char* b) { |
+ return !a.is_null() && a.get() == b; |
+} |
+inline bool operator!=(const String& a, const String& b) { return !(a == b); } |
+inline bool operator!=(const char* a, const String& b) { return !(a == b); } |
+inline bool operator!=(const String& a, const char* b) { return !(a == b); } |
+ |
+// TODO(darin): Add similar variants of operator<,<=,>,>= |
+ |
+template <> |
+class TypeConverter<String, std::string> { |
+ public: |
+ static String ConvertFrom(const std::string& input) { |
+ return String(input).Pass(); |
+ } |
+ static std::string ConvertTo(const String& input) { |
+ return input.get(); |
+ } |
+}; |
+ |
+template <size_t N> |
+class TypeConverter<String, char[N]> { |
+ public: |
+ static String ConvertFrom(const char input[N]) { |
+ return String(input, N-1).Pass(); |
+ } |
+}; |
+ |
+// Appease MSVC. |
+template <size_t N> |
+class TypeConverter<String, const char[N]> { |
+ public: |
+ static String ConvertFrom(const char input[N]) { |
+ return String(input, N-1).Pass(); |
+ } |
+}; |
+ |
+template <> |
+class TypeConverter<String, const char*> { |
+ public: |
+ static String ConvertFrom(const char* input) { |
+ return String(input).Pass(); |
+ } |
+}; |
+ |
+} // namespace mojo |
+ |
+#endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ |