| 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 #include <type_traits> | 9 #include <type_traits> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "mojo/public/c/system/macros.h" | 12 #include "mojo/public/c/system/macros.h" |
| 13 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 14 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 14 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 15 #include "mojo/public/cpp/bindings/lib/iterator_util.h" | 15 #include "mojo/public/cpp/bindings/lib/iterator_util.h" |
| 16 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" | 16 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
| 17 #include "mojo/public/cpp/bindings/lib/map_serialization_forward.h" | 17 #include "mojo/public/cpp/bindings/lib/map_serialization_forward.h" |
| 18 #include "mojo/public/cpp/bindings/lib/string_serialization.h" | 18 #include "mojo/public/cpp/bindings/lib/string_serialization.h" |
| 19 #include "mojo/public/cpp/bindings/lib/template_util.h" | |
| 20 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 19 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 21 | 20 |
| 22 namespace mojo { | 21 namespace mojo { |
| 23 namespace internal { | 22 namespace internal { |
| 24 | 23 |
| 25 // The ArraySerializer template contains static methods for serializing |Array|s | 24 // The ArraySerializer template contains static methods for serializing |Array|s |
| 26 // of various types. These methods include: | 25 // of various types. These methods include: |
| 27 // * size_t GetSerializedSize(..) | 26 // * size_t GetSerializedSize(..) |
| 28 // Computes the size of the serialized version of the |Array|. | 27 // Computes the size of the serialized version of the |Array|. |
| 29 // * void SerializeElements(..) | 28 // * void SerializeElements(..) |
| 30 // Takes an |Iterator| and a size and serializes it. | 29 // Takes an |Iterator| and a size and serializes it. |
| 31 // * void DeserializeElements(..) | 30 // * void DeserializeElements(..) |
| 32 // Takes a pointer to an |Array_Data| and deserializes it into a given | 31 // Takes a pointer to an |Array_Data| and deserializes it into a given |
| 33 // |Array|. | 32 // |Array|. |
| 34 // | 33 // |
| 35 // Note: The enable template parameter exists only to allow partial | 34 // Note: The enable template parameter exists only to allow partial |
| 36 // specializations to disable instantiation using logic based on E and F. | 35 // specializations to disable instantiation using logic based on E and F. |
| 37 // By default, assuming that there are no other substitution failures, the | 36 // By default, assuming that there are no other substitution failures, the |
| 38 // specialization will instantiate and needs to take no action. A partial | 37 // specialization will instantiate and needs to take no action. A partial |
| 39 // specialization of the form | 38 // specialization of the form |
| 40 // | 39 // |
| 41 // template<E, F> struct ArraySerialzer<E, F> | 40 // template<E, F> struct ArraySerialzer<E, F> |
| 42 // | 41 // |
| 43 // may be limited to values of E and F with particular properties by supplying | 42 // may be limited to values of E and F with particular properties by supplying |
| 44 // an expression for enable which will cause substitution failure if the | 43 // an expression for enable which will cause substitution failure if the |
| 45 // properties of E and F do not satisfy the expression. | 44 // properties of E and F do not satisfy the expression. |
| 46 template <typename E, | 45 template <typename E, |
| 47 typename F, | 46 typename F, |
| 48 bool is_union = | 47 bool is_union = |
| 49 IsUnionDataType<typename RemovePointer<F>::type>::value, | 48 IsUnionDataType<typename std::remove_pointer<F>::type>::value, |
| 50 typename enable = void> | 49 typename enable = void> |
| 51 struct ArraySerializer; | 50 struct ArraySerializer; |
| 52 | 51 |
| 53 // Handles serialization and deserialization of arrays of pod types. | 52 // Handles serialization and deserialization of arrays of pod types. |
| 54 template <typename E, typename F> | 53 template <typename E, typename F> |
| 55 struct ArraySerializer< | 54 struct ArraySerializer< |
| 56 E, | 55 E, |
| 57 F, | 56 F, |
| 58 false, | 57 false, |
| 59 typename std::enable_if<std::is_convertible<E, F>::value || | 58 typename std::enable_if<std::is_convertible<E, F>::value || |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 output->Swap(&result); | 184 output->Swap(&result); |
| 186 } | 185 } |
| 187 }; | 186 }; |
| 188 | 187 |
| 189 // This template must only apply to pointer mojo entity (structs, arrays, | 188 // This template must only apply to pointer mojo entity (structs, arrays, |
| 190 // strings). This is done by ensuring that WrapperTraits<S>::DataType is a | 189 // strings). This is done by ensuring that WrapperTraits<S>::DataType is a |
| 191 // pointer. | 190 // pointer. |
| 192 template <typename S> | 191 template <typename S> |
| 193 struct ArraySerializer< | 192 struct ArraySerializer< |
| 194 S, | 193 S, |
| 195 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, | 194 typename std::enable_if< |
| 196 typename WrapperTraits<S>::DataType>::type, | 195 std::is_pointer<typename WrapperTraits<S>::DataType>::value, |
| 196 typename WrapperTraits<S>::DataType>::type, |
| 197 false> { | 197 false> { |
| 198 typedef | 198 typedef |
| 199 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; | 199 typename std::remove_pointer<typename WrapperTraits<S>::DataType>::type |
| 200 S_Data; |
| 200 static size_t GetSerializedSize(const Array<S>& input) { | 201 static size_t GetSerializedSize(const Array<S>& input) { |
| 201 size_t size = sizeof(Array_Data<S_Data*>) + | 202 size_t size = sizeof(Array_Data<S_Data*>) + |
| 202 input.size() * sizeof(StructPointer<S_Data>); | 203 input.size() * sizeof(StructPointer<S_Data>); |
| 203 for (size_t i = 0; i < input.size(); ++i) | 204 for (size_t i = 0; i < input.size(); ++i) |
| 204 size += GetSerializedSize_(*(UnwrapConstStructPtr<S>::value(input[i]))); | 205 size += GetSerializedSize_(*(UnwrapConstStructPtr<S>::value(input[i]))); |
| 205 return size; | 206 return size; |
| 206 } | 207 } |
| 207 | 208 |
| 208 template <typename Iterator> | 209 template <typename Iterator> |
| 209 static ValidationError SerializeElements( | 210 static ValidationError SerializeElements( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 243 } |
| 243 | 244 |
| 244 private: | 245 private: |
| 245 // SerializeCaller template is used by |ArraySerializer| to dispatch a | 246 // SerializeCaller template is used by |ArraySerializer| to dispatch a |
| 246 // serialize call on a non-POD type. This template is defined outside | 247 // serialize call on a non-POD type. This template is defined outside |
| 247 // |ArraySerializer| since you cannot specialize a struct within a class | 248 // |ArraySerializer| since you cannot specialize a struct within a class |
| 248 // definition. | 249 // definition. |
| 249 struct SerializeCaller { | 250 struct SerializeCaller { |
| 250 // This template needs to be suppressed if |T| is |String|, otherwise it | 251 // This template needs to be suppressed if |T| is |String|, otherwise it |
| 251 // takes precedence over the |String|-overloaded Run() below. | 252 // takes precedence over the |String|-overloaded Run() below. |
| 252 template <typename T, | 253 template < |
| 253 typename = typename EnableIf<!IsSame<T, String>::value, T>::type> | 254 typename T, |
| 255 typename = |
| 256 typename std::enable_if<!std::is_same<T, String>::value, T>::type> |
| 254 static ValidationError Run(T* input, | 257 static ValidationError Run(T* input, |
| 255 Buffer* buf, | 258 Buffer* buf, |
| 256 typename WrapperTraits<T>::DataType* output, | 259 typename WrapperTraits<T>::DataType* output, |
| 257 const ArrayValidateParams* validate_params) { | 260 const ArrayValidateParams* validate_params) { |
| 258 MOJO_DCHECK(!validate_params) | 261 MOJO_DCHECK(!validate_params) |
| 259 << "Struct type should not have array validate params"; | 262 << "Struct type should not have array validate params"; |
| 260 return Serialize_(UnwrapStructPtr<T>::value(*input), buf, output); | 263 return Serialize_(UnwrapStructPtr<T>::value(*input), buf, output); |
| 261 } | 264 } |
| 262 | 265 |
| 263 static ValidationError Run(const String* input, | 266 static ValidationError Run(const String* input, |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 if (input) { | 426 if (input) { |
| 424 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 427 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 425 } else { | 428 } else { |
| 426 output->reset(); | 429 output->reset(); |
| 427 } | 430 } |
| 428 } | 431 } |
| 429 | 432 |
| 430 } // namespace mojo | 433 } // namespace mojo |
| 431 | 434 |
| 432 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 435 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |