Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(607)

Side by Side Diff: mojo/public/cpp/bindings/lib/map_serialization.h

Issue 1358353002: * Change C++ serialization/deserialization to not be move-only operations (with the except of |Ha… (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Cleanup forward declarations of array/map serialization functions Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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>::
153 value_validate_params); 152 template SerializeElementsWithIterator<
153 typename internal::MapKeyIterator<MapKey, MapValue>::Iterator>(
154 key_iter.begin(), input->size(), buf, result->keys.ptr,
155 key_validate_params);
156
157 // Now we try allocate an Array_Data for the values
158 internal::Array_Data<DataValue>* values_data =
159 internal::Array_Data<DataValue>::New(input->size(), buf);
160 if (values_data) {
161 result->values.ptr = values_data;
162 internal::MapValueIterator<MapKey, MapValue> value_iter(input);
163 internal::ArraySerializer<MapValue, DataValue>::
164 template SerializeElementsWithIterator<
165 typename internal::MapValueIterator<MapKey,
166 MapValue>::Iterator>(
167 value_iter.begin(), input->size(), buf, result->values.ptr,
168 value_validate_params);
169 }
154 } 170 }
155 *output = result; 171 *output = result;
156 } else { 172 } else {
157 *output = nullptr; 173 *output = nullptr;
158 } 174 }
159 } 175 }
160 176
161 template <typename MapKey, 177 template <typename MapKey,
162 typename MapValue, 178 typename MapValue,
163 typename DataKey, 179 typename DataKey,
164 typename DataValue> 180 typename DataValue>
165 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, 181 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input,
166 Map<MapKey, MapValue>* output) { 182 Map<MapKey, MapValue>* output) {
167 if (input) { 183 if (input) {
168 Array<MapKey> keys; 184 Array<MapKey> keys;
169 Array<MapValue> values; 185 Array<MapValue> values;
170 186
171 Deserialize_(input->keys.ptr, &keys); 187 Deserialize_(input->keys.ptr, &keys);
172 Deserialize_(input->values.ptr, &values); 188 Deserialize_(input->values.ptr, &values);
173 189
174 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); 190 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass());
175 } else { 191 } else {
176 output->reset(); 192 output->reset();
177 } 193 }
178 } 194 }
179 195
180 } // namespace mojo 196 } // namespace mojo
181 197
182 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ 198 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698