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 "mojo/public/cpp/bindings/lib/array_internal.h" | 8 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
| 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 11 #include "mojo/public/cpp/bindings/lib/iterator_util.h" |
9 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" | 12 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
10 #include "mojo/public/cpp/bindings/lib/map_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/map_internal.h" |
11 #include "mojo/public/cpp/bindings/lib/string_serialization.h" | 14 #include "mojo/public/cpp/bindings/lib/string_serialization.h" |
12 #include "mojo/public/cpp/bindings/map.h" | 15 #include "mojo/public/cpp/bindings/map.h" |
13 | 16 |
14 namespace mojo { | 17 namespace mojo { |
15 | |
16 template <typename Key, typename Value> | |
17 inline size_t GetSerializedSize_(const Map<Key, Value>& input); | |
18 | |
19 template <typename ValidateParams, typename E, typename F> | |
20 inline void SerializeArray_( | |
21 Array<E> input, | |
22 internal::Buffer* buf, | |
23 internal::Array_Data<F>** output, | |
24 const internal::ArrayValidateParams* validate_params); | |
25 | |
26 namespace internal { | 18 namespace internal { |
27 | 19 |
28 template <typename MapType, | 20 template <typename MapType, |
29 typename DataType, | 21 typename DataType, |
30 bool value_is_move_only_type = IsMoveOnlyType<MapType>::value, | 22 bool value_is_move_only_type = IsMoveOnlyType<MapType>::value, |
31 bool is_union = | 23 bool is_union = |
32 IsUnionDataType<typename RemovePointer<DataType>::type>::value> | 24 IsUnionDataType<typename RemovePointer<DataType>::type>::value> |
33 struct MapSerializer; | 25 struct MapSerializer; |
34 | 26 |
35 template <typename MapType, typename DataType> | 27 template <typename MapType, typename DataType> |
(...skipping 27 matching lines...) Expand all Loading... |
63 S, | 55 S, |
64 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, | 56 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, |
65 typename WrapperTraits<S>::DataType>::type, | 57 typename WrapperTraits<S>::DataType>::type, |
66 true, | 58 true, |
67 false> { | 59 false> { |
68 typedef | 60 typedef |
69 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; | 61 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; |
70 static size_t GetBaseArraySize(size_t count) { | 62 static size_t GetBaseArraySize(size_t count) { |
71 return count * sizeof(StructPointer<S_Data>); | 63 return count * sizeof(StructPointer<S_Data>); |
72 } | 64 } |
73 static size_t GetItemSize(const S& item) { return GetSerializedSize_(item); } | 65 static size_t GetItemSize(const S& item) { |
| 66 return GetSerializedSize_(*UnwrapConstStructPtr<S>::value(item)); |
| 67 } |
74 }; | 68 }; |
75 | 69 |
76 template <typename U, typename U_Data> | 70 template <typename U, typename U_Data> |
77 struct MapSerializer<U, U_Data, true, true> { | 71 struct MapSerializer<U, U_Data, true, true> { |
78 static size_t GetBaseArraySize(size_t count) { | 72 static size_t GetBaseArraySize(size_t count) { |
79 return count * sizeof(U_Data); | 73 return count * sizeof(U_Data); |
80 } | 74 } |
81 static size_t GetItemSize(const U& item) { | 75 static size_t GetItemSize(const U& item) { |
82 return GetSerializedSize_(item, true); | 76 return GetSerializedSize_(item, true); |
83 } | 77 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 122 } |
129 | 123 |
130 // We don't need an ArrayValidateParams instance for key validation since | 124 // We don't need an ArrayValidateParams instance for key validation since |
131 // we can deduce it from the Key type. (which can only be primitive types or | 125 // we can deduce it from the Key type. (which can only be primitive types or |
132 // non-nullable strings.) | 126 // non-nullable strings.) |
133 template <typename MapKey, | 127 template <typename MapKey, |
134 typename MapValue, | 128 typename MapValue, |
135 typename DataKey, | 129 typename DataKey, |
136 typename DataValue> | 130 typename DataValue> |
137 inline void SerializeMap_( | 131 inline void SerializeMap_( |
138 Map<MapKey, MapValue> input, | 132 Map<MapKey, MapValue>* input, |
139 internal::Buffer* buf, | 133 internal::Buffer* buf, |
140 internal::Map_Data<DataKey, DataValue>** output, | 134 internal::Map_Data<DataKey, DataValue>** output, |
141 const internal::ArrayValidateParams* value_validate_params) { | 135 const internal::ArrayValidateParams* value_validate_params) { |
142 if (input) { | 136 if (input && *input) { |
143 internal::Map_Data<DataKey, DataValue>* result = | 137 internal::Map_Data<DataKey, DataValue>* result = |
144 internal::Map_Data<DataKey, DataValue>::New(buf); | 138 internal::Map_Data<DataKey, DataValue>::New(buf); |
145 if (result) { | 139 internal::Array_Data<DataKey>* keys_data = |
146 Array<MapKey> keys; | 140 internal::Array_Data<DataKey>::New(input->size(), buf); |
147 Array<MapValue> values; | 141 |
148 input.DecomposeMapTo(&keys, &values); | 142 if (result && keys_data) { |
| 143 result->keys.ptr = keys_data; |
| 144 |
| 145 // We *must* serialize the keys before we allocate an Array_Data for the |
| 146 // values. |
| 147 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); |
149 const internal::ArrayValidateParams* key_validate_params = | 148 const internal::ArrayValidateParams* key_validate_params = |
150 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | 149 internal::MapKeyValidateParamsFactory<DataKey>::Get(); |
151 SerializeArray_(keys.Pass(), buf, &result->keys.ptr, key_validate_params); | 150 |
152 SerializeArray_(values.Pass(), buf, &result->values.ptr, | 151 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( |
153 value_validate_params); | 152 key_iter.begin(), input->size(), buf, result->keys.ptr, |
| 153 key_validate_params); |
| 154 |
| 155 // Now we try allocate an Array_Data for the values |
| 156 internal::Array_Data<DataValue>* values_data = |
| 157 internal::Array_Data<DataValue>::New(input->size(), buf); |
| 158 if (values_data) { |
| 159 result->values.ptr = values_data; |
| 160 internal::MapValueIterator<MapKey, MapValue> value_iter(input); |
| 161 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( |
| 162 value_iter.begin(), input->size(), buf, result->values.ptr, |
| 163 value_validate_params); |
| 164 } |
154 } | 165 } |
155 *output = result; | 166 *output = result; |
156 } else { | 167 } else { |
157 *output = nullptr; | 168 *output = nullptr; |
158 } | 169 } |
159 } | 170 } |
160 | 171 |
161 template <typename MapKey, | 172 template <typename MapKey, |
162 typename MapValue, | 173 typename MapValue, |
163 typename DataKey, | 174 typename DataKey, |
164 typename DataValue> | 175 typename DataValue> |
165 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, | 176 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, |
166 Map<MapKey, MapValue>* output) { | 177 Map<MapKey, MapValue>* output) { |
167 if (input) { | 178 if (input) { |
168 Array<MapKey> keys; | 179 Array<MapKey> keys; |
169 Array<MapValue> values; | 180 Array<MapValue> values; |
170 | 181 |
171 Deserialize_(input->keys.ptr, &keys); | 182 Deserialize_(input->keys.ptr, &keys); |
172 Deserialize_(input->values.ptr, &values); | 183 Deserialize_(input->values.ptr, &values); |
173 | 184 |
174 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); | 185 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); |
175 } else { | 186 } else { |
176 output->reset(); | 187 output->reset(); |
177 } | 188 } |
178 } | 189 } |
179 | 190 |
180 } // namespace mojo | 191 } // namespace mojo |
181 | 192 |
182 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 193 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
OLD | NEW |