Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: third_party/mojo/src/mojo/public/cpp/bindings/array.h

Issue 1410053006: Move third_party/mojo/src/mojo/public to mojo/public (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 THIRD_PARTY_MOJO_SRC_MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
6 #define THIRD_PARTY_MOJO_SRC_MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
7
8 #include <string.h>
9
10 #include <algorithm>
11 #include <set>
12 #include <string>
13 #include <vector>
14
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/lib/array_internal.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/lib/bindings_internal.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/lib/template_util.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/lib/value_traits.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/type_converter.h"
20
21 namespace mojo {
22
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.
25 template <typename T>
26 class Array {
27 MOJO_MOVE_ONLY_TYPE(Array)
28 public:
29 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits;
30 typedef typename Traits::ConstRefType ConstRefType;
31 typedef typename Traits::RefType RefType;
32 typedef typename Traits::StorageType StorageType;
33 typedef typename Traits::ForwardType 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 // Constructs a new non-null array of the specified size. The elements will
42 // be value-initialized (meaning that they will be initialized by their
43 // default constructor, if any, or else zero-initialized).
44 explicit Array(size_t size) : vec_(size), is_null_(false) {
45 Traits::Initialize(&vec_);
46 }
47 ~Array() { Traits::Finalize(&vec_); }
48
49 // Moves the contents of |other| into this array.
50 Array(Array&& other) : is_null_(true) { Take(&other); }
51 Array& operator=(Array&& other) {
52 Take(&other);
53 return *this;
54 }
55
56 // Creates a non-null array of the specified size. The elements will be
57 // value-initialized (meaning that they will be initialized by their default
58 // constructor, if any, or else zero-initialized).
59 static Array New(size_t size) { return Array(size).Pass(); }
60
61 // Creates a new array with a copy of the contents of |other|.
62 template <typename U>
63 static Array From(const U& other) {
64 return TypeConverter<Array, U>::Convert(other);
65 }
66
67 // Copies the contents of this array to a new object of type |U|.
68 template <typename U>
69 U To() const {
70 return TypeConverter<U, Array>::Convert(*this);
71 }
72
73 // Resets the contents of this array back to null.
74 void reset() {
75 if (!vec_.empty()) {
76 Traits::Finalize(&vec_);
77 vec_.clear();
78 }
79 is_null_ = true;
80 }
81
82 // Indicates whether the array is null (which is distinct from empty).
83 bool is_null() const { return is_null_; }
84
85 // Returns a reference to the first element of the array. Calling this on a
86 // null or empty array causes undefined behavior.
87 ConstRefType front() const { return vec_.front(); }
88 RefType front() { return vec_.front(); }
89
90 // Returns the size of the array, which will be zero if the array is null.
91 size_t size() const { return vec_.size(); }
92
93 // Returns a reference to the element at zero-based |offset|. Calling this on
94 // an array with size less than |offset|+1 causes undefined behavior.
95 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); }
96 ConstRefType operator[](size_t offset) const { return at(offset); }
97 RefType at(size_t offset) { return Traits::at(&vec_, offset); }
98 RefType operator[](size_t offset) { return at(offset); }
99
100 // Pushes |value| onto the back of the array. If this array was null, it will
101 // become non-null with a size of 1.
102 void push_back(ForwardType value) {
103 is_null_ = false;
104 Traits::PushBack(&vec_, value);
105 }
106
107 // Resizes the array to |size| and makes it non-null. Otherwise, works just
108 // like the resize method of |std::vector|.
109 void resize(size_t size) {
110 is_null_ = false;
111 Traits::Resize(&vec_, size);
112 }
113
114 // Returns a const reference to the |std::vector| managed by this class. If
115 // the array is null, this will be an empty vector.
116 const std::vector<StorageType>& storage() const { return vec_; }
117 operator const std::vector<StorageType>&() const { return vec_; }
118
119 // Swaps the contents of this array with the |other| array, including
120 // nullness.
121 void Swap(Array* other) {
122 std::swap(is_null_, other->is_null_);
123 vec_.swap(other->vec_);
124 }
125
126 // Swaps the contents of this array with the specified vector, making this
127 // array non-null. Since the vector cannot represent null, it will just be
128 // made empty if this array is null.
129 void Swap(std::vector<StorageType>* other) {
130 is_null_ = false;
131 vec_.swap(*other);
132 }
133
134 // Returns a copy of the array where each value of the new array has been
135 // "cloned" from the corresponding value of this array. If this array contains
136 // primitive data types, this is equivalent to simply copying the contents.
137 // However, if the array contains objects, then each new element is created by
138 // calling the |Clone| method of the source element, which should make a copy
139 // of the element.
140 //
141 // Please note that calling this method will fail compilation if the element
142 // type cannot be cloned (which usually means that it is a Mojo handle type or
143 // a type contains Mojo handles).
144 Array Clone() const {
145 Array result;
146 result.is_null_ = is_null_;
147 Traits::Clone(vec_, &result.vec_);
148 return result.Pass();
149 }
150
151 // Indicates whether the contents of this array are equal to |other|. A null
152 // array is only equal to another null array. Elements are compared using the
153 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method
154 // of the element.
155 bool Equals(const Array& other) const {
156 if (is_null() != other.is_null())
157 return false;
158 if (size() != other.size())
159 return false;
160 for (size_t i = 0; i < size(); ++i) {
161 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i)))
162 return false;
163 }
164 return true;
165 }
166
167 private:
168 typedef std::vector<StorageType> Array::*Testable;
169
170 public:
171 operator Testable() const { return is_null_ ? 0 : &Array::vec_; }
172
173 private:
174 // Forbid the == and != operators explicitly, otherwise Array will be
175 // converted to Testable to do == or != comparison.
176 template <typename U>
177 bool operator==(const Array<U>& other) const = delete;
178 template <typename U>
179 bool operator!=(const Array<U>& other) const = delete;
180
181 void Take(Array* other) {
182 reset();
183 Swap(other);
184 }
185
186 std::vector<StorageType> vec_;
187 bool is_null_;
188 };
189
190 // A |TypeConverter| that will create an |Array<T>| containing a copy of the
191 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each
192 // element. The returned array will always be non-null.
193 template <typename T, typename E>
194 struct TypeConverter<Array<T>, std::vector<E>> {
195 static Array<T> Convert(const std::vector<E>& input) {
196 Array<T> result(input.size());
197 for (size_t i = 0; i < input.size(); ++i)
198 result[i] = TypeConverter<T, E>::Convert(input[i]);
199 return result.Pass();
200 }
201 };
202
203 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of
204 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each
205 // element. If the input array is null, the output vector will be empty.
206 template <typename E, typename T>
207 struct TypeConverter<std::vector<E>, Array<T>> {
208 static std::vector<E> Convert(const Array<T>& input) {
209 std::vector<E> result;
210 if (!input.is_null()) {
211 result.resize(input.size());
212 for (size_t i = 0; i < input.size(); ++i)
213 result[i] = TypeConverter<E, T>::Convert(input[i]);
214 }
215 return result;
216 }
217 };
218
219 // A |TypeConverter| that will create an |Array<T>| containing a copy of the
220 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each
221 // element. The returned array will always be non-null.
222 template <typename T, typename E>
223 struct TypeConverter<Array<T>, std::set<E>> {
224 static Array<T> Convert(const std::set<E>& input) {
225 Array<T> result(0u);
226 for (auto i : input)
227 result.push_back(TypeConverter<T, E>::Convert(i));
228 return result.Pass();
229 }
230 };
231
232 // A |TypeConverter| that will create an |std::set<E>| containing a copy of
233 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each
234 // element. If the input array is null, the output set will be empty.
235 template <typename E, typename T>
236 struct TypeConverter<std::set<E>, Array<T>> {
237 static std::set<E> Convert(const Array<T>& input) {
238 std::set<E> result;
239 if (!input.is_null()) {
240 for (size_t i = 0; i < input.size(); ++i)
241 result.insert(TypeConverter<E, T>::Convert(input[i]));
242 }
243 return result;
244 }
245 };
246
247 } // namespace mojo
248
249 #endif // THIRD_PARTY_MOJO_SRC_MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698