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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(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 MojomType::Struct::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 = input.empty() ? nullptr : &input.front(); | 78 void* input_buffer = |
| 79 input.empty() |
| 80 ? nullptr |
| 81 : const_cast<void*>(reinterpret_cast<const void*>(&input.front())); |
79 | 82 |
80 // Please see comments in StructSerializeImpl. | 83 // Please see comments in StructSerializeImpl. |
81 bool need_copy = !IsAligned(input_buffer); | 84 bool need_copy = !IsAligned(input_buffer); |
82 | 85 |
83 if (need_copy) { | 86 if (need_copy) { |
84 input_buffer = malloc(input.size()); | 87 input_buffer = malloc(input.size()); |
85 DCHECK(IsAligned(input_buffer)); | 88 DCHECK(IsAligned(input_buffer)); |
86 memcpy(input_buffer, &input.front(), input.size()); | 89 memcpy(input_buffer, &input.front(), input.size()); |
87 } | 90 } |
88 | 91 |
89 ValidationContext validation_context(input_buffer, input.size(), 0); | 92 ValidationContext validation_context(input_buffer, input.size(), 0); |
90 bool result = false; | 93 bool result = false; |
91 if (DataType::Validate(input_buffer, &validation_context)) { | 94 if (DataType::Validate(input_buffer, &validation_context)) { |
92 auto data = reinterpret_cast<DataType*>(input_buffer); | 95 auto data = reinterpret_cast<DataType*>(input_buffer); |
93 SerializationContext context; | 96 SerializationContext context; |
94 result = Deserialize<MojomType>(data, output, &context); | 97 result = Deserialize<MojomType>(data, output, &context); |
95 } | 98 } |
96 | 99 |
97 if (need_copy) | 100 if (need_copy) |
98 free(input_buffer); | 101 free(input_buffer); |
99 | 102 |
100 return result; | 103 return result; |
101 } | 104 } |
102 | 105 |
103 } // namespace internal | 106 } // namespace internal |
104 } // namespace mojo | 107 } // namespace mojo |
105 | 108 |
106 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ | 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ |
OLD | NEW |