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