Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/optional.h" | |
| 17 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 18 #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/bindings_internal.h" |
| 19 #include "mojo/public/cpp/bindings/lib/clone_equals_util.h" | 20 #include "mojo/public/cpp/bindings/lib/clone_equals_util.h" |
| 20 #include "mojo/public/cpp/bindings/lib/hash_util.h" | 21 #include "mojo/public/cpp/bindings/lib/hash_util.h" |
| 21 #include "mojo/public/cpp/bindings/lib/template_util.h" | 22 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 22 #include "mojo/public/cpp/bindings/type_converter.h" | 23 #include "mojo/public/cpp/bindings/type_converter.h" |
| 23 | 24 |
| 24 namespace mojo { | 25 namespace mojo { |
| 25 | 26 |
| 26 // Represents a moveable array with contents of type |T|. The array can be null, | 27 // Represents a moveable array with contents of type |T|. The array can be null, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 42 Array(std::nullptr_t null_pointer) : is_null_(true) {} | 43 Array(std::nullptr_t null_pointer) : is_null_(true) {} |
| 43 | 44 |
| 44 // Constructs a new non-null array of the specified size. The elements will | 45 // Constructs a new non-null array of the specified size. The elements will |
| 45 // be value-initialized (meaning that they will be initialized by their | 46 // be value-initialized (meaning that they will be initialized by their |
| 46 // default constructor, if any, or else zero-initialized). | 47 // default constructor, if any, or else zero-initialized). |
| 47 explicit Array(size_t size) : vec_(size), is_null_(false) {} | 48 explicit Array(size_t size) : vec_(size), is_null_(false) {} |
| 48 ~Array() {} | 49 ~Array() {} |
| 49 | 50 |
| 50 // Copies the contents of |other| into this array. | 51 // Copies the contents of |other| into this array. |
| 51 Array(const std::vector<T>& other) : vec_(other), is_null_(false) {} | 52 Array(const std::vector<T>& other) : vec_(other), is_null_(false) {} |
| 53 Array(const base::Optional<std::vector<T>>& other) | |
|
sky
2016/11/18 00:41:55
Is it typical not to use explicit for this and 58?
yzshen1
2016/11/18 00:48:09
Non-explicit makes the transition easier. Because
| |
| 54 : vec_(other.value_or(std::vector<T>())), is_null_(!other) {} | |
| 52 | 55 |
| 53 // Moves the contents of |other| into this array. | 56 // Moves the contents of |other| into this array. |
| 54 Array(std::vector<T>&& other) : vec_(std::move(other)), is_null_(false) {} | 57 Array(std::vector<T>&& other) : vec_(std::move(other)), is_null_(false) {} |
| 58 Array(base::Optional<std::vector<T>>&& other) : is_null_(!other) { | |
| 59 if (!is_null_) | |
| 60 vec_ = std::move(other.value()); | |
| 61 } | |
| 55 Array(Array&& other) : is_null_(true) { Take(&other); } | 62 Array(Array&& other) : is_null_(true) { Take(&other); } |
| 56 | 63 |
| 57 Array& operator=(std::vector<T>&& other) { | 64 Array& operator=(std::vector<T>&& other) { |
| 58 vec_ = std::move(other); | 65 vec_ = std::move(other); |
| 59 is_null_ = false; | 66 is_null_ = false; |
| 60 return *this; | 67 return *this; |
| 61 } | 68 } |
| 69 Array& operator=(base::Optional<std::vector<T>>&& other) { | |
| 70 is_null_ = !other; | |
| 71 vec_ = std::move(other).value_or(std::vector<T>()); | |
| 72 return *this; | |
| 73 } | |
| 62 Array& operator=(Array&& other) { | 74 Array& operator=(Array&& other) { |
| 63 Take(&other); | 75 Take(&other); |
| 64 return *this; | 76 return *this; |
| 65 } | 77 } |
| 66 | 78 |
| 67 Array& operator=(std::nullptr_t null_pointer) { | 79 Array& operator=(std::nullptr_t null_pointer) { |
| 68 is_null_ = true; | 80 is_null_ = true; |
| 69 vec_.clear(); | 81 vec_.clear(); |
| 70 return *this; | 82 return *this; |
| 71 } | 83 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 // Returns a const reference to the |std::vector| managed by this class. If | 154 // Returns a const reference to the |std::vector| managed by this class. If |
| 143 // the array is null, this will be an empty vector. | 155 // the array is null, this will be an empty vector. |
| 144 const std::vector<T>& storage() const { return vec_; } | 156 const std::vector<T>& storage() const { return vec_; } |
| 145 | 157 |
| 146 // Passes the underlying storage and resets this array to null. | 158 // Passes the underlying storage and resets this array to null. |
| 147 std::vector<T> PassStorage() { | 159 std::vector<T> PassStorage() { |
| 148 is_null_ = true; | 160 is_null_ = true; |
| 149 return std::move(vec_); | 161 return std::move(vec_); |
| 150 } | 162 } |
| 151 | 163 |
| 164 base::Optional<std::vector<T>> PassStorageAsOptional() { | |
| 165 base::Optional<std::vector<T>> result; | |
| 166 if (!is_null_) | |
| 167 result.emplace(std::move(vec_)); | |
| 168 is_null_ = true; | |
| 169 return result; | |
| 170 } | |
| 171 | |
| 152 operator const std::vector<T>&() const { return vec_; } | 172 operator const std::vector<T>&() const { return vec_; } |
| 153 | 173 |
| 154 void Swap(Array* other) { | 174 void Swap(Array* other) { |
| 155 std::swap(is_null_, other->is_null_); | 175 std::swap(is_null_, other->is_null_); |
| 156 vec_.swap(other->vec_); | 176 vec_.swap(other->vec_); |
| 157 } | 177 } |
| 158 | 178 |
| 159 // Swaps the contents of this array with the specified vector, making this | 179 // Swaps the contents of this array with the specified vector, making this |
| 160 // array non-null. Since the vector cannot represent null, it will just be | 180 // array non-null. Since the vector cannot represent null, it will just be |
| 161 // made empty if this array is null. | 181 // made empty if this array is null. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 if (a.is_null()) | 300 if (a.is_null()) |
| 281 return !b.is_null(); | 301 return !b.is_null(); |
| 282 if (b.is_null()) | 302 if (b.is_null()) |
| 283 return false; | 303 return false; |
| 284 return a.storage() < b.storage(); | 304 return a.storage() < b.storage(); |
| 285 } | 305 } |
| 286 | 306 |
| 287 } // namespace mojo | 307 } // namespace mojo |
| 288 | 308 |
| 289 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 309 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| OLD | NEW |