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

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

Issue 2259283003: Mojo C++ bindings: share DataView class between chromium and blink variants. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@92_change_traits_param
Patch Set: . Created 4 years, 4 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 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
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
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_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/pipe_control_message_proxy.cc ('k') | mojo/public/cpp/bindings/lib/string_serialization.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698