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

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

Issue 1956603002: Mojo C++ bindings: switch union to use the new serialization interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@20_array_serializer
Patch Set: Created 4 years, 7 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 #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 <stddef.h> 8 #include <stddef.h>
9 #include <string.h> // For |memcpy()|. 9 #include <string.h> // For |memcpy()|.
10 10
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 }; 288 };
289 }; 289 };
290 290
291 // Handles serialization and deserialization of arrays of unions. 291 // Handles serialization and deserialization of arrays of unions.
292 template <typename MojomType, typename MaybeConstUserType> 292 template <typename MojomType, typename MaybeConstUserType>
293 struct ArraySerializer<MojomType, 293 struct ArraySerializer<MojomType,
294 MaybeConstUserType, 294 MaybeConstUserType,
295 ArraySerializerType::UNION> { 295 ArraySerializerType::UNION> {
296 using UserType = typename std::remove_const<MaybeConstUserType>::type; 296 using UserType = typename std::remove_const<MaybeConstUserType>::type;
297 using Data = typename MojomType::Data_; 297 using Data = typename MojomType::Data_;
298 using Element = typename MojomType::Element;
298 using Traits = ArrayTraits<UserType>; 299 using Traits = ArrayTraits<UserType>;
299 300
300 static_assert(std::is_same<typename MojomType::Element, 301 static_assert(std::is_same<typename MojomType::Element,
301 typename Traits::Element>::value, 302 typename Traits::Element>::value,
302 "Incorrect array serializer"); 303 "Incorrect array serializer");
303 304
304 static size_t GetSerializedSize(MaybeConstUserType& input, 305 static size_t GetSerializedSize(MaybeConstUserType& input,
305 SerializationContext* context) { 306 SerializationContext* context) {
306 size_t element_count = Traits::GetSize(input); 307 size_t element_count = Traits::GetSize(input);
307 size_t size = sizeof(Data); 308 size_t size = sizeof(Data);
308 for (size_t i = 0; i < element_count; ++i) { 309 for (size_t i = 0; i < element_count; ++i) {
309 // Call GetSerializedSize_ with |inlined| set to false, so that it will 310 // Call with |inlined| set to false, so that it will account for both the
310 // account for both the data in the union and the space in the array used 311 // data in the union and the space in the array used to hold the union.
311 // to hold the union. 312 size +=
312 size += GetSerializedSize_(Traits::GetAt(input, i), false, context); 313 PrepareToSerialize<Element>(Traits::GetAt(input, i), false, context);
313 } 314 }
314 return size; 315 return size;
315 } 316 }
316 317
317 static void SerializeElements(MaybeConstUserType& input, 318 static void SerializeElements(MaybeConstUserType& input,
318 Buffer* buf, 319 Buffer* buf,
319 Data* output, 320 Data* output,
320 const ArrayValidateParams* validate_params, 321 const ArrayValidateParams* validate_params,
321 SerializationContext* context) { 322 SerializationContext* context) {
322 size_t size = Traits::GetSize(input); 323 size_t size = Traits::GetSize(input);
323 for (size_t i = 0; i < size; ++i) { 324 for (size_t i = 0; i < size; ++i) {
324 typename Data::Element* result = output->storage() + i; 325 typename Data::Element* result = output->storage() + i;
325 SerializeUnion_(std::move(Traits::GetAt(input, i)), buf, &result, true, 326 Serialize<Element>(Traits::GetAt(input, i), buf, &result, true, context);
326 context);
327 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 327 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
328 !validate_params->element_is_nullable && output->at(i).is_null(), 328 !validate_params->element_is_nullable && output->at(i).is_null(),
329 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 329 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
330 MakeMessageWithArrayIndex("null in array expecting valid unions", 330 MakeMessageWithArrayIndex("null in array expecting valid unions",
331 size, i)); 331 size, i));
332 } 332 }
333 } 333 }
334 334
335 static bool DeserializeElements(Data* input, 335 static bool DeserializeElements(Data* input,
336 UserType* output, 336 UserType* output,
337 SerializationContext* context) { 337 SerializationContext* context) {
338 bool success = true; 338 bool success = true;
339 Traits::Resize(*output, input->size()); 339 Traits::Resize(*output, input->size());
340 for (size_t i = 0; i < input->size(); ++i) { 340 for (size_t i = 0; i < input->size(); ++i) {
341 // Note that we rely on complete deserialization taking place in order to 341 // Note that we rely on complete deserialization taking place in order to
342 // transfer ownership of all encoded handles. Therefore we don't 342 // transfer ownership of all encoded handles. Therefore we don't
343 // short-circuit on failure here. 343 // short-circuit on failure here.
344 if (!Deserialize_(&input->at(i), &Traits::GetAt(*output, i), context)) 344 if (!Deserialize<Element>(&input->at(i), &Traits::GetAt(*output, i),
345 context)) {
345 success = false; 346 success = false;
347 }
346 } 348 }
347 return success; 349 return success;
348 } 350 }
349 }; 351 };
350 352
351 template <typename Element, typename MaybeConstUserType> 353 template <typename Element, typename MaybeConstUserType>
352 struct Serializer<Array<Element>, MaybeConstUserType> { 354 struct Serializer<Array<Element>, MaybeConstUserType> {
353 using UserType = typename std::remove_const<MaybeConstUserType>::type; 355 using UserType = typename std::remove_const<MaybeConstUserType>::type;
354 using Impl = ArraySerializer<Array<Element>, MaybeConstUserType>; 356 using Impl = ArraySerializer<Array<Element>, MaybeConstUserType>;
355 using Traits = ArrayTraits<UserType>; 357 using Traits = ArrayTraits<UserType>;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 return Impl::DeserializeElements(input, output, context); 393 return Impl::DeserializeElements(input, output, context);
392 Traits::SetToNull(*output); 394 Traits::SetToNull(*output);
393 return true; 395 return true;
394 } 396 }
395 }; 397 };
396 398
397 } // namespace internal 399 } // namespace internal
398 } // namespace mojo 400 } // namespace mojo
399 401
400 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 402 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698