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

Side by Side Diff: mojo/public/cpp/bindings/lib/array_serialization.h

Issue 1682113003: Mojo C++ bindings: Generate InterfaceHandle<> instead of InterfacePtr<>. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebase ontop of master, address trung's comments Created 4 years, 10 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
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 // TODO(vardhan): Currently, the logic for serializing a mojom type exists in 5 // TODO(vardhan): Currently, the logic for serializing a mojom type exists in
6 // two places: the C++ code generator template, and here. However, most types 6 // two places: the C++ code generator template, and here. However, most types
7 // are serialized the same way within Arrays or outside, with the exception of 7 // are serialized the same way within Arrays or outside, with the exception of
8 // |bool|. Consider defining serialization/deserialization traits for each 8 // |bool|. Consider defining serialization/deserialization traits for each
9 // serializable type and call those traits from here. This should help us 9 // serializable type and call those traits from here. This should help us
10 // remove most of the ArraySerializer<> specializations here. 10 // remove most of the ArraySerializer<> specializations here.
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 static void DeserializeElements(Array_Data<MessagePipeHandle>* input, 233 static void DeserializeElements(Array_Data<MessagePipeHandle>* input,
234 Array<InterfaceRequest<I>>* output) { 234 Array<InterfaceRequest<I>>* output) {
235 auto result = Array<InterfaceRequest<I>>::New(input->size()); 235 auto result = Array<InterfaceRequest<I>>::New(input->size());
236 for (size_t i = 0; i < input->size(); ++i) 236 for (size_t i = 0; i < input->size(); ++i)
237 result.at(i) = 237 result.at(i) =
238 MakeRequest<I>(MakeScopedHandle(FetchAndReset(&input->at(i)))).Pass(); 238 MakeRequest<I>(MakeScopedHandle(FetchAndReset(&input->at(i)))).Pass();
239 output->Swap(&result); 239 output->Swap(&result);
240 } 240 }
241 }; 241 };
242 242
243 // Serializes and deserializes arrays of interfaces (interface pointers). 243 // Serializes and deserializes arrays of interfaces (interface handles).
244 template <typename Interface> 244 template <typename Interface>
245 struct ArraySerializer<InterfacePtr<Interface>, Interface_Data, false> { 245 struct ArraySerializer<InterfaceHandle<Interface>, Interface_Data, false> {
246 static size_t GetSerializedSize(const Array<InterfacePtr<Interface>>& input) { 246 static size_t GetSerializedSize(
247 const Array<InterfaceHandle<Interface>>& input) {
247 return sizeof(Array_Data<Interface_Data>) + 248 return sizeof(Array_Data<Interface_Data>) +
248 Align(input.size() * sizeof(Interface_Data)); 249 Align(input.size() * sizeof(Interface_Data));
249 } 250 }
250 251
251 template <typename Iterator> 252 template <typename Iterator>
252 static ValidationError SerializeElements( 253 static ValidationError SerializeElements(
253 Iterator it, 254 Iterator it,
254 size_t num_elements, 255 size_t num_elements,
255 Buffer* buf, 256 Buffer* buf,
256 Array_Data<Interface_Data>* output, 257 Array_Data<Interface_Data>* output,
257 const ArrayValidateParams* validate_params) { 258 const ArrayValidateParams* validate_params) {
258 MOJO_DCHECK(!validate_params->element_validate_params) 259 MOJO_DCHECK(!validate_params->element_validate_params)
259 << "Interface type should not have array validate params"; 260 << "Interface type should not have array validate params";
260 261
261 for (size_t i = 0; i < num_elements; ++i, ++it) { 262 for (size_t i = 0; i < num_elements; ++i, ++it) {
262 // Transfer ownership of the handle. 263 // Transfer ownership of the handle.
263 internal::InterfacePointerToData(it->Pass(), &output->at(i)); 264 internal::InterfaceHandleToData(it->Pass(), &output->at(i));
264 if (!validate_params->element_is_nullable && 265 if (!validate_params->element_is_nullable &&
265 !output->at(i).handle.is_valid()) { 266 !output->at(i).handle.is_valid()) {
266 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 267 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
267 ValidationError::UNEXPECTED_INVALID_HANDLE, 268 ValidationError::UNEXPECTED_INVALID_HANDLE,
268 MakeMessageWithArrayIndex( 269 MakeMessageWithArrayIndex(
269 "invalid handle in array expecting valid handles", num_elements, 270 "invalid handle in array expecting valid handles", num_elements,
270 i)); 271 i));
271 return ValidationError::UNEXPECTED_INVALID_HANDLE; 272 return ValidationError::UNEXPECTED_INVALID_HANDLE;
272 } 273 }
273 } 274 }
274 275
275 return ValidationError::NONE; 276 return ValidationError::NONE;
276 } 277 }
277 278
278 static void DeserializeElements(Array_Data<Interface_Data>* input, 279 static void DeserializeElements(Array_Data<Interface_Data>* input,
279 Array<InterfacePtr<Interface>>* output) { 280 Array<InterfaceHandle<Interface>>* output) {
280 auto result = Array<InterfacePtr<Interface>>::New(input->size()); 281 auto result = Array<InterfaceHandle<Interface>>::New(input->size());
281 for (size_t i = 0; i < input->size(); ++i) 282 for (size_t i = 0; i < input->size(); ++i)
282 internal::InterfaceDataToPointer(&input->at(i), &result.at(i)); 283 internal::InterfaceDataToHandle(&input->at(i), &result.at(i));
283 output->Swap(&result); 284 output->Swap(&result);
284 } 285 }
285 }; 286 };
286 287
287 // This template must only apply to pointer mojo entity (structs, arrays, 288 // This template must only apply to pointer mojo entity (structs, arrays,
288 // strings). This is done by ensuring that WrapperTraits<S>::DataType is a 289 // strings). This is done by ensuring that WrapperTraits<S>::DataType is a
289 // pointer. 290 // pointer.
290 template <typename S> 291 template <typename S>
291 struct ArraySerializer< 292 struct ArraySerializer<
292 S, 293 S,
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 if (input) { 532 if (input) {
532 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 533 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
533 } else { 534 } else {
534 output->reset(); 535 output->reset();
535 } 536 }
536 } 537 }
537 538
538 } // namespace mojo 539 } // namespace mojo
539 540
540 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 541 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/interface_request.h ('k') | mojo/public/cpp/bindings/lib/bindings_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698