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 | 10 |
10 #include "mojo/public/cpp/bindings/array.h" | 11 #include "mojo/public/cpp/bindings/array.h" |
11 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 12 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
12 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
13 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" | 14 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
14 #include "mojo/public/cpp/bindings/map.h" | 15 #include "mojo/public/cpp/bindings/map.h" |
15 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 namespace internal { | 18 namespace internal { |
18 | 19 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 }; | 78 }; |
78 | 79 |
79 template <typename Key, typename Value, typename MaybeConstUserType> | 80 template <typename Key, typename Value, typename MaybeConstUserType> |
80 struct Serializer<Map<Key, Value>, MaybeConstUserType> { | 81 struct Serializer<Map<Key, Value>, MaybeConstUserType> { |
81 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 82 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
82 using Traits = MapTraits<UserType>; | 83 using Traits = MapTraits<UserType>; |
83 using UserKey = typename Traits::Key; | 84 using UserKey = typename Traits::Key; |
84 using UserValue = typename Traits::Value; | 85 using UserValue = typename Traits::Value; |
85 using Data = typename Map<Key, Value>::Data_; | 86 using Data = typename Map<Key, Value>::Data_; |
86 using KeyArraySerializer = ArraySerializer<Array<Key>, | 87 using KeyArraySerializer = ArraySerializer<Array<Key>, |
87 Array<UserKey>, | 88 std::vector<UserKey>, |
88 MapKeyReader<MaybeConstUserType>>; | 89 MapKeyReader<MaybeConstUserType>>; |
89 using ValueArraySerializer = | 90 using ValueArraySerializer = |
90 ArraySerializer<Array<Value>, | 91 ArraySerializer<Array<Value>, |
91 Array<UserValue>, | 92 std::vector<UserValue>, |
92 MapValueReader<MaybeConstUserType>>; | 93 MapValueReader<MaybeConstUserType>>; |
93 | 94 |
94 static size_t PrepareToSerialize(MaybeConstUserType& input, | 95 static size_t PrepareToSerialize(MaybeConstUserType& input, |
95 SerializationContext* context) { | 96 SerializationContext* context) { |
96 if (CallIsNullIfExists<Traits>(input)) | 97 if (CallIsNullIfExists<Traits>(input)) |
97 return 0; | 98 return 0; |
98 | 99 |
99 size_t struct_overhead = sizeof(Data); | 100 size_t struct_overhead = sizeof(Data); |
100 MapKeyReader<MaybeConstUserType> key_reader(input); | 101 MapKeyReader<MaybeConstUserType> key_reader(input); |
101 size_t keys_size = | 102 size_t keys_size = |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 144 } |
144 *output = result; | 145 *output = result; |
145 } | 146 } |
146 | 147 |
147 static bool Deserialize(Data* input, | 148 static bool Deserialize(Data* input, |
148 UserType* output, | 149 UserType* output, |
149 SerializationContext* context) { | 150 SerializationContext* context) { |
150 if (!input) | 151 if (!input) |
151 return CallSetToNullIfExists<Traits>(output); | 152 return CallSetToNullIfExists<Traits>(output); |
152 | 153 |
153 Array<UserKey> keys; | 154 std::vector<UserKey> keys; |
154 Array<UserValue> values; | 155 std::vector<UserValue> values; |
155 | 156 |
156 if (!KeyArraySerializer::DeserializeElements(input->keys.ptr, &keys, | 157 if (!KeyArraySerializer::DeserializeElements(input->keys.ptr, &keys, |
157 context) || | 158 context) || |
158 !ValueArraySerializer::DeserializeElements(input->values.ptr, &values, | 159 !ValueArraySerializer::DeserializeElements(input->values.ptr, &values, |
159 context)) { | 160 context)) { |
160 return false; | 161 return false; |
161 } | 162 } |
162 | 163 |
163 DCHECK_EQ(keys.size(), values.size()); | 164 DCHECK_EQ(keys.size(), values.size()); |
164 size_t size = keys.size(); | 165 size_t size = keys.size(); |
165 Traits::SetToEmpty(output); | 166 Traits::SetToEmpty(output); |
166 | 167 |
167 for (size_t i = 0; i < size; ++i) | 168 for (size_t i = 0; i < size; ++i) |
168 Traits::Insert(*output, std::move(keys[i]), std::move(values[i])); | 169 Traits::Insert(*output, std::move(keys[i]), std::move(values[i])); |
169 return true; | 170 return true; |
170 } | 171 } |
171 }; | 172 }; |
172 | 173 |
173 } // namespace internal | 174 } // namespace internal |
174 } // namespace mojo | 175 } // namespace mojo |
175 | 176 |
176 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 177 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
OLD | NEW |