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/lib/array_serialization.h

Issue 1397133002: Remove callers of mojo::Array<size_t> constructor in favor of ::New (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 2 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 | « mojo/public/cpp/bindings/array.h ('k') | mojo/public/cpp/bindings/tests/array_unittest.cc » ('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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_LIB_ARRAY_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
7 7
8 #include <string.h> // For |memcpy()|. 8 #include <string.h> // For |memcpy()|.
9 #include <vector> 9 #include <vector>
10 10
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 MOJO_DCHECK(!validate_params->element_validate_params) 99 MOJO_DCHECK(!validate_params->element_validate_params)
100 << "Primitive type should not have array validate params"; 100 << "Primitive type should not have array validate params";
101 101
102 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? 102 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy?
103 for (size_t i = 0; i < num_elements; ++i, ++it) 103 for (size_t i = 0; i < num_elements; ++i, ++it)
104 output->at(i) = *it; 104 output->at(i) = *it;
105 } 105 }
106 106
107 static void DeserializeElements(Array_Data<bool>* input, 107 static void DeserializeElements(Array_Data<bool>* input,
108 Array<bool>* output) { 108 Array<bool>* output) {
109 Array<bool> result(input->size()); 109 auto result = Array<bool>::New(input->size());
110 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? 110 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy?
111 for (size_t i = 0; i < input->size(); ++i) 111 for (size_t i = 0; i < input->size(); ++i)
112 result.at(i) = input->at(i); 112 result.at(i) = input->at(i);
113 output->Swap(&result); 113 output->Swap(&result);
114 } 114 }
115 }; 115 };
116 116
117 // Serializes and deserializes arrays of handles. 117 // Serializes and deserializes arrays of handles.
118 template <typename H> 118 template <typename H>
119 struct ArraySerializer<ScopedHandleBase<H>, H, false> { 119 struct ArraySerializer<ScopedHandleBase<H>, H, false> {
(...skipping 17 matching lines...) Expand all
137 !validate_params->element_is_nullable && !output->at(i).is_valid(), 137 !validate_params->element_is_nullable && !output->at(i).is_valid(),
138 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, 138 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
139 MakeMessageWithArrayIndex( 139 MakeMessageWithArrayIndex(
140 "invalid handle in array expecting valid handles", num_elements, 140 "invalid handle in array expecting valid handles", num_elements,
141 i)); 141 i));
142 } 142 }
143 } 143 }
144 144
145 static void DeserializeElements(Array_Data<H>* input, 145 static void DeserializeElements(Array_Data<H>* input,
146 Array<ScopedHandleBase<H>>* output) { 146 Array<ScopedHandleBase<H>>* output) {
147 Array<ScopedHandleBase<H>> result(input->size()); 147 auto result = Array<ScopedHandleBase<H>>::New(input->size());
148 for (size_t i = 0; i < input->size(); ++i) 148 for (size_t i = 0; i < input->size(); ++i)
149 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); 149 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i)));
150 output->Swap(&result); 150 output->Swap(&result);
151 } 151 }
152 }; 152 };
153 153
154 // This template must only apply to pointer mojo entity (structs, arrays, 154 // This template must only apply to pointer mojo entity (structs, arrays,
155 // strings). This is done by ensuring that WrapperTraits<S>::DataType is a 155 // strings). This is done by ensuring that WrapperTraits<S>::DataType is a
156 // pointer. 156 // pointer.
157 template <typename S> 157 template <typename S>
(...skipping 26 matching lines...) Expand all
184 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 184 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
185 !validate_params->element_is_nullable && !element, 185 !validate_params->element_is_nullable && !element,
186 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 186 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
187 MakeMessageWithArrayIndex("null in array expecting valid pointers", 187 MakeMessageWithArrayIndex("null in array expecting valid pointers",
188 num_elements, i)); 188 num_elements, i));
189 } 189 }
190 } 190 }
191 191
192 static void DeserializeElements(Array_Data<S_Data*>* input, 192 static void DeserializeElements(Array_Data<S_Data*>* input,
193 Array<S>* output) { 193 Array<S>* output) {
194 Array<S> result(input->size()); 194 auto result = Array<S>::New(input->size());
195 for (size_t i = 0; i < input->size(); ++i) { 195 for (size_t i = 0; i < input->size(); ++i) {
196 DeserializeCaller::Run(input->at(i), &result[i]); 196 DeserializeCaller::Run(input->at(i), &result[i]);
197 } 197 }
198 output->Swap(&result); 198 output->Swap(&result);
199 } 199 }
200 200
201 private: 201 private:
202 // SerializeCaller template is used by |ArraySerializer| to dispatch a 202 // SerializeCaller template is used by |ArraySerializer| to dispatch a
203 // serialize call on a non-POD type. This template is defined outside 203 // serialize call on a non-POD type. This template is defined outside
204 // |ArraySerializer| since you cannot specialize a struct within a class 204 // |ArraySerializer| since you cannot specialize a struct within a class
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 SerializeUnion_(it->get(), buf, &result, true); 297 SerializeUnion_(it->get(), buf, &result, true);
298 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 298 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
299 !validate_params->element_is_nullable && output->at(i).is_null(), 299 !validate_params->element_is_nullable && output->at(i).is_null(),
300 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 300 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
301 MakeMessageWithArrayIndex("null in array expecting valid unions", 301 MakeMessageWithArrayIndex("null in array expecting valid unions",
302 num_elements, i)); 302 num_elements, i));
303 } 303 }
304 } 304 }
305 305
306 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { 306 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) {
307 Array<U> result(input->size()); 307 auto result = Array<U>::New(input->size());
308 for (size_t i = 0; i < input->size(); ++i) { 308 for (size_t i = 0; i < input->size(); ++i) {
309 auto& elem = input->at(i); 309 auto& elem = input->at(i);
310 if (!elem.is_null()) { 310 if (!elem.is_null()) {
311 using UnwrapedUnionType = typename RemoveStructPtr<U>::type; 311 using UnwrapedUnionType = typename RemoveStructPtr<U>::type;
312 result[i] = UnwrapedUnionType::New(); 312 result[i] = UnwrapedUnionType::New();
313 Deserialize_(&elem, result[i].get()); 313 Deserialize_(&elem, result[i].get());
314 } 314 }
315 } 315 }
316 output->Swap(&result); 316 output->Swap(&result);
317 } 317 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 if (input) { 360 if (input) {
361 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 361 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
362 } else { 362 } else {
363 output->reset(); 363 output->reset();
364 } 364 }
365 } 365 }
366 366
367 } // namespace mojo 367 } // namespace mojo
368 368
369 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 369 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/array.h ('k') | mojo/public/cpp/bindings/tests/array_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698