OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <string.h> | |
10 | |
11 #include <algorithm> | |
12 #include <cstddef> | |
13 #include <set> | |
14 #include <string> | |
15 #include <vector> | |
16 | |
17 #include "mojo/public/cpp/bindings/lib/array_internal.h" | |
18 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | |
19 #include "mojo/public/cpp/bindings/lib/template_util.h" | |
20 #include "mojo/public/cpp/bindings/type_converter.h" | |
21 | |
22 namespace mojo { | |
23 | |
24 // Represents a moveable array with contents of type |T|. The array can be null, | |
25 // meaning that no value has been assigned to it. Null is distinct from empty. | |
26 template <typename T> | |
27 class Array { | |
28 public: | |
29 using ConstRefType = typename std::vector<T>::const_reference; | |
30 using RefType = typename std::vector<T>::reference; | |
31 | |
32 using Traits = internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value>; | |
33 using ForwardType = typename Traits::ForwardType; | |
34 | |
35 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> | |
36 Data_; | |
37 | |
38 // Constructs a new array that is null. | |
39 Array() : is_null_(true) {} | |
40 | |
41 // Makes null arrays implicitly constructible from |nullptr|. | |
42 Array(std::nullptr_t) : is_null_(true) {} | |
43 | |
44 ~Array() {} | |
45 | |
46 // Moves the contents of |other| into this array. | |
47 Array(Array&& other) : is_null_(true) { Take(&other); } | |
48 Array& operator=(Array&& other) { | |
49 Take(&other); | |
50 return *this; | |
51 } | |
52 | |
53 // Creates a non-null array of the specified size. The elements will be | |
54 // value-initialized (meaning that they will be initialized by their default | |
55 // constructor, if any, or else zero-initialized). | |
56 static Array New(size_t size) { | |
57 Array ret; | |
58 ret.resize(size); | |
59 return ret; | |
60 } | |
61 | |
62 // Creates a new array with a copy of the contents of |other|. | |
63 template <typename U> | |
64 static Array From(const U& other) { | |
65 return TypeConverter<Array, U>::Convert(other); | |
66 } | |
67 | |
68 // Copies the contents of this array to a new object of type |U|. | |
69 template <typename U> | |
70 U To() const { | |
71 return TypeConverter<U, Array>::Convert(*this); | |
72 } | |
73 | |
74 // Resets the contents of this array back to null. | |
75 void reset() { | |
76 vec_.clear(); | |
77 is_null_ = true; | |
78 } | |
79 | |
80 // Tests as true if non-null, false if null. | |
81 explicit operator bool() const { return !is_null_; } | |
82 | |
83 // Indicates whether the array is null (which is distinct from empty). | |
84 bool is_null() const { return is_null_; } | |
85 | |
86 // Returns a reference to the first element of the array. Calling this on a | |
87 // null or empty array causes undefined behavior. | |
88 ConstRefType front() const { return vec_.front(); } | |
89 RefType front() { return vec_.front(); } | |
90 | |
91 // Returns the size of the array, which will be zero if the array is null. | |
92 size_t size() const { return vec_.size(); } | |
93 | |
94 // For non-null arrays of non-bool types, returns a pointer to the first | |
95 // element, if any. (If the array is empty, the semantics are the same as for | |
96 // |std::vector<T>::data()|. The behavior is undefined if the array is null.) | |
97 const T* data() const { return vec_.data(); } | |
98 T* data() { return vec_.data(); } | |
99 | |
100 // Returns a reference to the element at zero-based |offset|. Calling this on | |
101 // an array with size less than |offset|+1 causes undefined behavior. | |
102 ConstRefType at(size_t offset) const { return vec_.at(offset); } | |
103 ConstRefType operator[](size_t offset) const { return at(offset); } | |
104 RefType at(size_t offset) { return vec_.at(offset); } | |
105 RefType operator[](size_t offset) { return at(offset); } | |
106 | |
107 // Pushes |value| onto the back of the array. If this array was null, it will | |
108 // become non-null with a size of 1. | |
109 void push_back(ForwardType value) { | |
110 is_null_ = false; | |
111 Traits::PushBack(&vec_, value); | |
112 } | |
113 | |
114 // Resizes the array to |size| and makes it non-null. Otherwise, works just | |
115 // like the resize method of |std::vector|. | |
116 void resize(size_t size) { | |
117 is_null_ = false; | |
118 vec_.resize(size); | |
119 } | |
120 | |
121 // Returns a const reference to the |std::vector| managed by this class. If | |
122 // the array is null, this will be an empty vector. | |
123 const std::vector<T>& storage() const { return vec_; } | |
124 operator const std::vector<T>&() const { return vec_; } | |
125 | |
126 // Swaps the contents of this array with the |other| array, including | |
127 // nullness. | |
128 void Swap(Array* other) { | |
129 std::swap(is_null_, other->is_null_); | |
130 vec_.swap(other->vec_); | |
131 } | |
132 | |
133 // Swaps the contents of this array with the specified vector, making this | |
134 // array non-null. Since the vector cannot represent null, it will just be | |
135 // made empty if this array is null. | |
136 void Swap(std::vector<T>* other) { | |
137 is_null_ = false; | |
138 vec_.swap(*other); | |
139 } | |
140 | |
141 // Returns a copy of the array where each value of the new array has been | |
142 // "cloned" from the corresponding value of this array. If this array contains | |
143 // primitive data types, this is equivalent to simply copying the contents. | |
144 // However, if the array contains objects, then each new element is created by | |
145 // calling the |Clone| method of the source element, which should make a copy | |
146 // of the element. | |
147 // | |
148 // Please note that calling this method will fail compilation if the element | |
149 // type cannot be cloned (which usually means that it is a Mojo handle type or | |
150 // a type contains Mojo handles). | |
151 Array Clone() const { | |
152 Array result; | |
153 result.is_null_ = is_null_; | |
154 Traits::Clone(vec_, &result.vec_); | |
155 return result; | |
156 } | |
157 | |
158 // Indicates whether the contents of this array are equal to |other|. A null | |
159 // array is only equal to another null array. Elements are compared using the | |
160 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method | |
161 // of the element. | |
162 bool Equals(const Array& other) const { | |
163 if (is_null() != other.is_null()) | |
164 return false; | |
165 if (size() != other.size()) | |
166 return false; | |
167 for (size_t i = 0; i < size(); ++i) { | |
168 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) | |
169 return false; | |
170 } | |
171 return true; | |
172 } | |
173 | |
174 public: | |
175 // Array<>::Iterator satisfies the RandomAccessIterator concept: | |
176 // http://en.cppreference.com/w/cpp/concept/RandomAccessIterator. | |
177 class Iterator { | |
178 public: | |
179 using difference_type = std::ptrdiff_t; | |
180 | |
181 // The following satisfy BidirectionalIterator: | |
182 Iterator() : arr_(nullptr), pos_(0u) {} | |
183 Iterator(Array<T>* arr, size_t pos) : arr_(arr), pos_(pos) {} | |
184 Iterator& operator++() { | |
185 ++pos_; | |
186 return *this; | |
187 } | |
188 Iterator operator++(int) { | |
189 Iterator original = *this; | |
190 ++pos_; | |
191 return original; | |
192 } | |
193 Iterator& operator--() { | |
194 --pos_; | |
195 return *this; | |
196 } | |
197 Iterator operator--(int) { | |
198 Iterator original = *this; | |
199 --pos_; | |
200 return original; | |
201 } | |
202 bool operator==(const Iterator& o) const { | |
203 return o.arr_ == arr_ && o.pos_ == pos_; | |
204 } | |
205 bool operator!=(const Iterator& o) const { return !(*this == o); } | |
206 RefType operator*() const { return arr_->at(pos_); } | |
207 T* operator->() const { return &arr_->at(pos_); } | |
208 | |
209 // The following satisfy RandomAccessIterator: | |
210 Iterator& operator+=(difference_type dist) { | |
211 pos_ += dist; | |
212 return *this; | |
213 } | |
214 Iterator& operator-=(difference_type dist) { | |
215 pos_ -= dist; | |
216 return *this; | |
217 } | |
218 friend Iterator operator+(difference_type dist, const Iterator& o_it) { | |
219 return Iterator(o_it.arr_, dist + o_it.pos_); | |
220 } | |
221 Iterator operator+(difference_type dist) const { | |
222 return Iterator(arr_, pos_ + dist); | |
223 } | |
224 Iterator operator-(difference_type dist) const { | |
225 return Iterator(arr_, pos_ - dist); | |
226 } | |
227 difference_type operator-(const Iterator& o_it) const { | |
228 return pos_ - o_it.pos_; | |
229 } | |
230 bool operator<(const Iterator& o_it) const { return pos_ < o_it.pos_; } | |
231 bool operator>(const Iterator& o_it) const { return pos_ > o_it.pos_; } | |
232 bool operator<=(const Iterator& o_it) const { return pos_ <= o_it.pos_; } | |
233 bool operator>=(const Iterator& o_it) const { return pos_ >= o_it.pos_; } | |
234 RefType operator[](difference_type dist) { return arr_->at(pos_ + dist); } | |
235 | |
236 private: | |
237 Array<T>* arr_; | |
238 size_t pos_; | |
239 }; | |
240 | |
241 Iterator begin() { return Iterator(this, 0); } | |
242 Iterator end() { return Iterator(this, size()); } | |
243 | |
244 private: | |
245 void Take(Array* other) { | |
246 reset(); | |
247 Swap(other); | |
248 } | |
249 | |
250 std::vector<T> vec_; | |
251 bool is_null_; | |
252 | |
253 MOJO_MOVE_ONLY_TYPE(Array); | |
254 }; | |
255 | |
256 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | |
257 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each | |
258 // element. The returned array will always be non-null. | |
259 template <typename T, typename E> | |
260 struct TypeConverter<Array<T>, std::vector<E>> { | |
261 static Array<T> Convert(const std::vector<E>& input) { | |
262 auto result = Array<T>::New(input.size()); | |
263 for (size_t i = 0; i < input.size(); ++i) | |
264 result[i] = TypeConverter<T, E>::Convert(input[i]); | |
265 return result; | |
266 } | |
267 }; | |
268 | |
269 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of | |
270 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | |
271 // element. If the input array is null, the output vector will be empty. | |
272 template <typename E, typename T> | |
273 struct TypeConverter<std::vector<E>, Array<T>> { | |
274 static std::vector<E> Convert(const Array<T>& input) { | |
275 std::vector<E> result; | |
276 if (!input.is_null()) { | |
277 result.resize(input.size()); | |
278 for (size_t i = 0; i < input.size(); ++i) | |
279 result[i] = TypeConverter<E, T>::Convert(input[i]); | |
280 } | |
281 return result; | |
282 } | |
283 }; | |
284 | |
285 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | |
286 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each | |
287 // element. The returned array will always be non-null. | |
288 template <typename T, typename E> | |
289 struct TypeConverter<Array<T>, std::set<E>> { | |
290 static Array<T> Convert(const std::set<E>& input) { | |
291 Array<T> result = Array<T>::New(0u); | |
292 for (auto i : input) | |
293 result.push_back(TypeConverter<T, E>::Convert(i)); | |
294 return result; | |
295 } | |
296 }; | |
297 | |
298 // A |TypeConverter| that will create an |std::set<E>| containing a copy of | |
299 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | |
300 // element. If the input array is null, the output set will be empty. | |
301 template <typename E, typename T> | |
302 struct TypeConverter<std::set<E>, Array<T>> { | |
303 static std::set<E> Convert(const Array<T>& input) { | |
304 std::set<E> result; | |
305 if (!input.is_null()) { | |
306 for (size_t i = 0; i < input.size(); ++i) | |
307 result.insert(TypeConverter<E, T>::Convert(input[i])); | |
308 } | |
309 return result; | |
310 } | |
311 }; | |
312 | |
313 } // namespace mojo | |
314 | |
315 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | |
OLD | NEW |