Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 = internal::IsWrappedUnionPtr<E>::value> | |
|
yzshen1
2015/02/25 21:07:00
nit: internal:: is not needed.
azani
2015/03/03 00:44:15
Done.
| |
| 49 struct ArraySerializer; | 52 struct ArraySerializer; |
| 50 | 53 |
| 51 template <typename E, typename F> | 54 template <typename E, typename F> |
| 52 struct ArraySerializer<E, F, false> { | 55 struct ArraySerializer<E, F, false, false> { |
| 53 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); | 56 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); |
| 54 static size_t GetSerializedSize(const Array<E>& input) { | 57 static size_t GetSerializedSize(const Array<E>& input) { |
| 55 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); | 58 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); |
| 56 } | 59 } |
| 57 template <bool element_is_nullable, typename ElementValidateParams> | 60 template <bool element_is_nullable, typename ElementValidateParams> |
| 58 static void SerializeElements(Array<E> input, | 61 static void SerializeElements(Array<E> input, |
| 59 Buffer* buf, | 62 Buffer* buf, |
| 60 Array_Data<F>* output) { | 63 Array_Data<F>* output) { |
| 61 static_assert(!element_is_nullable, | 64 static_assert(!element_is_nullable, |
| 62 "Primitive type should be non-nullable"); | 65 "Primitive type should be non-nullable"); |
| 63 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), | 66 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), |
| 64 "Primitive type should not have array validate params"); | 67 "Primitive type should not have array validate params"); |
| 65 | 68 |
| 66 if (input.size()) | 69 if (input.size()) |
| 67 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); | 70 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); |
| 68 } | 71 } |
| 69 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { | 72 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { |
| 70 std::vector<E> result(input->size()); | 73 std::vector<E> result(input->size()); |
| 71 if (input->size()) | 74 if (input->size()) |
| 72 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); | 75 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); |
| 73 output->Swap(&result); | 76 output->Swap(&result); |
| 74 } | 77 } |
| 75 }; | 78 }; |
| 76 | 79 |
| 77 template <> | 80 template <> |
| 78 struct ArraySerializer<bool, bool, false> { | 81 struct ArraySerializer<bool, bool, false, false> { |
| 79 static size_t GetSerializedSize(const Array<bool>& input) { | 82 static size_t GetSerializedSize(const Array<bool>& input) { |
| 80 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); | 83 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); |
| 81 } | 84 } |
| 82 template <bool element_is_nullable, typename ElementValidateParams> | 85 template <bool element_is_nullable, typename ElementValidateParams> |
| 83 static void SerializeElements(Array<bool> input, | 86 static void SerializeElements(Array<bool> input, |
| 84 Buffer* buf, | 87 Buffer* buf, |
| 85 Array_Data<bool>* output) { | 88 Array_Data<bool>* output) { |
| 86 static_assert(!element_is_nullable, | 89 static_assert(!element_is_nullable, |
| 87 "Primitive type should be non-nullable"); | 90 "Primitive type should be non-nullable"); |
| 88 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), | 91 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), |
| 89 "Primitive type should not have array validate params"); | 92 "Primitive type should not have array validate params"); |
| 90 | 93 |
| 91 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 94 // 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) | 95 for (size_t i = 0; i < input.size(); ++i) |
| 93 output->at(i) = input[i]; | 96 output->at(i) = input[i]; |
| 94 } | 97 } |
| 95 static void DeserializeElements(Array_Data<bool>* input, | 98 static void DeserializeElements(Array_Data<bool>* input, |
| 96 Array<bool>* output) { | 99 Array<bool>* output) { |
| 97 Array<bool> result(input->size()); | 100 Array<bool> result(input->size()); |
| 98 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 101 // 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) | 102 for (size_t i = 0; i < input->size(); ++i) |
| 100 result.at(i) = input->at(i); | 103 result.at(i) = input->at(i); |
| 101 output->Swap(&result); | 104 output->Swap(&result); |
| 102 } | 105 } |
| 103 }; | 106 }; |
| 104 | 107 |
| 105 template <typename H> | 108 template <typename H> |
| 106 struct ArraySerializer<ScopedHandleBase<H>, H, true> { | 109 struct ArraySerializer<ScopedHandleBase<H>, H, true, false> { |
| 107 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { | 110 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { |
| 108 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); | 111 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); |
| 109 } | 112 } |
| 110 template <bool element_is_nullable, typename ElementValidateParams> | 113 template <bool element_is_nullable, typename ElementValidateParams> |
| 111 static void SerializeElements(Array<ScopedHandleBase<H>> input, | 114 static void SerializeElements(Array<ScopedHandleBase<H>> input, |
| 112 Buffer* buf, | 115 Buffer* buf, |
| 113 Array_Data<H>* output) { | 116 Array_Data<H>* output) { |
| 114 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), | 117 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), |
| 115 "Handle type should not have array validate params"); | 118 "Handle type should not have array validate params"); |
| 116 | 119 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 135 }; | 138 }; |
| 136 | 139 |
| 137 // This template must only apply to pointer mojo entity (structs and arrays). | 140 // 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. | 141 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. |
| 139 template <typename S> | 142 template <typename S> |
| 140 struct ArraySerializer<S, | 143 struct ArraySerializer<S, |
| 141 typename internal::EnableIf< | 144 typename internal::EnableIf< |
| 142 internal::IsPointer<typename internal::WrapperTraits< | 145 internal::IsPointer<typename internal::WrapperTraits< |
| 143 S>::DataType>::value, | 146 S>::DataType>::value, |
| 144 typename internal::WrapperTraits<S>::DataType>::type, | 147 typename internal::WrapperTraits<S>::DataType>::type, |
| 145 true> { | 148 true, |
| 149 false> { | |
| 146 typedef typename internal::RemovePointer< | 150 typedef typename internal::RemovePointer< |
| 147 typename internal::WrapperTraits<S>::DataType>::type S_Data; | 151 typename internal::WrapperTraits<S>::DataType>::type S_Data; |
| 148 static size_t GetSerializedSize(const Array<S>& input) { | 152 static size_t GetSerializedSize(const Array<S>& input) { |
| 149 size_t size = sizeof(Array_Data<S_Data*>) + | 153 size_t size = sizeof(Array_Data<S_Data*>) + |
| 150 input.size() * sizeof(internal::StructPointer<S_Data>); | 154 input.size() * sizeof(internal::StructPointer<S_Data>); |
| 151 for (size_t i = 0; i < input.size(); ++i) | 155 for (size_t i = 0; i < input.size(); ++i) |
| 152 size += GetSerializedSize_(input[i]); | 156 size += GetSerializedSize_(input[i]); |
| 153 return size; | 157 return size; |
| 154 } | 158 } |
| 155 template <bool element_is_nullable, typename ElementValidateParams> | 159 template <bool element_is_nullable, typename ElementValidateParams> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 template <typename T, typename U, typename Params> | 208 template <typename T, typename U, typename Params> |
| 205 struct SerializeCaller<Map<T, U>, Params> { | 209 struct SerializeCaller<Map<T, U>, Params> { |
| 206 static void Run(Map<T, U> input, | 210 static void Run(Map<T, U> input, |
| 207 Buffer* buf, | 211 Buffer* buf, |
| 208 typename Map<T, U>::Data_** output) { | 212 typename Map<T, U>::Data_** output) { |
| 209 SerializeMap_<Params>(input.Pass(), buf, output); | 213 SerializeMap_<Params>(input.Pass(), buf, output); |
| 210 } | 214 } |
| 211 }; | 215 }; |
| 212 }; | 216 }; |
| 213 | 217 |
| 218 template <typename U, typename U_Data> | |
|
yzshen1
2015/02/25 21:06:59
nit: Please comment about what is this specializat
azani
2015/03/03 00:44:15
Done.
| |
| 219 struct ArraySerializer<U, U_Data, true, true> { | |
| 220 static size_t GetSerializedSize(const Array<U>& input) { | |
| 221 size_t size = sizeof(Array_Data<U_Data>); | |
| 222 for (size_t i = 0; i < input.size(); ++i) { | |
| 223 size += GetSerializedSize_(input[i]); | |
| 224 } | |
| 225 return size; | |
| 226 } | |
| 227 | |
| 228 template <bool element_is_nullable, typename ElementValidateParams> | |
| 229 static void SerializeElements(Array<U> input, | |
| 230 Buffer* buf, | |
| 231 Array_Data<U_Data>* output) { | |
| 232 for (size_t i = 0; i < input.size(); ++i) { | |
| 233 U_Data* result = output->storage() + i; | |
| 234 Serialize_(input[i].Pass(), buf, &result); | |
|
yzshen1
2015/02/25 21:07:00
I think maybe we should make a different Serialize
azani
2015/03/03 00:44:15
Done.
| |
| 235 } | |
|
yzshen1
2015/02/25 21:07:00
Please look at line 168: we need to do similar thi
azani
2015/03/03 00:44:15
Done.
| |
| 236 } | |
| 237 | |
| 238 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { | |
| 239 Array<U> result(input->size()); | |
| 240 for (size_t i = 0; i < input->size(); ++i) { | |
| 241 U element; | |
| 242 Deserialize_(&input->at(i), &element); | |
| 243 result[i] = element.Pass(); | |
| 244 } | |
| 245 output->Swap(&result); | |
| 246 } | |
| 247 }; | |
| 248 /* | |
|
yzshen1
2015/02/25 21:07:00
If something is not useful, please remove it inste
azani
2015/03/03 00:44:15
Done.
| |
| 249 template <typename U> | |
| 250 struct ArraySerializer<U, | |
| 251 typename internal::EnableIf< | |
| 252 internal::IsWrappedUnionPtr<U>::value, | |
| 253 typename internal::WrapperTraits<U>::DataType>::type, | |
| 254 true> { | |
| 255 static const int i = 10; | |
| 256 static size_t GetSerializedSize(const Array<U>& input) { | |
| 257 return 0; | |
| 258 } | |
| 259 }; | |
| 260 */ | |
| 261 | |
| 214 template <> | 262 template <> |
| 215 struct ArraySerializer<String, String_Data*, false> { | 263 struct ArraySerializer<String, String_Data*, false> { |
| 216 static size_t GetSerializedSize(const Array<String>& input) { | 264 static size_t GetSerializedSize(const Array<String>& input) { |
| 217 size_t size = sizeof(Array_Data<String_Data*>) + | 265 size_t size = sizeof(Array_Data<String_Data*>) + |
| 218 input.size() * sizeof(internal::StringPointer); | 266 input.size() * sizeof(internal::StringPointer); |
| 219 for (size_t i = 0; i < input.size(); ++i) | 267 for (size_t i = 0; i < input.size(); ++i) |
| 220 size += GetSerializedSize_(input[i]); | 268 size += GetSerializedSize_(input[i]); |
| 221 return size; | 269 return size; |
| 222 } | 270 } |
| 223 template <bool element_is_nullable, typename ElementValidateParams> | 271 template <bool element_is_nullable, typename ElementValidateParams> |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 if (input) { | 340 if (input) { |
| 293 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 341 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 294 } else { | 342 } else { |
| 295 output->reset(); | 343 output->reset(); |
| 296 } | 344 } |
| 297 } | 345 } |
| 298 | 346 |
| 299 } // namespace mojo | 347 } // namespace mojo |
| 300 | 348 |
| 301 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 349 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |