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 <cstddef> | 11 #include <cstddef> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 16 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
18 #include "mojo/public/cpp/bindings/lib/template_util.h" | 18 #include "mojo/public/cpp/bindings/lib/template_util.h" |
19 #include "mojo/public/cpp/bindings/type_converter.h" | 19 #include "mojo/public/cpp/bindings/type_converter.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 | 22 |
23 // Represents a moveable array with contents of type |T|. The array can be null, | 23 // Represents a moveable array with contents of type |T|. The array can be null, |
24 // meaning that no value has been assigned to it. Null is distinct from empty. | 24 // meaning that no value has been assigned to it. Null is distinct from empty. |
25 template <typename T> | 25 template <typename T> |
26 class Array { | 26 class Array { |
27 public: | 27 public: |
28 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits; | 28 using ConstRefType = typename std::vector<T>::const_reference; |
29 typedef typename Traits::ConstRefType ConstRefType; | 29 using RefType = typename std::vector<T>::reference; |
30 typedef typename Traits::RefType RefType; | 30 |
31 typedef typename Traits::StorageType StorageType; | 31 using Traits = internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value>; |
32 typedef typename Traits::ForwardType ForwardType; | 32 using ForwardType = typename Traits::ForwardType; |
33 | 33 |
34 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> | 34 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> |
35 Data_; | 35 Data_; |
36 | 36 |
37 // Constructs a new array that is null. | 37 // Constructs a new array that is null. |
38 Array() : is_null_(true) {} | 38 Array() : is_null_(true) {} |
39 | 39 |
40 ~Array() { Traits::Finalize(&vec_); } | 40 ~Array() {} |
41 | 41 |
42 // Moves the contents of |other| into this array. | 42 // Moves the contents of |other| into this array. |
43 Array(Array&& other) : is_null_(true) { Take(&other); } | 43 Array(Array&& other) : is_null_(true) { Take(&other); } |
44 Array& operator=(Array&& other) { | 44 Array& operator=(Array&& other) { |
45 Take(&other); | 45 Take(&other); |
46 return *this; | 46 return *this; |
47 } | 47 } |
48 | 48 |
49 // Creates a non-null array of the specified size. The elements will be | 49 // Creates a non-null array of the specified size. The elements will be |
50 // value-initialized (meaning that they will be initialized by their default | 50 // value-initialized (meaning that they will be initialized by their default |
(...skipping 11 matching lines...) Expand all Loading... |
62 } | 62 } |
63 | 63 |
64 // Copies the contents of this array to a new object of type |U|. | 64 // Copies the contents of this array to a new object of type |U|. |
65 template <typename U> | 65 template <typename U> |
66 U To() const { | 66 U To() const { |
67 return TypeConverter<U, Array>::Convert(*this); | 67 return TypeConverter<U, Array>::Convert(*this); |
68 } | 68 } |
69 | 69 |
70 // Resets the contents of this array back to null. | 70 // Resets the contents of this array back to null. |
71 void reset() { | 71 void reset() { |
72 if (!vec_.empty()) { | 72 if (!vec_.empty()) |
73 Traits::Finalize(&vec_); | |
74 vec_.clear(); | 73 vec_.clear(); |
75 } | |
76 is_null_ = true; | 74 is_null_ = true; |
77 } | 75 } |
78 | 76 |
79 // Indicates whether the array is null (which is distinct from empty). | 77 // Indicates whether the array is null (which is distinct from empty). |
80 bool is_null() const { return is_null_; } | 78 bool is_null() const { return is_null_; } |
81 | 79 |
82 // Returns a reference to the first element of the array. Calling this on a | 80 // Returns a reference to the first element of the array. Calling this on a |
83 // null or empty array causes undefined behavior. | 81 // null or empty array causes undefined behavior. |
84 ConstRefType front() const { return vec_.front(); } | 82 ConstRefType front() const { return vec_.front(); } |
85 RefType front() { return vec_.front(); } | 83 RefType front() { return vec_.front(); } |
86 | 84 |
87 // Returns the size of the array, which will be zero if the array is null. | 85 // Returns the size of the array, which will be zero if the array is null. |
88 size_t size() const { return vec_.size(); } | 86 size_t size() const { return vec_.size(); } |
89 | 87 |
90 // Returns a reference to the element at zero-based |offset|. Calling this on | 88 // Returns a reference to the element at zero-based |offset|. Calling this on |
91 // an array with size less than |offset|+1 causes undefined behavior. | 89 // an array with size less than |offset|+1 causes undefined behavior. |
92 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } | 90 ConstRefType at(size_t offset) const { return vec_.at(offset); } |
93 ConstRefType operator[](size_t offset) const { return at(offset); } | 91 ConstRefType operator[](size_t offset) const { return at(offset); } |
94 RefType at(size_t offset) { return Traits::at(&vec_, offset); } | 92 RefType at(size_t offset) { return vec_.at(offset); } |
95 RefType operator[](size_t offset) { return at(offset); } | 93 RefType operator[](size_t offset) { return at(offset); } |
96 | 94 |
97 // Pushes |value| onto the back of the array. If this array was null, it will | 95 // Pushes |value| onto the back of the array. If this array was null, it will |
98 // become non-null with a size of 1. | 96 // become non-null with a size of 1. |
99 void push_back(ForwardType value) { | 97 void push_back(ForwardType value) { |
100 is_null_ = false; | 98 is_null_ = false; |
101 Traits::PushBack(&vec_, value); | 99 Traits::PushBack(&vec_, value); |
102 } | 100 } |
103 | 101 |
104 // Resizes the array to |size| and makes it non-null. Otherwise, works just | 102 // Resizes the array to |size| and makes it non-null. Otherwise, works just |
105 // like the resize method of |std::vector|. | 103 // like the resize method of |std::vector|. |
106 void resize(size_t size) { | 104 void resize(size_t size) { |
107 is_null_ = false; | 105 is_null_ = false; |
108 Traits::Resize(&vec_, size); | 106 vec_.resize(size); |
109 } | 107 } |
110 | 108 |
111 // Returns a const reference to the |std::vector| managed by this class. If | 109 // Returns a const reference to the |std::vector| managed by this class. If |
112 // the array is null, this will be an empty vector. | 110 // the array is null, this will be an empty vector. |
113 const std::vector<StorageType>& storage() const { return vec_; } | 111 const std::vector<T>& storage() const { return vec_; } |
114 operator const std::vector<StorageType>&() const { return vec_; } | 112 operator const std::vector<T>&() const { return vec_; } |
115 | 113 |
116 // Swaps the contents of this array with the |other| array, including | 114 // Swaps the contents of this array with the |other| array, including |
117 // nullness. | 115 // nullness. |
118 void Swap(Array* other) { | 116 void Swap(Array* other) { |
119 std::swap(is_null_, other->is_null_); | 117 std::swap(is_null_, other->is_null_); |
120 vec_.swap(other->vec_); | 118 vec_.swap(other->vec_); |
121 } | 119 } |
122 | 120 |
123 // Swaps the contents of this array with the specified vector, making this | 121 // Swaps the contents of this array with the specified vector, making this |
124 // array non-null. Since the vector cannot represent null, it will just be | 122 // array non-null. Since the vector cannot represent null, it will just be |
125 // made empty if this array is null. | 123 // made empty if this array is null. |
126 void Swap(std::vector<StorageType>* other) { | 124 void Swap(std::vector<T>* other) { |
127 is_null_ = false; | 125 is_null_ = false; |
128 vec_.swap(*other); | 126 vec_.swap(*other); |
129 } | 127 } |
130 | 128 |
131 // Returns a copy of the array where each value of the new array has been | 129 // Returns a copy of the array where each value of the new array has been |
132 // "cloned" from the corresponding value of this array. If this array contains | 130 // "cloned" from the corresponding value of this array. If this array contains |
133 // primitive data types, this is equivalent to simply copying the contents. | 131 // primitive data types, this is equivalent to simply copying the contents. |
134 // However, if the array contains objects, then each new element is created by | 132 // However, if the array contains objects, then each new element is created by |
135 // calling the |Clone| method of the source element, which should make a copy | 133 // calling the |Clone| method of the source element, which should make a copy |
136 // of the element. | 134 // of the element. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 223 |
226 private: | 224 private: |
227 Array<T>* arr_; | 225 Array<T>* arr_; |
228 size_t pos_; | 226 size_t pos_; |
229 }; | 227 }; |
230 | 228 |
231 Iterator begin() { return Iterator(this, 0); } | 229 Iterator begin() { return Iterator(this, 0); } |
232 Iterator end() { return Iterator(this, size()); } | 230 Iterator end() { return Iterator(this, size()); } |
233 | 231 |
234 private: | 232 private: |
235 typedef std::vector<StorageType> Array::*Testable; | 233 typedef std::vector<T> Array::*Testable; |
236 | 234 |
237 public: | 235 public: |
238 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } | 236 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } |
239 | 237 |
240 private: | 238 private: |
241 void Take(Array* other) { | 239 void Take(Array* other) { |
242 reset(); | 240 reset(); |
243 Swap(other); | 241 Swap(other); |
244 } | 242 } |
245 | 243 |
246 std::vector<StorageType> vec_; | 244 std::vector<T> vec_; |
247 bool is_null_; | 245 bool is_null_; |
248 | 246 |
249 MOJO_MOVE_ONLY_TYPE(Array); | 247 MOJO_MOVE_ONLY_TYPE(Array); |
250 }; | 248 }; |
251 | 249 |
252 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 250 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
253 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each | 251 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each |
254 // element. The returned array will always be non-null. | 252 // element. The returned array will always be non-null. |
255 template <typename T, typename E> | 253 template <typename T, typename E> |
256 struct TypeConverter<Array<T>, std::vector<E>> { | 254 struct TypeConverter<Array<T>, std::vector<E>> { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 for (size_t i = 0; i < input.size(); ++i) | 300 for (size_t i = 0; i < input.size(); ++i) |
303 result.insert(TypeConverter<E, T>::Convert(input[i])); | 301 result.insert(TypeConverter<E, T>::Convert(input[i])); |
304 } | 302 } |
305 return result; | 303 return result; |
306 } | 304 } |
307 }; | 305 }; |
308 | 306 |
309 } // namespace mojo | 307 } // namespace mojo |
310 | 308 |
311 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 309 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
OLD | NEW |