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

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

Issue 792543005: Adding API documentation to the C++ Array and Binding classes. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Responding to comments. Created 5 years, 11 months 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
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/binding.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
21 template <typename T> 23 template <typename T>
22 class Array { 24 class Array {
23 MOJO_MOVE_ONLY_TYPE(Array) 25 MOJO_MOVE_ONLY_TYPE(Array)
24 public: 26 public:
25 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits; 27 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits;
26 typedef typename Traits::ConstRefType ConstRefType; 28 typedef typename Traits::ConstRefType ConstRefType;
27 typedef typename Traits::RefType RefType; 29 typedef typename Traits::RefType RefType;
28 typedef typename Traits::StorageType StorageType; 30 typedef typename Traits::StorageType StorageType;
29 typedef typename Traits::ForwardType ForwardType; 31 typedef typename Traits::ForwardType ForwardType;
30 32
31 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> 33 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType>
32 Data_; 34 Data_;
33 35
36 // Constructs a new array that is null.
34 Array() : is_null_(true) {} 37 Array() : is_null_(true) {}
38
39 // Constructs a new non-null array of the specified size. If the elements are
40 // of a type that has a constructor, the default constructor will be invoked
41 // to initialize each new element. Otherwise, the value of the new elements is
yzshen1 2015/01/21 17:30:40 I thought they will be value initialized? For exam
ggowan 2015/01/21 18:56:19 Thanks, my mistake.
42 // undefined and they should be initialized by the caller.
35 explicit Array(size_t size) : vec_(size), is_null_(false) { 43 explicit Array(size_t size) : vec_(size), is_null_(false) {
36 Traits::Initialize(&vec_); 44 Traits::Initialize(&vec_);
37 } 45 }
38 ~Array() { Traits::Finalize(&vec_); } 46 ~Array() { Traits::Finalize(&vec_); }
39 47
48 // Moves the contents of |other| into this array.
40 Array(Array&& other) : is_null_(true) { Take(&other); } 49 Array(Array&& other) : is_null_(true) { Take(&other); }
41 Array& operator=(Array&& other) { 50 Array& operator=(Array&& other) {
42 Take(&other); 51 Take(&other);
43 return *this; 52 return *this;
44 } 53 }
45 54
55 // Creates a non-null array of the specified size. If the elements are of a
56 // type that has a constructor, the default constructor will be invoked to
57 // initialize each new element. Otherwise, the value of the new elements is
58 // undefined and they should be initialized by the caller.
yzshen1 2015/01/21 17:30:40 ditto.
ggowan 2015/01/21 18:56:19 Done.
46 static Array New(size_t size) { return Array(size).Pass(); } 59 static Array New(size_t size) { return Array(size).Pass(); }
47 60
61 // Creates a new array with a copy of the contents of |other|.
48 template <typename U> 62 template <typename U>
49 static Array From(const U& other) { 63 static Array From(const U& other) {
50 return TypeConverter<Array, U>::Convert(other); 64 return TypeConverter<Array, U>::Convert(other);
51 } 65 }
52 66
67 // Copies the contents of this array to a new object of type |U|.
53 template <typename U> 68 template <typename U>
54 U To() const { 69 U To() const {
55 return TypeConverter<U, Array>::Convert(*this); 70 return TypeConverter<U, Array>::Convert(*this);
56 } 71 }
57 72
73 // Resets the contents of this array back to null.
58 void reset() { 74 void reset() {
59 if (!vec_.empty()) { 75 if (!vec_.empty()) {
60 Traits::Finalize(&vec_); 76 Traits::Finalize(&vec_);
61 vec_.clear(); 77 vec_.clear();
62 } 78 }
63 is_null_ = true; 79 is_null_ = true;
64 } 80 }
65 81
82 // Indicates whether the array is null (which is distinct from empty).
66 bool is_null() const { return is_null_; } 83 bool is_null() const { return is_null_; }
67 84
85 // Returns a reference to the first element of the array. Calling this on a
86 // null or empty array causes undefined behavior.
68 ConstRefType front() const { return vec_.front(); } 87 ConstRefType front() const { return vec_.front(); }
69 RefType front() { return vec_.front(); } 88 RefType front() { return vec_.front(); }
70 89
90 // Returns the size of the array, which will be zero if the array is null.
71 size_t size() const { return vec_.size(); } 91 size_t size() const { return vec_.size(); }
72 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.
73 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } 95 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); }
74 ConstRefType operator[](size_t offset) const { return at(offset); } 96 ConstRefType operator[](size_t offset) const { return at(offset); }
75
76 RefType at(size_t offset) { return Traits::at(&vec_, offset); } 97 RefType at(size_t offset) { return Traits::at(&vec_, offset); }
77 RefType operator[](size_t offset) { return at(offset); } 98 RefType operator[](size_t offset) { return at(offset); }
78 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.
79 void push_back(ForwardType value) { 102 void push_back(ForwardType value) {
80 is_null_ = false; 103 is_null_ = false;
81 Traits::PushBack(&vec_, value); 104 Traits::PushBack(&vec_, value);
82 } 105 }
83 106
107 // Resizes the array to |size| and makes it non-null. Otherwise, works just
108 // like the resize method of |std::vector|.
84 void resize(size_t size) { 109 void resize(size_t size) {
85 is_null_ = false; 110 is_null_ = false;
86 Traits::Resize(&vec_, size); 111 Traits::Resize(&vec_, size);
87 } 112 }
88 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.
89 const std::vector<StorageType>& storage() const { return vec_; } 116 const std::vector<StorageType>& storage() const { return vec_; }
90 operator const std::vector<StorageType>&() const { return vec_; } 117 operator const std::vector<StorageType>&() const { return vec_; }
91 118
119 // Swaps the contents of this array with the |other| array, including
120 // nullness.
92 void Swap(Array* other) { 121 void Swap(Array* other) {
93 std::swap(is_null_, other->is_null_); 122 std::swap(is_null_, other->is_null_);
94 vec_.swap(other->vec_); 123 vec_.swap(other->vec_);
95 } 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.
96 void Swap(std::vector<StorageType>* other) { 129 void Swap(std::vector<StorageType>* other) {
97 is_null_ = false; 130 is_null_ = false;
98 vec_.swap(*other); 131 vec_.swap(*other);
99 } 132 }
100 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 //
101 // Please note that calling this method will fail compilation if the element 141 // Please note that calling this method will fail compilation if the element
102 // type cannot be cloned (which usually means that it is a Mojo handle type or 142 // type cannot be cloned (which usually means that it is a Mojo handle type or
103 // a type contains Mojo handles). 143 // a type contains Mojo handles).
104 Array Clone() const { 144 Array Clone() const {
105 Array result; 145 Array result;
106 result.is_null_ = is_null_; 146 result.is_null_ = is_null_;
107 Traits::Clone(vec_, &result.vec_); 147 Traits::Clone(vec_, &result.vec_);
108 return result.Pass(); 148 return result.Pass();
109 } 149 }
110 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.
111 bool Equals(const Array& other) const { 155 bool Equals(const Array& other) const {
112 if (is_null() != other.is_null()) 156 if (is_null() != other.is_null())
113 return false; 157 return false;
114 if (size() != other.size()) 158 if (size() != other.size())
115 return false; 159 return false;
116 for (size_t i = 0; i < size(); ++i) { 160 for (size_t i = 0; i < size(); ++i) {
117 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) 161 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i)))
118 return false; 162 return false;
119 } 163 }
120 return true; 164 return true;
121 } 165 }
122 166
123 private: 167 private:
124 typedef std::vector<StorageType> Array::*Testable; 168 typedef std::vector<StorageType> Array::*Testable;
125 169
126 public: 170 public:
127 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } 171 operator Testable() const { return is_null_ ? 0 : &Array::vec_; }
128 172
129 private: 173 private:
130 void Take(Array* other) { 174 void Take(Array* other) {
131 reset(); 175 reset();
132 Swap(other); 176 Swap(other);
133 } 177 }
134 178
135 std::vector<StorageType> vec_; 179 std::vector<StorageType> vec_;
136 bool is_null_; 180 bool is_null_;
137 }; 181 };
138 182
183 // A |TypeConverter| that will create an |Array<T>| containing a copy of the
184 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each
185 // element. The returned array will always be non-null.
139 template <typename T, typename E> 186 template <typename T, typename E>
140 struct TypeConverter<Array<T>, std::vector<E>> { 187 struct TypeConverter<Array<T>, std::vector<E>> {
141 static Array<T> Convert(const std::vector<E>& input) { 188 static Array<T> Convert(const std::vector<E>& input) {
142 Array<T> result(input.size()); 189 Array<T> result(input.size());
143 for (size_t i = 0; i < input.size(); ++i) 190 for (size_t i = 0; i < input.size(); ++i)
144 result[i] = TypeConverter<T, E>::Convert(input[i]); 191 result[i] = TypeConverter<T, E>::Convert(input[i]);
145 return result.Pass(); 192 return result.Pass();
146 } 193 }
147 }; 194 };
148 195
196 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of
197 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each
198 // element. If the input array is null, the output vector will be empty.
149 template <typename E, typename T> 199 template <typename E, typename T>
150 struct TypeConverter<std::vector<E>, Array<T>> { 200 struct TypeConverter<std::vector<E>, Array<T>> {
151 static std::vector<E> Convert(const Array<T>& input) { 201 static std::vector<E> Convert(const Array<T>& input) {
152 std::vector<E> result; 202 std::vector<E> result;
153 if (!input.is_null()) { 203 if (!input.is_null()) {
154 result.resize(input.size()); 204 result.resize(input.size());
155 for (size_t i = 0; i < input.size(); ++i) 205 for (size_t i = 0; i < input.size(); ++i)
156 result[i] = TypeConverter<E, T>::Convert(input[i]); 206 result[i] = TypeConverter<E, T>::Convert(input[i]);
157 } 207 }
158 return result; 208 return result;
159 } 209 }
160 }; 210 };
161 211
162 } // namespace mojo 212 } // namespace mojo
163 213
164 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ 214 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/binding.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698