| 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/type_converter.h" |
| 11 #include "mojo/public/cpp/system/macros.h" |
| 12 |
| 13 namespace mojo { |
| 14 |
| 15 class String { |
| 16 public: |
| 17 String() : is_null_(true) {} |
| 18 String(const std::string& str) : value_(str), is_null_(false) {} |
| 19 String(const char* chars) : value_(chars), is_null_(false) {} |
| 20 String(const char* chars, size_t num_chars) |
| 21 : value_(chars, num_chars), |
| 22 is_null_(false) { |
| 23 } |
| 24 template <size_t N> |
| 25 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {} |
| 26 |
| 27 #if 0 |
| 28 template <typename U> |
| 29 explicit String(const U& other) : is_null_(true) { |
| 30 *this = TypeConverter<String, U>::ConvertFrom(other); |
| 31 } |
| 32 #endif |
| 33 |
| 34 template <typename U> |
| 35 static String From(const U& other) { |
| 36 return TypeConverter<String, U>::ConvertFrom(other); |
| 37 } |
| 38 |
| 39 template <typename U> |
| 40 U To() const { |
| 41 return TypeConverter<String, U>::ConvertTo(*this); |
| 42 } |
| 43 |
| 44 void reset() { |
| 45 value_.clear(); |
| 46 is_null_ = true; |
| 47 } |
| 48 |
| 49 bool is_null() const { return is_null_; } |
| 50 |
| 51 size_t size() const { return value_.size(); } |
| 52 |
| 53 const char* data() const { return value_.data(); } |
| 54 |
| 55 char at(size_t offset) const { return value_.at(offset); } |
| 56 char operator[](size_t offset) const { return value_[offset]; } |
| 57 |
| 58 const std::string& get() const { return value_; } |
| 59 operator const std::string&() const { return value_; } |
| 60 |
| 61 void Swap(String* other) { |
| 62 std::swap(is_null_, other->is_null_); |
| 63 value_.swap(other->value_); |
| 64 } |
| 65 |
| 66 void Swap(std::string* other) { |
| 67 is_null_ = false; |
| 68 value_.swap(*other); |
| 69 } |
| 70 |
| 71 private: |
| 72 typedef std::string String::*Testable; |
| 73 |
| 74 public: |
| 75 operator Testable() const { return is_null_ ? 0 : &String::value_; } |
| 76 |
| 77 private: |
| 78 std::string value_; |
| 79 bool is_null_; |
| 80 }; |
| 81 |
| 82 inline bool operator==(const String& a, const String& b) { |
| 83 return a.is_null() == b.is_null() && a.get() == b.get(); |
| 84 } |
| 85 inline bool operator==(const char* a, const String& b) { |
| 86 return !b.is_null() && a == b.get(); |
| 87 } |
| 88 inline bool operator==(const String& a, const char* b) { |
| 89 return !a.is_null() && a.get() == b; |
| 90 } |
| 91 inline bool operator!=(const String& a, const String& b) { return !(a == b); } |
| 92 inline bool operator!=(const char* a, const String& b) { return !(a == b); } |
| 93 inline bool operator!=(const String& a, const char* b) { return !(a == b); } |
| 94 |
| 95 // TODO(darin): Add similar variants of operator<,<=,>,>= |
| 96 |
| 97 template <> |
| 98 class TypeConverter<String, std::string> { |
| 99 public: |
| 100 static String ConvertFrom(const std::string& input) { |
| 101 return String(input); |
| 102 } |
| 103 static std::string ConvertTo(const String& input) { |
| 104 return input.get(); |
| 105 } |
| 106 }; |
| 107 |
| 108 template <size_t N> |
| 109 class TypeConverter<String, char[N]> { |
| 110 public: |
| 111 static String ConvertFrom(const char input[N]) { |
| 112 return String(input, N-1); |
| 113 } |
| 114 }; |
| 115 |
| 116 // Appease MSVC. |
| 117 template <size_t N> |
| 118 class TypeConverter<String, const char[N]> { |
| 119 public: |
| 120 static String ConvertFrom(const char input[N]) { |
| 121 return String(input, N-1); |
| 122 } |
| 123 }; |
| 124 |
| 125 template <> |
| 126 class TypeConverter<String, const char*> { |
| 127 public: |
| 128 static String ConvertFrom(const char* input) { |
| 129 return String(input); |
| 130 } |
| 131 }; |
| 132 |
| 133 } // namespace mojo |
| 134 |
| 135 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_ |
| OLD | NEW |