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

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