| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 14 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 15 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 15 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 16 #include "mojo/public/cpp/bindings/lib/template_util.h" | 16 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 17 #include "mojo/public/cpp/bindings/type_converter.h" | 17 #include "mojo/public/cpp/bindings/type_converter.h" |
| 18 | 18 |
| 19 namespace mojo { | 19 namespace mojo { |
| 20 | 20 |
| 21 // Represents a moveable array with contents of type |T|. The array can be null, | |
| 22 // meaning that no value has been assigned to it. Null is distinct from empty. | |
| 23 template <typename T> | 21 template <typename T> |
| 24 class Array { | 22 class Array { |
| 25 MOJO_MOVE_ONLY_TYPE(Array) | 23 MOJO_MOVE_ONLY_TYPE(Array) |
| 26 public: | 24 public: |
| 27 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits; | 25 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits; |
| 28 typedef typename Traits::ConstRefType ConstRefType; | 26 typedef typename Traits::ConstRefType ConstRefType; |
| 29 typedef typename Traits::RefType RefType; | 27 typedef typename Traits::RefType RefType; |
| 30 typedef typename Traits::StorageType StorageType; | 28 typedef typename Traits::StorageType StorageType; |
| 31 typedef typename Traits::ForwardType ForwardType; | 29 typedef typename Traits::ForwardType ForwardType; |
| 32 | 30 |
| 33 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> | 31 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> |
| 34 Data_; | 32 Data_; |
| 35 | 33 |
| 36 // Constructs a new array that is null. | |
| 37 Array() : is_null_(true) {} | 34 Array() : is_null_(true) {} |
| 38 | |
| 39 // Constructs a new non-null array of the specified size. The elements will | |
| 40 // be value-initialized (meaning that they will be initialized by their | |
| 41 // default constructor, if any, or else zero-initialized). | |
| 42 explicit Array(size_t size) : vec_(size), is_null_(false) { | 35 explicit Array(size_t size) : vec_(size), is_null_(false) { |
| 43 Traits::Initialize(&vec_); | 36 Traits::Initialize(&vec_); |
| 44 } | 37 } |
| 45 ~Array() { Traits::Finalize(&vec_); } | 38 ~Array() { Traits::Finalize(&vec_); } |
| 46 | 39 |
| 47 // Moves the contents of |other| into this array. | |
| 48 Array(Array&& other) : is_null_(true) { Take(&other); } | 40 Array(Array&& other) : is_null_(true) { Take(&other); } |
| 49 Array& operator=(Array&& other) { | 41 Array& operator=(Array&& other) { |
| 50 Take(&other); | 42 Take(&other); |
| 51 return *this; | 43 return *this; |
| 52 } | 44 } |
| 53 | 45 |
| 54 // Creates a non-null array of the specified size. The elements will be | |
| 55 // value-initialized (meaning that they will be initialized by their default | |
| 56 // constructor, if any, or else zero-initialized). | |
| 57 static Array New(size_t size) { return Array(size).Pass(); } | 46 static Array New(size_t size) { return Array(size).Pass(); } |
| 58 | 47 |
| 59 // Creates a new array with a copy of the contents of |other|. | |
| 60 template <typename U> | 48 template <typename U> |
| 61 static Array From(const U& other) { | 49 static Array From(const U& other) { |
| 62 return TypeConverter<Array, U>::Convert(other); | 50 return TypeConverter<Array, U>::Convert(other); |
| 63 } | 51 } |
| 64 | 52 |
| 65 // Copies the contents of this array to a new object of type |U|. | |
| 66 template <typename U> | 53 template <typename U> |
| 67 U To() const { | 54 U To() const { |
| 68 return TypeConverter<U, Array>::Convert(*this); | 55 return TypeConverter<U, Array>::Convert(*this); |
| 69 } | 56 } |
| 70 | 57 |
| 71 // Resets the contents of this array back to null. | |
| 72 void reset() { | 58 void reset() { |
| 73 if (!vec_.empty()) { | 59 if (!vec_.empty()) { |
| 74 Traits::Finalize(&vec_); | 60 Traits::Finalize(&vec_); |
| 75 vec_.clear(); | 61 vec_.clear(); |
| 76 } | 62 } |
| 77 is_null_ = true; | 63 is_null_ = true; |
| 78 } | 64 } |
| 79 | 65 |
| 80 // Indicates whether the array is null (which is distinct from empty). | |
| 81 bool is_null() const { return is_null_; } | 66 bool is_null() const { return is_null_; } |
| 82 | 67 |
| 83 // Returns a reference to the first element of the array. Calling this on a | |
| 84 // null or empty array causes undefined behavior. | |
| 85 ConstRefType front() const { return vec_.front(); } | 68 ConstRefType front() const { return vec_.front(); } |
| 86 RefType front() { return vec_.front(); } | 69 RefType front() { return vec_.front(); } |
| 87 | 70 |
| 88 // Returns the size of the array, which will be zero if the array is null. | |
| 89 size_t size() const { return vec_.size(); } | 71 size_t size() const { return vec_.size(); } |
| 90 | 72 |
| 91 // Returns a reference to the element at zero-based |offset|. Calling this on | |
| 92 // an array with size less than |offset|+1 causes undefined behavior. | |
| 93 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } | 73 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } |
| 94 ConstRefType operator[](size_t offset) const { return at(offset); } | 74 ConstRefType operator[](size_t offset) const { return at(offset); } |
| 75 |
| 95 RefType at(size_t offset) { return Traits::at(&vec_, offset); } | 76 RefType at(size_t offset) { return Traits::at(&vec_, offset); } |
| 96 RefType operator[](size_t offset) { return at(offset); } | 77 RefType operator[](size_t offset) { return at(offset); } |
| 97 | 78 |
| 98 // Pushes |value| onto the back of the array. If this array was null, it will | |
| 99 // become non-null with a size of 1. | |
| 100 void push_back(ForwardType value) { | 79 void push_back(ForwardType value) { |
| 101 is_null_ = false; | 80 is_null_ = false; |
| 102 Traits::PushBack(&vec_, value); | 81 Traits::PushBack(&vec_, value); |
| 103 } | 82 } |
| 104 | 83 |
| 105 // Resizes the array to |size| and makes it non-null. Otherwise, works just | |
| 106 // like the resize method of |std::vector|. | |
| 107 void resize(size_t size) { | 84 void resize(size_t size) { |
| 108 is_null_ = false; | 85 is_null_ = false; |
| 109 Traits::Resize(&vec_, size); | 86 Traits::Resize(&vec_, size); |
| 110 } | 87 } |
| 111 | 88 |
| 112 // Returns a const reference to the |std::vector| managed by this class. If | |
| 113 // the array is null, this will be an empty vector. | |
| 114 const std::vector<StorageType>& storage() const { return vec_; } | 89 const std::vector<StorageType>& storage() const { return vec_; } |
| 115 operator const std::vector<StorageType>&() const { return vec_; } | 90 operator const std::vector<StorageType>&() const { return vec_; } |
| 116 | 91 |
| 117 // Swaps the contents of this array with the |other| array, including | |
| 118 // nullness. | |
| 119 void Swap(Array* other) { | 92 void Swap(Array* other) { |
| 120 std::swap(is_null_, other->is_null_); | 93 std::swap(is_null_, other->is_null_); |
| 121 vec_.swap(other->vec_); | 94 vec_.swap(other->vec_); |
| 122 } | 95 } |
| 123 | |
| 124 // Swaps the contents of this array with the specified vector, making this | |
| 125 // array non-null. Since the vector cannot represent null, it will just be | |
| 126 // made empty if this array is null. | |
| 127 void Swap(std::vector<StorageType>* other) { | 96 void Swap(std::vector<StorageType>* other) { |
| 128 is_null_ = false; | 97 is_null_ = false; |
| 129 vec_.swap(*other); | 98 vec_.swap(*other); |
| 130 } | 99 } |
| 131 | 100 |
| 132 // Returns a copy of the array where each value of the new array has been | |
| 133 // "cloned" from the corresponding value of this array. If this array contains | |
| 134 // primitive data types, this is equivalent to simply copying the contents. | |
| 135 // However, if the array contains objects, then each new element is created by | |
| 136 // calling the |Clone| method of the source element, which should make a copy | |
| 137 // of the element. | |
| 138 // | |
| 139 // Please note that calling this method will fail compilation if the element | 101 // Please note that calling this method will fail compilation if the element |
| 140 // type cannot be cloned (which usually means that it is a Mojo handle type or | 102 // type cannot be cloned (which usually means that it is a Mojo handle type or |
| 141 // a type contains Mojo handles). | 103 // a type contains Mojo handles). |
| 142 Array Clone() const { | 104 Array Clone() const { |
| 143 Array result; | 105 Array result; |
| 144 result.is_null_ = is_null_; | 106 result.is_null_ = is_null_; |
| 145 Traits::Clone(vec_, &result.vec_); | 107 Traits::Clone(vec_, &result.vec_); |
| 146 return result.Pass(); | 108 return result.Pass(); |
| 147 } | 109 } |
| 148 | 110 |
| 149 // Indicates whether the contents of this array are equal to |other|. A null | |
| 150 // array is only equal to another null array. Elements are compared using the | |
| 151 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method | |
| 152 // of the element. | |
| 153 bool Equals(const Array& other) const { | 111 bool Equals(const Array& other) const { |
| 154 if (is_null() != other.is_null()) | 112 if (is_null() != other.is_null()) |
| 155 return false; | 113 return false; |
| 156 if (size() != other.size()) | 114 if (size() != other.size()) |
| 157 return false; | 115 return false; |
| 158 for (size_t i = 0; i < size(); ++i) { | 116 for (size_t i = 0; i < size(); ++i) { |
| 159 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) | 117 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) |
| 160 return false; | 118 return false; |
| 161 } | 119 } |
| 162 return true; | 120 return true; |
| 163 } | 121 } |
| 164 | 122 |
| 165 private: | 123 private: |
| 166 typedef std::vector<StorageType> Array::*Testable; | 124 typedef std::vector<StorageType> Array::*Testable; |
| 167 | 125 |
| 168 public: | 126 public: |
| 169 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } | 127 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } |
| 170 | 128 |
| 171 private: | 129 private: |
| 172 void Take(Array* other) { | 130 void Take(Array* other) { |
| 173 reset(); | 131 reset(); |
| 174 Swap(other); | 132 Swap(other); |
| 175 } | 133 } |
| 176 | 134 |
| 177 std::vector<StorageType> vec_; | 135 std::vector<StorageType> vec_; |
| 178 bool is_null_; | 136 bool is_null_; |
| 179 }; | 137 }; |
| 180 | 138 |
| 181 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | |
| 182 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each | |
| 183 // element. The returned array will always be non-null. | |
| 184 template <typename T, typename E> | 139 template <typename T, typename E> |
| 185 struct TypeConverter<Array<T>, std::vector<E>> { | 140 struct TypeConverter<Array<T>, std::vector<E>> { |
| 186 static Array<T> Convert(const std::vector<E>& input) { | 141 static Array<T> Convert(const std::vector<E>& input) { |
| 187 Array<T> result(input.size()); | 142 Array<T> result(input.size()); |
| 188 for (size_t i = 0; i < input.size(); ++i) | 143 for (size_t i = 0; i < input.size(); ++i) |
| 189 result[i] = TypeConverter<T, E>::Convert(input[i]); | 144 result[i] = TypeConverter<T, E>::Convert(input[i]); |
| 190 return result.Pass(); | 145 return result.Pass(); |
| 191 } | 146 } |
| 192 }; | 147 }; |
| 193 | 148 |
| 194 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of | |
| 195 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | |
| 196 // element. If the input array is null, the output vector will be empty. | |
| 197 template <typename E, typename T> | 149 template <typename E, typename T> |
| 198 struct TypeConverter<std::vector<E>, Array<T>> { | 150 struct TypeConverter<std::vector<E>, Array<T>> { |
| 199 static std::vector<E> Convert(const Array<T>& input) { | 151 static std::vector<E> Convert(const Array<T>& input) { |
| 200 std::vector<E> result; | 152 std::vector<E> result; |
| 201 if (!input.is_null()) { | 153 if (!input.is_null()) { |
| 202 result.resize(input.size()); | 154 result.resize(input.size()); |
| 203 for (size_t i = 0; i < input.size(); ++i) | 155 for (size_t i = 0; i < input.size(); ++i) |
| 204 result[i] = TypeConverter<E, T>::Convert(input[i]); | 156 result[i] = TypeConverter<E, T>::Convert(input[i]); |
| 205 } | 157 } |
| 206 return result; | 158 return result; |
| 207 } | 159 } |
| 208 }; | 160 }; |
| 209 | 161 |
| 210 } // namespace mojo | 162 } // namespace mojo |
| 211 | 163 |
| 212 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 164 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| OLD | NEW |