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

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

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 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 <string.h> // For |memcpy()|. 8 #include <string.h> // For |memcpy()|.
9 9
10 #include <vector> 10 #include <vector>
(...skipping 27 matching lines...) Expand all
38 inline void SerializeMap_( 38 inline void SerializeMap_(
39 Map<KeyWrapperType, ValueWrapperType> input, 39 Map<KeyWrapperType, ValueWrapperType> input,
40 internal::Buffer* buf, 40 internal::Buffer* buf,
41 internal::Map_Data<KeySerializationType, ValueSerializationType>** output); 41 internal::Map_Data<KeySerializationType, ValueSerializationType>** output);
42 42
43 template <typename E, typename F> 43 template <typename E, typename F>
44 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); 44 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output);
45 45
46 namespace internal { 46 namespace internal {
47 47
48 template <typename E, typename F, bool move_only = IsMoveOnlyType<E>::value> 48 template <typename E,
49 typename F,
50 bool move_only = IsMoveOnlyType<E>::value,
51 bool is_union =
52 IsUnionDataType<typename internal::RemovePointer<F>::type>::value>
49 struct ArraySerializer; 53 struct ArraySerializer;
50 54
55 // Handles serialization and deserialization of arrays of pod types.
51 template <typename E, typename F> 56 template <typename E, typename F>
52 struct ArraySerializer<E, F, false> { 57 struct ArraySerializer<E, F, false, false> {
53 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); 58 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer");
54 static size_t GetSerializedSize(const Array<E>& input) { 59 static size_t GetSerializedSize(const Array<E>& input) {
55 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); 60 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E));
56 } 61 }
57 template <bool element_is_nullable, typename ElementValidateParams> 62 template <bool element_is_nullable, typename ElementValidateParams>
58 static void SerializeElements(Array<E> input, 63 static void SerializeElements(Array<E> input,
59 Buffer* buf, 64 Buffer* buf,
60 Array_Data<F>* output) { 65 Array_Data<F>* output) {
61 static_assert(!element_is_nullable, 66 static_assert(!element_is_nullable,
62 "Primitive type should be non-nullable"); 67 "Primitive type should be non-nullable");
63 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), 68 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value),
64 "Primitive type should not have array validate params"); 69 "Primitive type should not have array validate params");
65 70
66 if (input.size()) 71 if (input.size())
67 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); 72 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E));
68 } 73 }
69 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { 74 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) {
70 std::vector<E> result(input->size()); 75 std::vector<E> result(input->size());
71 if (input->size()) 76 if (input->size())
72 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); 77 memcpy(&result[0], input->storage(), input->size() * sizeof(E));
73 output->Swap(&result); 78 output->Swap(&result);
74 } 79 }
75 }; 80 };
76 81
82 // Serializes and deserializes arrays of bools.
77 template <> 83 template <>
78 struct ArraySerializer<bool, bool, false> { 84 struct ArraySerializer<bool, bool, false, false> {
79 static size_t GetSerializedSize(const Array<bool>& input) { 85 static size_t GetSerializedSize(const Array<bool>& input) {
80 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); 86 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8);
81 } 87 }
82 template <bool element_is_nullable, typename ElementValidateParams> 88 template <bool element_is_nullable, typename ElementValidateParams>
83 static void SerializeElements(Array<bool> input, 89 static void SerializeElements(Array<bool> input,
84 Buffer* buf, 90 Buffer* buf,
85 Array_Data<bool>* output) { 91 Array_Data<bool>* output) {
86 static_assert(!element_is_nullable, 92 static_assert(!element_is_nullable,
87 "Primitive type should be non-nullable"); 93 "Primitive type should be non-nullable");
88 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), 94 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value),
89 "Primitive type should not have array validate params"); 95 "Primitive type should not have array validate params");
90 96
91 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? 97 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy?
92 for (size_t i = 0; i < input.size(); ++i) 98 for (size_t i = 0; i < input.size(); ++i)
93 output->at(i) = input[i]; 99 output->at(i) = input[i];
94 } 100 }
95 static void DeserializeElements(Array_Data<bool>* input, 101 static void DeserializeElements(Array_Data<bool>* input,
96 Array<bool>* output) { 102 Array<bool>* output) {
97 Array<bool> result(input->size()); 103 Array<bool> result(input->size());
98 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? 104 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy?
99 for (size_t i = 0; i < input->size(); ++i) 105 for (size_t i = 0; i < input->size(); ++i)
100 result.at(i) = input->at(i); 106 result.at(i) = input->at(i);
101 output->Swap(&result); 107 output->Swap(&result);
102 } 108 }
103 }; 109 };
104 110
111 // Serializes and deserializes arrays of handles.
105 template <typename H> 112 template <typename H>
106 struct ArraySerializer<ScopedHandleBase<H>, H, true> { 113 struct ArraySerializer<ScopedHandleBase<H>, H, true, false> {
107 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { 114 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) {
108 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); 115 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H));
109 } 116 }
110 template <bool element_is_nullable, typename ElementValidateParams> 117 template <bool element_is_nullable, typename ElementValidateParams>
111 static void SerializeElements(Array<ScopedHandleBase<H>> input, 118 static void SerializeElements(Array<ScopedHandleBase<H>> input,
112 Buffer* buf, 119 Buffer* buf,
113 Array_Data<H>* output) { 120 Array_Data<H>* output) {
114 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), 121 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value),
115 "Handle type should not have array validate params"); 122 "Handle type should not have array validate params");
116 123
(...skipping 18 matching lines...) Expand all
135 }; 142 };
136 143
137 // This template must only apply to pointer mojo entity (structs and arrays). 144 // This template must only apply to pointer mojo entity (structs and arrays).
138 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. 145 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer.
139 template <typename S> 146 template <typename S>
140 struct ArraySerializer<S, 147 struct ArraySerializer<S,
141 typename internal::EnableIf< 148 typename internal::EnableIf<
142 internal::IsPointer<typename internal::WrapperTraits< 149 internal::IsPointer<typename internal::WrapperTraits<
143 S>::DataType>::value, 150 S>::DataType>::value,
144 typename internal::WrapperTraits<S>::DataType>::type, 151 typename internal::WrapperTraits<S>::DataType>::type,
145 true> { 152 true,
153 false> {
146 typedef typename internal::RemovePointer< 154 typedef typename internal::RemovePointer<
147 typename internal::WrapperTraits<S>::DataType>::type S_Data; 155 typename internal::WrapperTraits<S>::DataType>::type S_Data;
148 static size_t GetSerializedSize(const Array<S>& input) { 156 static size_t GetSerializedSize(const Array<S>& input) {
149 size_t size = sizeof(Array_Data<S_Data*>) + 157 size_t size = sizeof(Array_Data<S_Data*>) +
150 input.size() * sizeof(internal::StructPointer<S_Data>); 158 input.size() * sizeof(internal::StructPointer<S_Data>);
151 for (size_t i = 0; i < input.size(); ++i) 159 for (size_t i = 0; i < input.size(); ++i)
152 size += GetSerializedSize_(input[i]); 160 size += GetSerializedSize_(input[i]);
153 return size; 161 return size;
154 } 162 }
155 template <bool element_is_nullable, typename ElementValidateParams> 163 template <bool element_is_nullable, typename ElementValidateParams>
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 template <typename T, typename U, typename Params> 212 template <typename T, typename U, typename Params>
205 struct SerializeCaller<Map<T, U>, Params> { 213 struct SerializeCaller<Map<T, U>, Params> {
206 static void Run(Map<T, U> input, 214 static void Run(Map<T, U> input,
207 Buffer* buf, 215 Buffer* buf,
208 typename Map<T, U>::Data_** output) { 216 typename Map<T, U>::Data_** output) {
209 SerializeMap_<Params>(input.Pass(), buf, output); 217 SerializeMap_<Params>(input.Pass(), buf, output);
210 } 218 }
211 }; 219 };
212 }; 220 };
213 221
222 // Handles serialization and deserialization of arrays of unions.
223 template <typename U, typename U_Data>
224 struct ArraySerializer<U, U_Data, true, true> {
225 static size_t GetSerializedSize(const Array<U>& input) {
226 size_t size = sizeof(Array_Data<U_Data>);
viettrungluu 2015/03/04 20:58:13 I don't understand why this doesn't account for th
azani 2015/03/05 23:59:25 See lower.
227 for (size_t i = 0; i < input.size(); ++i) {
viettrungluu 2015/03/04 20:58:13 nit: Other one-line for statements in this file do
azani 2015/03/05 23:59:25 Done.
228 size += GetSerializedSize_(input[i]);
viettrungluu 2015/03/04 20:58:13 (I suppose might include the inline part, but that
azani 2015/03/05 23:59:25 It does. I've added a comment to clarify.
229 }
230 return size;
231 }
232
233 template <bool element_is_nullable, typename ElementValidateParams>
234 static void SerializeElements(Array<U> input,
235 Buffer* buf,
236 Array_Data<U_Data>* output) {
237 for (size_t i = 0; i < input.size(); ++i) {
238 U_Data* result = output->storage() + i;
239 SerializeUnion_(input[i].Pass(), buf, &result, true);
240 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
241 !element_is_nullable && output->at(i).is_null(),
242 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
243 MakeMessageWithArrayIndex("null in array expecting valid unions",
244 input.size(), i));
245 }
246 }
247
248 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) {
249 Array<U> result(input->size());
250 for (size_t i = 0; i < input->size(); ++i) {
251 U element;
252 Deserialize_(&input->at(i), &element);
viettrungluu 2015/03/04 20:58:13 Why don't you just do &result[i], instead of going
azani 2015/03/05 23:59:25 Done.
253 result[i] = element.Pass();
254 }
255 output->Swap(&result);
256 }
257 };
258
259 // Handles serialization and deserialization of arrays of strings.
214 template <> 260 template <>
215 struct ArraySerializer<String, String_Data*, false> { 261 struct ArraySerializer<String, String_Data*, false> {
216 static size_t GetSerializedSize(const Array<String>& input) { 262 static size_t GetSerializedSize(const Array<String>& input) {
217 size_t size = sizeof(Array_Data<String_Data*>) + 263 size_t size = sizeof(Array_Data<String_Data*>) +
218 input.size() * sizeof(internal::StringPointer); 264 input.size() * sizeof(internal::StringPointer);
219 for (size_t i = 0; i < input.size(); ++i) 265 for (size_t i = 0; i < input.size(); ++i)
220 size += GetSerializedSize_(input[i]); 266 size += GetSerializedSize_(input[i]);
221 return size; 267 return size;
222 } 268 }
223 template <bool element_is_nullable, typename ElementValidateParams> 269 template <bool element_is_nullable, typename ElementValidateParams>
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 if (input) { 338 if (input) {
293 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 339 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
294 } else { 340 } else {
295 output->reset(); 341 output->reset();
296 } 342 }
297 } 343 }
298 344
299 } // namespace mojo 345 } // namespace mojo
300 346
301 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 347 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698