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

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>
yzshen1 2015/03/26 07:30:14 nit: internal:: is not needed, is it?
azani 2015/03/26 22:27:39 Done.
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>
156 static void SerializeElements(Array<S> input, 164 static void SerializeElements(Array<S> input,
157 Buffer* buf, 165 Buffer* buf,
158 Array_Data<S_Data*>* output) { 166 Array_Data<S_Data*>* output) {
159 for (size_t i = 0; i < input.size(); ++i) { 167 for (size_t i = 0; i < input.size(); ++i) {
160 S_Data* element; 168 S_Data* element;
161 SerializeCaller<S, ElementValidateParams>::Run( 169 SerializeCaller<S, ElementValidateParams>::Run(
162 input[i].Pass(), buf, &element); 170 input[i].Pass(), buf, &element);
163 output->at(i) = element; 171 output->at(i) = element;
164 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 172 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
165 !element_is_nullable && !element, 173 !element_is_nullable && !element,
166 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 174 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
167 MakeMessageWithArrayIndex( 175 MakeMessageWithArrayIndex(
168 "null in array expecting valid pointers", input.size(), i)); 176 "null in array expecting valid pointers", input.size(), i));
169 } 177 }
170 } 178 }
171 static void DeserializeElements(Array_Data<S_Data*>* input, 179 static void DeserializeElements(Array_Data<S_Data*>* input,
172 Array<S>* output) { 180 Array<S>* output) {
173 Array<S> result(input->size()); 181 Array<S> result(input->size());
174 for (size_t i = 0; i < input->size(); ++i) { 182 for (size_t i = 0; i < input->size(); ++i) {
175 S element; 183 Deserialize_(input->at(i), &result[i]);
176 Deserialize_(input->at(i), &element);
177 result[i] = element.Pass();
178 } 184 }
179 output->Swap(&result); 185 output->Swap(&result);
180 } 186 }
181 187
182 private: 188 private:
183 template <typename T, typename Params> 189 template <typename T, typename Params>
184 struct SerializeCaller { 190 struct SerializeCaller {
185 static void Run(T input, 191 static void Run(T input,
186 Buffer* buf, 192 Buffer* buf,
187 typename internal::WrapperTraits<T>::DataType* output) { 193 typename internal::WrapperTraits<T>::DataType* output) {
(...skipping 16 matching lines...) Expand all
204 template <typename T, typename U, typename Params> 210 template <typename T, typename U, typename Params>
205 struct SerializeCaller<Map<T, U>, Params> { 211 struct SerializeCaller<Map<T, U>, Params> {
206 static void Run(Map<T, U> input, 212 static void Run(Map<T, U> input,
207 Buffer* buf, 213 Buffer* buf,
208 typename Map<T, U>::Data_** output) { 214 typename Map<T, U>::Data_** output) {
209 SerializeMap_<Params>(input.Pass(), buf, output); 215 SerializeMap_<Params>(input.Pass(), buf, output);
210 } 216 }
211 }; 217 };
212 }; 218 };
213 219
220 // Handles serialization and deserialization of arrays of unions.
221 template <typename U, typename U_Data>
222 struct ArraySerializer<U, U_Data, true, true> {
223 static size_t GetSerializedSize(const Array<U>& input) {
224 size_t size = sizeof(Array_Data<U_Data>);
225 for (size_t i = 0; i < input.size(); ++i)
yzshen1 2015/03/26 07:30:14 Please add {}. The body of the for-statement are m
azani 2015/03/26 22:27:39 Done.
226 // GetSerializedSize_ will account for both the data in the union and the
227 // space in the array used to hold the union.
228 size += GetSerializedSize_(input[i]);
229 return size;
230 }
231
232 template <bool element_is_nullable, typename ElementValidateParams>
233 static void SerializeElements(Array<U> input,
234 Buffer* buf,
235 Array_Data<U_Data>* output) {
236 for (size_t i = 0; i < input.size(); ++i) {
237 U_Data* result = output->storage() + i;
238 SerializeUnion_(input[i].Pass(), buf, &result, true);
239 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
240 !element_is_nullable && output->at(i).is_null(),
241 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
242 MakeMessageWithArrayIndex("null in array expecting valid unions",
243 input.size(), i));
244 }
245 }
246
247 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) {
248 Array<U> result(input->size());
249 for (size_t i = 0; i < input->size(); ++i) {
250 Deserialize_(&input->at(i), &result[i]);
251 }
252 output->Swap(&result);
253 }
254 };
255
256 // Handles serialization and deserialization of arrays of strings.
214 template <> 257 template <>
215 struct ArraySerializer<String, String_Data*, false> { 258 struct ArraySerializer<String, String_Data*, false> {
216 static size_t GetSerializedSize(const Array<String>& input) { 259 static size_t GetSerializedSize(const Array<String>& input) {
217 size_t size = sizeof(Array_Data<String_Data*>) + 260 size_t size = sizeof(Array_Data<String_Data*>) +
218 input.size() * sizeof(internal::StringPointer); 261 input.size() * sizeof(internal::StringPointer);
219 for (size_t i = 0; i < input.size(); ++i) 262 for (size_t i = 0; i < input.size(); ++i)
220 size += GetSerializedSize_(input[i]); 263 size += GetSerializedSize_(input[i]);
221 return size; 264 return size;
222 } 265 }
223 template <bool element_is_nullable, typename ElementValidateParams> 266 template <bool element_is_nullable, typename ElementValidateParams>
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 if (input) { 335 if (input) {
293 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 336 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
294 } else { 337 } else {
295 output->reset(); 338 output->reset();
296 } 339 }
297 } 340 }
298 341
299 } // namespace mojo 342 } // namespace mojo
300 343
301 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 344 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698