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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 } | 141 } |
141 *output = result; | 142 *output = result; |
142 } | 143 } |
143 | 144 |
144 static bool Deserialize(Data* input, | 145 static bool Deserialize(Data* input, |
145 UserType* output, | 146 UserType* output, |
146 SerializationContext* context) { | 147 SerializationContext* context) { |
147 if (!input) | 148 if (!input) |
148 return CallSetToNullIfExists<Traits>(output); | 149 return CallSetToNullIfExists<Traits>(output); |
149 | 150 |
150 Array<UserKey> keys; | 151 std::vector<UserKey> keys; |
151 Array<UserValue> values; | 152 std::vector<UserValue> values; |
152 | 153 |
153 if (!KeyArraySerializer::DeserializeElements(input->keys.ptr, &keys, | 154 if (!KeyArraySerializer::DeserializeElements(input->keys.ptr, &keys, |
154 context) || | 155 context) || |
155 !ValueArraySerializer::DeserializeElements(input->values.ptr, &values, | 156 !ValueArraySerializer::DeserializeElements(input->values.ptr, &values, |
156 context)) { | 157 context)) { |
157 return false; | 158 return false; |
158 } | 159 } |
159 | 160 |
160 DCHECK_EQ(keys.size(), values.size()); | 161 DCHECK_EQ(keys.size(), values.size()); |
161 size_t size = keys.size(); | 162 size_t size = keys.size(); |
162 Traits::SetToEmpty(output); | 163 Traits::SetToEmpty(output); |
163 | 164 |
164 for (size_t i = 0; i < size; ++i) | 165 for (size_t i = 0; i < size; ++i) { |
165 Traits::Insert(*output, std::move(keys[i]), std::move(values[i])); | 166 if (!Traits::Insert(*output, std::move(keys[i]), std::move(values[i]))) |
| 167 return false; |
| 168 } |
166 return true; | 169 return true; |
167 } | 170 } |
168 }; | 171 }; |
169 | 172 |
170 } // namespace internal | 173 } // namespace internal |
171 } // namespace mojo | 174 } // namespace mojo |
172 | 175 |
173 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 176 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
OLD | NEW |