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" |
9 #include "mojo/public/cpp/bindings/lib/map_internal.h" | 10 #include "mojo/public/cpp/bindings/lib/map_internal.h" |
10 | 11 |
11 namespace mojo { | 12 namespace mojo { |
12 namespace internal { | 13 namespace internal { |
13 | 14 |
14 template <typename MapType, typename DataType, | 15 template <typename MapType, typename DataType, |
15 bool kValueIsMoveOnlyType = IsMoveOnlyType<MapType>::value> | 16 bool kValueIsMoveOnlyType = IsMoveOnlyType<MapType>::value> |
16 struct MapSerializer; | 17 struct MapSerializer; |
17 | 18 |
18 template <typename MapType, typename DataType> | 19 template <typename MapType, typename DataType> |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 struct SizeAccumulator { | 86 struct SizeAccumulator { |
86 SizeAccumulator(size_t* data_size) : size(data_size) {} | 87 SizeAccumulator(size_t* data_size) : size(data_size) {} |
87 size_t* size; | 88 size_t* size; |
88 | 89 |
89 void operator()(const MapKey& key, const MapValue& value) { | 90 void operator()(const MapKey& key, const MapValue& value) { |
90 *size += internal::MapSerializer<MapKey, DataKey>::GetItemSize(key); | 91 *size += internal::MapSerializer<MapKey, DataKey>::GetItemSize(key); |
91 *size += internal::MapSerializer<MapValue, DataValue>::GetItemSize(value); | 92 *size += internal::MapSerializer<MapValue, DataValue>::GetItemSize(value); |
92 } | 93 } |
93 }; | 94 }; |
94 | 95 |
| 96 |
| 97 template <typename MapKey> |
| 98 struct MapKeyValidateParams { |
| 99 public: |
| 100 typedef NoValidateParams ElementValidateParams; |
| 101 static const uint32_t expected_num_elements = 0; |
| 102 static const bool element_is_nullable = false; |
| 103 }; |
| 104 |
| 105 |
| 106 template <> struct MapKeyValidateParams<String> { |
| 107 public: |
| 108 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams; |
| 109 static const uint32_t expected_num_elements = 0; |
| 110 static const bool element_is_nullable = false; |
| 111 }; |
| 112 |
95 } // namespace internal | 113 } // namespace internal |
96 | 114 |
97 template <typename MapKey, typename MapValue> | 115 template <typename MapKey, typename MapValue> |
98 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) { | 116 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) { |
99 if (!input) | 117 if (!input) |
100 return 0; | 118 return 0; |
101 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey; | 119 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey; |
102 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue; | 120 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue; |
103 | 121 |
104 size_t count = input.size(); | 122 size_t count = input.size(); |
(...skipping 13 matching lines...) Expand all Loading... |
118 value_data_size += | 136 value_data_size += |
119 internal::MapSerializer<MapValue, DataValue>::GetItemSize( | 137 internal::MapSerializer<MapValue, DataValue>::GetItemSize( |
120 it.GetValue()); | 138 it.GetValue()); |
121 } | 139 } |
122 | 140 |
123 return key_base_size + key_data_size + | 141 return key_base_size + key_data_size + |
124 // SOME SORT OF ALIGNMENT GOES HERE? | 142 // SOME SORT OF ALIGNMENT GOES HERE? |
125 value_base_size + value_data_size; | 143 value_base_size + value_data_size; |
126 } | 144 } |
127 | 145 |
| 146 |
| 147 // We don't need a KeyValidateParams, because we konw exactly what params are |
| 148 // needed. (Keys are primitive types or non-nullable strings.) |
| 149 template <typename ValueValidateParams, |
| 150 typename KeyWrapperType, |
| 151 typename ValueWrapperType, |
| 152 typename KeySerializationType, |
| 153 typename ValueSerializationType> |
| 154 inline void SerializeMap_( |
| 155 Map<KeyWrapperType, ValueWrapperType> input, |
| 156 internal::Buffer* buf, |
| 157 internal::MapPointerPair<KeySerializationType, |
| 158 ValueSerializationType>* output) { |
| 159 if (input) { |
| 160 Array<KeyWrapperType> keys; |
| 161 Array<ValueWrapperType> values; |
| 162 input.DecomposeMapTo(&keys, &values); |
| 163 SerializeArray_<internal::MapKeyValidateParams<KeyWrapperType>>( |
| 164 keys.Pass(), buf, &output->keys.ptr); |
| 165 SerializeArray_<ValueValidateParams>(values.Pass(), buf, |
| 166 &output->values.ptr); |
| 167 } else { |
| 168 output->keys.ptr = nullptr; |
| 169 output->values.ptr = nullptr; |
| 170 } |
| 171 } |
| 172 |
| 173 template <typename KeyWrapperType, |
| 174 typename ValueWrapperType, |
| 175 typename KeySerializationType, |
| 176 typename ValueSerializationType> |
| 177 inline void Deserialize_( |
| 178 internal::MapPointerPair<KeySerializationType, |
| 179 ValueSerializationType>* input, |
| 180 Map<KeyWrapperType, ValueWrapperType>* output) { |
| 181 if (input) { |
| 182 Array<KeyWrapperType> keys; |
| 183 Array<ValueWrapperType> values; |
| 184 |
| 185 Deserialize_(input->keys.ptr, &keys); |
| 186 Deserialize_(input->values.ptr, &values); |
| 187 |
| 188 *output = Map<KeyWrapperType, ValueWrapperType>(keys.Pass(), values.Pass()); |
| 189 } else { |
| 190 output->reset(); |
| 191 } |
| 192 } |
| 193 |
128 } // namespace mojo | 194 } // namespace mojo |
129 | 195 |
130 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 196 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
OLD | NEW |