| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 internal::TypeTraits<T>::kIsObject, | 24 internal::TypeTraits<T>::kIsObject, |
| 25 internal::TypeTraits<T>::kIsHandle> Traits_; | 25 internal::TypeTraits<T>::kIsHandle> Traits_; |
| 26 typedef typename Traits_::DataType Data; | 26 typedef typename Traits_::DataType Data; |
| 27 typedef typename Traits_::ConstRef ConstRef; | 27 typedef typename Traits_::ConstRef ConstRef; |
| 28 | 28 |
| 29 Array() : data_(NULL) { | 29 Array() : data_(NULL) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 template <typename U> | 32 template <typename U> |
| 33 Array(const U& u, Buffer* buf = Buffer::current()) { | 33 Array(const U& u, Buffer* buf = Buffer::current()) { |
| 34 MOJO_INTERNAL_CHECK_ALLOW_DIRECT_TYPE_CONVERSION(Array<T>, U); |
| 34 *this = TypeConverter<Array<T>,U>::ConvertFrom(u, buf); | 35 *this = TypeConverter<Array<T>,U>::ConvertFrom(u, buf); |
| 35 } | 36 } |
| 36 | 37 |
| 37 template <typename U> | 38 template <typename U> |
| 38 Array& operator=(const U& u) { | 39 Array& operator=(const U& u) { |
| 40 MOJO_INTERNAL_CHECK_ALLOW_DIRECT_TYPE_CONVERSION(Array<T>, U); |
| 39 *this = TypeConverter<Array<T>,U>::ConvertFrom(u, Buffer::current()); | 41 *this = TypeConverter<Array<T>,U>::ConvertFrom(u, Buffer::current()); |
| 40 return *this; | 42 return *this; |
| 41 } | 43 } |
| 42 | 44 |
| 43 template <typename U> | 45 template <typename U> |
| 44 operator U() const { | 46 operator U() const { |
| 47 MOJO_INTERNAL_CHECK_ALLOW_DIRECT_TYPE_CONVERSION(Array<T>, U); |
| 45 return To<U>(); | 48 return To<U>(); |
| 46 } | 49 } |
| 47 | 50 |
| 48 template <typename U> | 51 template <typename U> |
| 49 U To() const { | 52 U To() const { |
| 50 return TypeConverter<Array<T>,U>::ConvertTo(*this); | 53 return TypeConverter<Array<T>,U>::ConvertTo(*this); |
| 51 } | 54 } |
| 52 | 55 |
| 56 template <typename U> |
| 57 static Array From(const U& u, Buffer* buf = Buffer::current()) { |
| 58 return TypeConverter<Array<T>,U>::ConvertFrom(u, buf); |
| 59 } |
| 60 |
| 53 bool is_null() const { return !data_; } | 61 bool is_null() const { return !data_; } |
| 54 | 62 |
| 55 size_t size() const { return data_->size(); } | 63 size_t size() const { return data_->size(); } |
| 56 | 64 |
| 57 ConstRef at(size_t offset) const { | 65 ConstRef at(size_t offset) const { |
| 58 return Traits_::ToConstRef(data_->at(offset)); | 66 return Traits_::ToConstRef(data_->at(offset)); |
| 59 } | 67 } |
| 60 ConstRef operator[](size_t offset) const { return at(offset); } | 68 ConstRef operator[](size_t offset) const { return at(offset); } |
| 61 | 69 |
| 62 // Provides a way to initialize an array element-by-element. | 70 // Provides a way to initialize an array element-by-element. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 }; | 106 }; |
| 99 | 107 |
| 100 // UTF-8 encoded | 108 // UTF-8 encoded |
| 101 typedef Array<char> String; | 109 typedef Array<char> String; |
| 102 | 110 |
| 103 template <> | 111 template <> |
| 104 class TypeConverter<String, std::string> { | 112 class TypeConverter<String, std::string> { |
| 105 public: | 113 public: |
| 106 static String ConvertFrom(const std::string& input, Buffer* buf); | 114 static String ConvertFrom(const std::string& input, Buffer* buf); |
| 107 static std::string ConvertTo(const String& input); | 115 static std::string ConvertTo(const String& input); |
| 116 |
| 117 MOJO_ALLOW_DIRECT_TYPE_CONVERSION(); |
| 108 }; | 118 }; |
| 109 | 119 |
| 110 template <size_t N> | 120 template <size_t N> |
| 111 class TypeConverter<String, char[N]> { | 121 class TypeConverter<String, char[N]> { |
| 112 public: | 122 public: |
| 113 static String ConvertFrom(const char input[N], Buffer* buf) { | 123 static String ConvertFrom(const char input[N], Buffer* buf) { |
| 114 String::Builder result(N - 1, buf); | 124 String::Builder result(N - 1, buf); |
| 115 memcpy(&result[0], input, N - 1); | 125 memcpy(&result[0], input, N - 1); |
| 116 return result.Finish(); | 126 return result.Finish(); |
| 117 } | 127 } |
| 128 |
| 129 MOJO_ALLOW_DIRECT_TYPE_CONVERSION(); |
| 118 }; | 130 }; |
| 119 | 131 |
| 120 // Appease MSVC. | 132 // Appease MSVC. |
| 121 template <size_t N> | 133 template <size_t N> |
| 122 class TypeConverter<String, const char[N]> { | 134 class TypeConverter<String, const char[N]> { |
| 123 public: | 135 public: |
| 124 static String ConvertFrom(const char input[N], Buffer* buf) { | 136 static String ConvertFrom(const char input[N], Buffer* buf) { |
| 125 return TypeConverter<String, char[N]>::ConvertFrom(input, buf); | 137 return TypeConverter<String, char[N]>::ConvertFrom(input, buf); |
| 126 } | 138 } |
| 139 |
| 140 MOJO_ALLOW_DIRECT_TYPE_CONVERSION(); |
| 127 }; | 141 }; |
| 128 | 142 |
| 129 template <> | 143 template <> |
| 130 class TypeConverter<String, const char*> { | 144 class TypeConverter<String, const char*> { |
| 131 public: | 145 public: |
| 132 static String ConvertFrom(const char* input, Buffer* buf); | 146 static String ConvertFrom(const char* input, Buffer* buf); |
| 133 // NOTE: |ConvertTo| explicitly not implemented since String is not null | 147 // NOTE: |ConvertTo| explicitly not implemented since String is not null |
| 134 // terminated (and may have embedded null bytes). | 148 // terminated (and may have embedded null bytes). |
| 149 |
| 150 MOJO_ALLOW_DIRECT_TYPE_CONVERSION(); |
| 135 }; | 151 }; |
| 136 | 152 |
| 137 template <typename T, typename E> | 153 template <typename T, typename E> |
| 138 class TypeConverter<Array<T>, std::vector<E> > { | 154 class GenericArrayTypeConverter { |
| 139 public: | 155 public: |
| 140 static Array<T> ConvertFrom(const std::vector<E>& input, Buffer* buf) { | 156 static Array<T> ConvertFrom(const std::vector<E>& input, Buffer* buf) { |
| 141 typename Array<T>::Builder result(input.size(), buf); | 157 typename Array<T>::Builder result(input.size(), buf); |
| 142 for (size_t i = 0; i < input.size(); ++i) | 158 for (size_t i = 0; i < input.size(); ++i) |
| 143 result[i] = TypeConverter<T, E>::ConvertFrom(input[i], buf); | 159 result[i] = TypeConverter<T, E>::ConvertFrom(input[i], buf); |
| 144 return result.Finish(); | 160 return result.Finish(); |
| 145 } | 161 } |
| 146 static std::vector<E> ConvertTo(const Array<T>& input) { | 162 static std::vector<E> ConvertTo(const Array<T>& input) { |
| 147 std::vector<E> result; | 163 std::vector<E> result; |
| 148 if (!input.is_null()) { | 164 if (!input.is_null()) { |
| 149 result.resize(input.size()); | 165 result.resize(input.size()); |
| 150 for (size_t i = 0; i < input.size(); ++i) | 166 for (size_t i = 0; i < input.size(); ++i) |
| 151 result[i] = TypeConverter<T, E>::ConvertTo(input[i]); | 167 result[i] = TypeConverter<T, E>::ConvertTo(input[i]); |
| 152 } | 168 } |
| 153 return result; | 169 return result; |
| 154 } | 170 } |
| 155 }; | 171 }; |
| 156 | 172 |
| 173 |
| 174 // Direct conversion is not enabled by default. |
| 175 // If you would like to enable that for certain array types, you need to do |
| 176 // specializations and specify MOJO_ALLOW_DIRECT_TYPE_CONVERSION() yourself. |
| 177 // |
| 178 // EXAMPLE: |
| 179 // |
| 180 // namespace mojo { |
| 181 // template <> |
| 182 // class TypeConverter<Array<X>, std::vector<Y> > |
| 183 // : public GenericArrayTypeConverter<X, Y> { |
| 184 // public: |
| 185 // MOJO_ALLOW_DIRECT_TYPE_CONVERSION(); |
| 186 // }; |
| 187 // } |
| 188 // |
| 189 // Please see the comments of MOJO_ALLOW_DIRECT_TYPE_CONVERSION() for more |
| 190 // details. |
| 191 template <typename T, typename E> |
| 192 class TypeConverter<Array<T>, std::vector<E> > |
| 193 : public GenericArrayTypeConverter<T, E> { |
| 194 }; |
| 195 |
| 157 } // namespace mojo | 196 } // namespace mojo |
| 158 | 197 |
| 159 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 198 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| OLD | NEW |