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

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

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to ToT; fixes clang-format bustage. Created 6 years, 2 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
7
8 #include "mojo/public/cpp/bindings/lib/array_internal.h"
9 #include "mojo/public/cpp/bindings/lib/map_data_internal.h"
10 #include "mojo/public/cpp/bindings/lib/map_internal.h"
11 #include "mojo/public/cpp/bindings/lib/string_serialization.h"
12 #include "mojo/public/cpp/bindings/map.h"
13
14 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_(Array<E> input, internal::Buffer* buf,
21 internal::Array_Data<F>** output);
22
23 namespace internal {
24
25 template <typename MapType, typename DataType,
26 bool kValueIsMoveOnlyType = IsMoveOnlyType<MapType>::value>
27 struct MapSerializer;
28
29 template <typename MapType, typename DataType>
30 struct MapSerializer<MapType, DataType, false> {
31 static size_t GetHeaderSize() {
32 return sizeof(Array_Data<DataType>);
33 }
34 static size_t GetBaseArraySize(size_t count) {
35 return Align(count * sizeof(DataType));
36 }
37 static size_t GetItemSize(const MapType& item) {
38 return 0;
39 }
40 };
41
42 template <>
43 struct MapSerializer<bool, bool, false> {
44 static size_t GetHeaderSize() {
45 return sizeof(Array_Data<bool>);
46 }
47 static size_t GetBaseArraySize(size_t count) {
48 return Align((count + 7) / 8);
49 }
50 static size_t GetItemSize(bool item) {
51 return 0;
52 }
53 };
54
55 template <typename H>
56 struct MapSerializer<ScopedHandleBase<H>, H, true> {
57 static size_t GetHeaderSize() {
58 return sizeof(Array_Data<H>);
59 }
60 static size_t GetBaseArraySize(size_t count) {
61 return Align(count * sizeof(H));
62 }
63 static size_t GetItemSize(const H& item) {
64 return 0;
65 }
66 };
67
68 template <typename S>
69 struct MapSerializer<S, typename S::Data_*, true> {
70 static size_t GetHeaderSize() {
71 return sizeof(Array_Data<typename S::Data_*>);
72 }
73 static size_t GetBaseArraySize(size_t count) {
74 return count * sizeof(internal::StructPointer<typename S::Data_>);
75 }
76 static size_t GetItemSize(const S& item) {
77 return GetSerializedSize_(item);
78 }
79 };
80
81 template <>
82 struct MapSerializer<String, String_Data*, false> {
83 static size_t GetHeaderSize() {
84 return sizeof(Array_Data<String_Data*>);
85 }
86 static size_t GetBaseArraySize(size_t count) {
87 return count * sizeof(internal::StringPointer);
88 }
89 static size_t GetItemSize(const String& item) {
90 return GetSerializedSize_(item);
91 }
92 };
93
94 template <typename MapKey, typename MapValue,
95 typename DataKey, typename DataValue>
96 struct SizeAccumulator {
97 SizeAccumulator(size_t* data_size) : size(data_size) {}
98 size_t* size;
99
100 void operator()(const MapKey& key, const MapValue& value) {
101 *size += internal::MapSerializer<MapKey, DataKey>::GetItemSize(key);
102 *size += internal::MapSerializer<MapValue, DataValue>::GetItemSize(value);
103 }
104 };
105
106 template <typename MapKey>
107 struct MapKeyValidateParams {
108 public:
109 typedef NoValidateParams ElementValidateParams;
110 static const uint32_t expected_num_elements = 0;
111 static const bool element_is_nullable = false;
112 };
113
114 template <> struct MapKeyValidateParams<String> {
115 public:
116 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams;
117 static const uint32_t expected_num_elements = 0;
118 static const bool element_is_nullable = false;
119 };
120
121 } // namespace internal
122
123 // TODO(erg): This can't go away yet. We still need to calculate out the size
124 // of a struct header, and two arrays.
125 template <typename MapKey, typename MapValue>
126 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) {
127 if (!input)
128 return 0;
129 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey;
130 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue;
131
132 size_t count = input.size();
133 size_t struct_overhead = sizeof(mojo::internal::Map_Data<DataKey, DataValue>);
134 size_t key_base_size =
135 internal::MapSerializer<MapKey, DataKey>::GetHeaderSize() +
136 internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count);
137 size_t value_base_size =
138 internal::MapSerializer<MapValue, DataValue>::GetHeaderSize() +
139 internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count);
140
141 size_t key_data_size = 0;
142 size_t value_data_size =0;
143 for (auto it = input.begin(); it != input.end(); ++it) {
144 key_data_size +=
145 internal::MapSerializer<MapKey, DataKey>::GetItemSize(
146 it.GetKey());
147 value_data_size +=
148 internal::MapSerializer<MapValue, DataValue>::GetItemSize(
149 it.GetValue());
150 }
151
152 return struct_overhead + key_base_size + key_data_size +
153 // SOME SORT OF ALIGNMENT GOES HERE?
154 value_base_size + value_data_size;
155 }
156
157 // We don't need a KeyValidateParams, because we konw exactly what params are
158 // needed. (Keys are primitive types or non-nullable strings.)
159 template <typename ValueValidateParams,
160 typename KeyWrapperType,
161 typename ValueWrapperType,
162 typename KeySerializationType,
163 typename ValueSerializationType>
164 inline void SerializeMap_(
165 Map<KeyWrapperType, ValueWrapperType> input,
166 internal::Buffer* buf,
167 internal::Map_Data<KeySerializationType, ValueSerializationType>** output) {
168 if (input) {
169 internal::Map_Data<KeySerializationType, ValueSerializationType>* result =
170 internal::Map_Data<KeySerializationType, ValueSerializationType>::New(
171 buf);
172 if (result) {
173 Array<KeyWrapperType> keys;
174 Array<ValueWrapperType> values;
175 input.DecomposeMapTo(&keys, &values);
176 SerializeArray_<internal::MapKeyValidateParams<KeyWrapperType>>(
177 keys.Pass(), buf, &result->first.ptr);
178 SerializeArray_<ValueValidateParams>(values.Pass(), buf,
179 &result->second.ptr);
180 }
181 *output = result;
182 } else {
183 *output = nullptr;
184 }
185 }
186
187 template <typename KeyWrapperType,
188 typename ValueWrapperType,
189 typename KeySerializationType,
190 typename ValueSerializationType>
191 inline void Deserialize_(
192 internal::Map_Data<KeySerializationType, ValueSerializationType>* input,
193 Map<KeyWrapperType, ValueWrapperType>* output) {
194 if (input) {
195 Array<KeyWrapperType> keys;
196 Array<ValueWrapperType> values;
197
198 Deserialize_(input->first.ptr, &keys);
199 Deserialize_(input->second.ptr, &values);
200
201 *output = Map<KeyWrapperType, ValueWrapperType>(keys.Pass(), values.Pass());
202 } else {
203 output->reset();
204 }
205 }
206
207 } // namespace mojo
208
209 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698