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

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: 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.
yzshen1 2015/01/20 22:45:49 nit: it might be nice to also mention the initiali
ggowan 2015/01/20 23:54:50 Done.
35 explicit Array(size_t size) : vec_(size), is_null_(false) { 40 explicit Array(size_t size) : vec_(size), is_null_(false) {
36 Traits::Initialize(&vec_); 41 Traits::Initialize(&vec_);
37 } 42 }
38 ~Array() { Traits::Finalize(&vec_); } 43 ~Array() { Traits::Finalize(&vec_); }
39 44
45 // Moves the contents of |other| into this array.
40 Array(Array&& other) : is_null_(true) { Take(&other); } 46 Array(Array&& other) : is_null_(true) { Take(&other); }
41 Array& operator=(Array&& other) { 47 Array& operator=(Array&& other) {
42 Take(&other); 48 Take(&other);
43 return *this; 49 return *this;
44 } 50 }
45 51
52 // Creates a non-null array of the specified size.
46 static Array New(size_t size) { return Array(size).Pass(); } 53 static Array New(size_t size) { return Array(size).Pass(); }
47 54
55 // Creates a new array with a copy of the contents of |other|.
48 template <typename U> 56 template <typename U>
49 static Array From(const U& other) { 57 static Array From(const U& other) {
50 return TypeConverter<Array, U>::Convert(other); 58 return TypeConverter<Array, U>::Convert(other);
51 } 59 }
52 60
61 // Copies the contents of this array to a new object of type |U|.
53 template <typename U> 62 template <typename U>
54 U To() const { 63 U To() const {
55 return TypeConverter<U, Array>::Convert(*this); 64 return TypeConverter<U, Array>::Convert(*this);
56 } 65 }
57 66
67 // Resets the contents of this array back to null.
58 void reset() { 68 void reset() {
59 if (!vec_.empty()) { 69 if (!vec_.empty()) {
60 Traits::Finalize(&vec_); 70 Traits::Finalize(&vec_);
61 vec_.clear(); 71 vec_.clear();
62 } 72 }
63 is_null_ = true; 73 is_null_ = true;
64 } 74 }
65 75
76 // Indicates whether the array is null (which is distinct from empty).
66 bool is_null() const { return is_null_; } 77 bool is_null() const { return is_null_; }
67 78
79 // Returns a reference to the first element of the array. Calling this on a
80 // null or empty array causes undefined behavior.
68 ConstRefType front() const { return vec_.front(); } 81 ConstRefType front() const { return vec_.front(); }
69 RefType front() { return vec_.front(); } 82 RefType front() { return vec_.front(); }
70 83
84 // Returns the size of the array, which will be zero if the array is null.
71 size_t size() const { return vec_.size(); } 85 size_t size() const { return vec_.size(); }
72 86
87 // Returns a reference to the element at zero-based |offset|. Calling this on
88 // an array with size less than |offset|+1 causes undefined behavior.
73 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } 89 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); }
74 ConstRefType operator[](size_t offset) const { return at(offset); } 90 ConstRefType operator[](size_t offset) const { return at(offset); }
75
76 RefType at(size_t offset) { return Traits::at(&vec_, offset); } 91 RefType at(size_t offset) { return Traits::at(&vec_, offset); }
77 RefType operator[](size_t offset) { return at(offset); } 92 RefType operator[](size_t offset) { return at(offset); }
78 93
94 // Pushes a copy of |value| onto the back of the array. If this array was
yzshen1 2015/01/20 22:45:49 Strictly speaking, "a copy of" is inaccurate when
ggowan 2015/01/20 23:54:50 True. I deleted "a copy of" since it was unnecessa
95 // null, it will become non-null with a size of 1.
79 void push_back(ForwardType value) { 96 void push_back(ForwardType value) {
80 is_null_ = false; 97 is_null_ = false;
81 Traits::PushBack(&vec_, value); 98 Traits::PushBack(&vec_, value);
82 } 99 }
83 100
101 // Resizes the array to |size| and makes it non-null.
yzshen1 2015/01/20 22:45:49 It is nice to mention initialization of those newl
ggowan 2015/01/20 23:54:50 I specified it by reference to std::vector, since
84 void resize(size_t size) { 102 void resize(size_t size) {
85 is_null_ = false; 103 is_null_ = false;
86 Traits::Resize(&vec_, size); 104 Traits::Resize(&vec_, size);
87 } 105 }
88 106
107 // Returns a const reference to the |std::vector| managed by this class. If
108 // the array is null, this will be an empty vector.
89 const std::vector<StorageType>& storage() const { return vec_; } 109 const std::vector<StorageType>& storage() const { return vec_; }
90 operator const std::vector<StorageType>&() const { return vec_; } 110 operator const std::vector<StorageType>&() const { return vec_; }
91 111
112 // Swaps the contents of this array with the |other| array, including
113 // nullness.
92 void Swap(Array* other) { 114 void Swap(Array* other) {
93 std::swap(is_null_, other->is_null_); 115 std::swap(is_null_, other->is_null_);
94 vec_.swap(other->vec_); 116 vec_.swap(other->vec_);
95 } 117 }
118
119 // Swaps the contents of this array with the specified vector, making this
120 // array non-null. Since the vector cannot represent null, it will just be
121 // made empty if this array is null.
96 void Swap(std::vector<StorageType>* other) { 122 void Swap(std::vector<StorageType>* other) {
97 is_null_ = false; 123 is_null_ = false;
98 vec_.swap(*other); 124 vec_.swap(*other);
99 } 125 }
100 126
127 // Returns a copy of the array where each value of the new array has been
128 // "cloned" from the corresponding value of this array. If this array contains
129 // primitive data types, this is equivalent to simply copying the contents.
130 // However, if the array contains objects, then each new element is created by
131 // calling the |Clone| method of the source element, which should make a copy
132 // of the element.
133 //
101 // Please note that calling this method will fail compilation if the element 134 // 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 135 // type cannot be cloned (which usually means that it is a Mojo handle type or
103 // a type contains Mojo handles). 136 // a type contains Mojo handles).
104 Array Clone() const { 137 Array Clone() const {
105 Array result; 138 Array result;
106 result.is_null_ = is_null_; 139 result.is_null_ = is_null_;
107 Traits::Clone(vec_, &result.vec_); 140 Traits::Clone(vec_, &result.vec_);
108 return result.Pass(); 141 return result.Pass();
109 } 142 }
110 143
144 // Indicates whether the contents of this array are equal to |other|. A null
145 // array is only equal to another null array. Elements are compared using the
146 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method
147 // of the element.
111 bool Equals(const Array& other) const { 148 bool Equals(const Array& other) const {
112 if (is_null() != other.is_null()) 149 if (is_null() != other.is_null())
113 return false; 150 return false;
114 if (size() != other.size()) 151 if (size() != other.size())
115 return false; 152 return false;
116 for (size_t i = 0; i < size(); ++i) { 153 for (size_t i = 0; i < size(); ++i) {
117 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) 154 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i)))
118 return false; 155 return false;
119 } 156 }
120 return true; 157 return true;
121 } 158 }
122 159
123 private: 160 private:
124 typedef std::vector<StorageType> Array::*Testable; 161 typedef std::vector<StorageType> Array::*Testable;
125 162
126 public: 163 public:
127 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } 164 operator Testable() const { return is_null_ ? 0 : &Array::vec_; }
128 165
129 private: 166 private:
130 void Take(Array* other) { 167 void Take(Array* other) {
131 reset(); 168 reset();
132 Swap(other); 169 Swap(other);
133 } 170 }
134 171
135 std::vector<StorageType> vec_; 172 std::vector<StorageType> vec_;
136 bool is_null_; 173 bool is_null_;
137 }; 174 };
138 175
176 // A |TypeConverter| that will create an |Array<T>| containing a copy of the
177 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each
178 // element. The returned array will always be non-null.
139 template <typename T, typename E> 179 template <typename T, typename E>
140 struct TypeConverter<Array<T>, std::vector<E>> { 180 struct TypeConverter<Array<T>, std::vector<E>> {
141 static Array<T> Convert(const std::vector<E>& input) { 181 static Array<T> Convert(const std::vector<E>& input) {
142 Array<T> result(input.size()); 182 Array<T> result(input.size());
143 for (size_t i = 0; i < input.size(); ++i) 183 for (size_t i = 0; i < input.size(); ++i)
144 result[i] = TypeConverter<T, E>::Convert(input[i]); 184 result[i] = TypeConverter<T, E>::Convert(input[i]);
145 return result.Pass(); 185 return result.Pass();
146 } 186 }
147 }; 187 };
148 188
189 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of
190 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each
191 // element. If the input array is null, the output vector will be empty.
149 template <typename E, typename T> 192 template <typename E, typename T>
150 struct TypeConverter<std::vector<E>, Array<T>> { 193 struct TypeConverter<std::vector<E>, Array<T>> {
151 static std::vector<E> Convert(const Array<T>& input) { 194 static std::vector<E> Convert(const Array<T>& input) {
152 std::vector<E> result; 195 std::vector<E> result;
153 if (!input.is_null()) { 196 if (!input.is_null()) {
154 result.resize(input.size()); 197 result.resize(input.size());
155 for (size_t i = 0; i < input.size(); ++i) 198 for (size_t i = 0; i < input.size(); ++i)
156 result[i] = TypeConverter<E, T>::Convert(input[i]); 199 result[i] = TypeConverter<E, T>::Convert(input[i]);
157 } 200 }
158 return result; 201 return result;
159 } 202 }
160 }; 203 };
161 204
162 } // namespace mojo 205 } // namespace mojo
163 206
164 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ 207 #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