| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/bindings/array_traits_carray.h" | 10 #include "mojo/public/cpp/bindings/array_traits_carray.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 bool need_copy = !IsAligned(result_buffer); | 48 bool need_copy = !IsAligned(result_buffer); |
| 49 | 49 |
| 50 if (need_copy) { | 50 if (need_copy) { |
| 51 // calloc sets the memory to all zero. | 51 // calloc sets the memory to all zero. |
| 52 result_buffer = calloc(size, 1); | 52 result_buffer = calloc(size, 1); |
| 53 DCHECK(IsAligned(result_buffer)); | 53 DCHECK(IsAligned(result_buffer)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 Buffer buffer; | 56 Buffer buffer; |
| 57 buffer.Initialize(result_buffer, size); | 57 buffer.Initialize(result_buffer, size); |
| 58 typename MojomType::Struct::Data_* data = nullptr; | 58 typename MojomTypeTraits<MojomType>::Data* data = nullptr; |
| 59 Serialize<MojomType>(*input, &buffer, &data, &context); | 59 Serialize<MojomType>(*input, &buffer, &data, &context); |
| 60 | 60 |
| 61 if (need_copy) { | 61 if (need_copy) { |
| 62 memcpy(&result.front(), result_buffer, size); | 62 memcpy(&result.front(), result_buffer, size); |
| 63 free(result_buffer); | 63 free(result_buffer); |
| 64 } | 64 } |
| 65 | 65 |
| 66 return result; | 66 return result; |
| 67 } | 67 } |
| 68 | 68 |
| 69 template <typename MojomType, typename DataArrayType, typename UserType> | 69 template <typename MojomType, typename DataArrayType, typename UserType> |
| 70 bool StructDeserializeImpl(const DataArrayType& input, UserType* output) { | 70 bool StructDeserializeImpl(const DataArrayType& input, UserType* output) { |
| 71 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value, | 71 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value, |
| 72 "Unexpected type."); | 72 "Unexpected type."); |
| 73 using DataType = typename MojomType::Struct::Data_; | 73 using DataType = typename MojomTypeTraits<MojomType>::Data; |
| 74 | 74 |
| 75 if (input.is_null()) | 75 if (input.is_null()) |
| 76 return false; | 76 return false; |
| 77 | 77 |
| 78 void* input_buffer = | 78 void* input_buffer = |
| 79 input.empty() | 79 input.empty() |
| 80 ? nullptr | 80 ? nullptr |
| 81 : const_cast<void*>(reinterpret_cast<const void*>(&input.front())); | 81 : const_cast<void*>(reinterpret_cast<const void*>(&input.front())); |
| 82 | 82 |
| 83 // Please see comments in StructSerializeImpl. | 83 // Please see comments in StructSerializeImpl. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 100 if (need_copy) | 100 if (need_copy) |
| 101 free(input_buffer); | 101 free(input_buffer); |
| 102 | 102 |
| 103 return result; | 103 return result; |
| 104 } | 104 } |
| 105 | 105 |
| 106 } // namespace internal | 106 } // namespace internal |
| 107 } // namespace mojo | 107 } // namespace mojo |
| 108 | 108 |
| 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ | 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ |
| OLD | NEW |