| 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_MAP_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <type_traits> | 8 #include <type_traits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "mojo/public/cpp/bindings/array.h" | 11 #include "mojo/public/cpp/bindings/array_data_view.h" |
| 12 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 12 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
| 13 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
| 14 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" | 14 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
| 15 #include "mojo/public/cpp/bindings/map.h" | 15 #include "mojo/public/cpp/bindings/map_data_view.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 namespace internal { | 18 namespace internal { |
| 19 | 19 |
| 20 template <typename MaybeConstUserType> | 20 template <typename MaybeConstUserType> |
| 21 class MapReaderBase { | 21 class MapReaderBase { |
| 22 public: | 22 public: |
| 23 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 23 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 24 using Traits = MapTraits<UserType>; | 24 using Traits = MapTraits<UserType>; |
| 25 using MaybeConstIterator = | 25 using MaybeConstIterator = |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 using GetNextResult = | 74 using GetNextResult = |
| 75 decltype(Traits::GetValue(std::declval<MaybeConstIterator&>())); | 75 decltype(Traits::GetValue(std::declval<MaybeConstIterator&>())); |
| 76 GetNextResult GetNext() { | 76 GetNextResult GetNext() { |
| 77 GetNextResult value = Traits::GetValue(this->iter_); | 77 GetNextResult value = Traits::GetValue(this->iter_); |
| 78 Traits::AdvanceIterator(this->iter_); | 78 Traits::AdvanceIterator(this->iter_); |
| 79 return value; | 79 return value; |
| 80 } | 80 } |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 template <typename Key, typename Value, typename MaybeConstUserType> | 83 template <typename Key, typename Value, typename MaybeConstUserType> |
| 84 struct Serializer<Map<Key, Value>, MaybeConstUserType> { | 84 struct Serializer<MapDataView<Key, Value>, MaybeConstUserType> { |
| 85 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 85 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 86 using Traits = MapTraits<UserType>; | 86 using Traits = MapTraits<UserType>; |
| 87 using UserKey = typename Traits::Key; | 87 using UserKey = typename Traits::Key; |
| 88 using UserValue = typename Traits::Value; | 88 using UserValue = typename Traits::Value; |
| 89 using Data = typename MojomTypeTraits<Map<Key, Value>>::Data; | 89 using Data = typename MojomTypeTraits<MapDataView<Key, Value>>::Data; |
| 90 using KeyArraySerializer = ArraySerializer<Array<Key>, | 90 using KeyArraySerializer = ArraySerializer<ArrayDataView<Key>, |
| 91 std::vector<UserKey>, | 91 std::vector<UserKey>, |
| 92 MapKeyReader<MaybeConstUserType>>; | 92 MapKeyReader<MaybeConstUserType>>; |
| 93 using ValueArraySerializer = | 93 using ValueArraySerializer = |
| 94 ArraySerializer<Array<Value>, | 94 ArraySerializer<ArrayDataView<Value>, |
| 95 std::vector<UserValue>, | 95 std::vector<UserValue>, |
| 96 MapValueReader<MaybeConstUserType>>; | 96 MapValueReader<MaybeConstUserType>>; |
| 97 | 97 |
| 98 static size_t PrepareToSerialize(MaybeConstUserType& input, | 98 static size_t PrepareToSerialize(MaybeConstUserType& input, |
| 99 SerializationContext* context) { | 99 SerializationContext* context) { |
| 100 if (CallIsNullIfExists<Traits>(input)) | 100 if (CallIsNullIfExists<Traits>(input)) |
| 101 return 0; | 101 return 0; |
| 102 | 102 |
| 103 size_t struct_overhead = sizeof(Data); | 103 size_t struct_overhead = sizeof(Data); |
| 104 MapKeyReader<MaybeConstUserType> key_reader(input); | 104 MapKeyReader<MaybeConstUserType> key_reader(input); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 118 SerializationContext* context) { | 118 SerializationContext* context) { |
| 119 DCHECK(validate_params->key_validate_params); | 119 DCHECK(validate_params->key_validate_params); |
| 120 DCHECK(validate_params->element_validate_params); | 120 DCHECK(validate_params->element_validate_params); |
| 121 if (CallIsNullIfExists<Traits>(input)) { | 121 if (CallIsNullIfExists<Traits>(input)) { |
| 122 *output = nullptr; | 122 *output = nullptr; |
| 123 return; | 123 return; |
| 124 } | 124 } |
| 125 | 125 |
| 126 auto result = Data::New(buf); | 126 auto result = Data::New(buf); |
| 127 if (result) { | 127 if (result) { |
| 128 auto keys_ptr = | 128 auto keys_ptr = MojomTypeTraits<ArrayDataView<Key>>::Data::New( |
| 129 MojomTypeTraits<Array<Key>>::Data::New(Traits::GetSize(input), buf); | 129 Traits::GetSize(input), buf); |
| 130 if (keys_ptr) { | 130 if (keys_ptr) { |
| 131 MapKeyReader<MaybeConstUserType> key_reader(input); | 131 MapKeyReader<MaybeConstUserType> key_reader(input); |
| 132 KeyArraySerializer::SerializeElements( | 132 KeyArraySerializer::SerializeElements( |
| 133 &key_reader, buf, keys_ptr, validate_params->key_validate_params, | 133 &key_reader, buf, keys_ptr, validate_params->key_validate_params, |
| 134 context); | 134 context); |
| 135 result->keys.Set(keys_ptr); | 135 result->keys.Set(keys_ptr); |
| 136 } | 136 } |
| 137 | 137 |
| 138 auto values_ptr = | 138 auto values_ptr = MojomTypeTraits<ArrayDataView<Value>>::Data::New( |
| 139 MojomTypeTraits<Array<Value>>::Data::New(Traits::GetSize(input), buf); | 139 Traits::GetSize(input), buf); |
| 140 if (values_ptr) { | 140 if (values_ptr) { |
| 141 MapValueReader<MaybeConstUserType> value_reader(input); | 141 MapValueReader<MaybeConstUserType> value_reader(input); |
| 142 ValueArraySerializer::SerializeElements( | 142 ValueArraySerializer::SerializeElements( |
| 143 &value_reader, buf, values_ptr, | 143 &value_reader, buf, values_ptr, |
| 144 validate_params->element_validate_params, context); | 144 validate_params->element_validate_params, context); |
| 145 result->values.Set(values_ptr); | 145 result->values.Set(values_ptr); |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 *output = result; | 148 *output = result; |
| 149 } | 149 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 173 return false; | 173 return false; |
| 174 } | 174 } |
| 175 return true; | 175 return true; |
| 176 } | 176 } |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 } // namespace internal | 179 } // namespace internal |
| 180 } // namespace mojo | 180 } // namespace mojo |
| 181 | 181 |
| 182 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 182 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| OLD | NEW |