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

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: Suppress pylint warnings instead of making MapKind public. 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,
21 internal::Buffer* buf,
22 internal::Array_Data<F>** output);
23
24 namespace internal {
25
26 template <typename MapType,
27 typename DataType,
28 bool kValueIsMoveOnlyType = IsMoveOnlyType<MapType>::value>
29 struct MapSerializer;
30
31 template <typename MapType, typename DataType>
32 struct MapSerializer<MapType, DataType, false> {
33 static size_t GetHeaderSize() { return sizeof(Array_Data<DataType>); }
yzshen1 2014/10/08 21:58:33 - Comments about the methods of MapSerializer woul
34 static size_t GetBaseArraySize(size_t count) {
35 return Align(count * sizeof(DataType));
36 }
37 static size_t GetItemSize(const MapType& item) { return 0; }
38 };
39
40 template <>
41 struct MapSerializer<bool, bool, false> {
42 static size_t GetHeaderSize() { return sizeof(Array_Data<bool>); }
43 static size_t GetBaseArraySize(size_t count) {
44 return Align((count + 7) / 8);
45 }
46 static size_t GetItemSize(bool item) { return 0; }
47 };
48
49 template <typename H>
50 struct MapSerializer<ScopedHandleBase<H>, H, true> {
51 static size_t GetHeaderSize() { return sizeof(Array_Data<H>); }
52 static size_t GetBaseArraySize(size_t count) {
53 return Align(count * sizeof(H));
54 }
55 static size_t GetItemSize(const H& item) { return 0; }
56 };
57
58 template <typename S>
59 struct MapSerializer<S, typename S::Data_*, true> {
60 static size_t GetHeaderSize() {
61 return sizeof(Array_Data<typename S::Data_*>);
62 }
63 static size_t GetBaseArraySize(size_t count) {
64 return count * sizeof(internal::StructPointer<typename S::Data_>);
65 }
66 static size_t GetItemSize(const S& item) { return GetSerializedSize_(item); }
67 };
68
69 template <>
70 struct MapSerializer<String, String_Data*, false> {
71 static size_t GetHeaderSize() { return sizeof(Array_Data<String_Data*>); }
72 static size_t GetBaseArraySize(size_t count) {
73 return count * sizeof(internal::StringPointer);
74 }
75 static size_t GetItemSize(const String& item) {
76 return GetSerializedSize_(item);
77 }
78 };
79
80 template <typename MapKey,
81 typename MapValue,
82 typename DataKey,
83 typename DataValue>
84 struct SizeAccumulator {
85 SizeAccumulator(size_t* data_size) : size(data_size) {}
86 size_t* size;
87
88 void operator()(const MapKey& key, const MapValue& value) {
89 *size += internal::MapSerializer<MapKey, DataKey>::GetItemSize(key);
90 *size += internal::MapSerializer<MapValue, DataValue>::GetItemSize(value);
91 }
92 };
93
94 template <typename MapKey>
95 struct MapKeyValidateParams {
yzshen1 2014/10/08 21:58:33 I think you have defined these types in map_data_i
Elliot Glaysher 2014/10/08 23:40:49 The ones in map_data_internal.h are different then
96 public:
97 typedef NoValidateParams ElementValidateParams;
98 static const uint32_t expected_num_elements = 0;
99 static const bool element_is_nullable = false;
100 };
101
102 template <>
103 struct MapKeyValidateParams<String> {
104 public:
105 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams;
106 static const uint32_t expected_num_elements = 0;
107 static const bool element_is_nullable = false;
108 };
109
110 } // namespace internal
111
112 // TODO(erg): This can't go away yet. We still need to calculate out the size
113 // of a struct header, and two arrays.
114 template <typename MapKey, typename MapValue>
115 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) {
116 if (!input)
117 return 0;
118 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey;
119 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue;
120
121 size_t count = input.size();
122 size_t struct_overhead = sizeof(mojo::internal::Map_Data<DataKey, DataValue>);
123 size_t key_base_size =
124 internal::MapSerializer<MapKey, DataKey>::GetHeaderSize() +
125 internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count);
126 size_t value_base_size =
127 internal::MapSerializer<MapValue, DataValue>::GetHeaderSize() +
128 internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count);
129
130 size_t key_data_size = 0;
131 size_t value_data_size = 0;
132 for (auto it = input.begin(); it != input.end(); ++it) {
133 key_data_size +=
134 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey());
135 value_data_size +=
136 internal::MapSerializer<MapValue, DataValue>::GetItemSize(
137 it.GetValue());
138 }
139
140 return struct_overhead + key_base_size + key_data_size +
141 // SOME SORT OF ALIGNMENT GOES HERE?
yzshen1 2014/10/08 21:58:33 I think you have considered all alignment properly
142 value_base_size + value_data_size;
143 }
144
145 // We don't need a KeyValidateParams, because we konw exactly what params are
146 // needed. (Keys are primitive types or non-nullable strings.)
147 template <typename ValueValidateParams,
148 typename KeyWrapperType,
yzshen1 2014/10/08 21:58:33 nit: it might be better to use consistent names fo
149 typename ValueWrapperType,
150 typename KeySerializationType,
151 typename ValueSerializationType>
152 inline void SerializeMap_(
153 Map<KeyWrapperType, ValueWrapperType> input,
154 internal::Buffer* buf,
155 internal::Map_Data<KeySerializationType, ValueSerializationType>** output) {
156 if (input) {
157 internal::Map_Data<KeySerializationType, ValueSerializationType>* result =
158 internal::Map_Data<KeySerializationType, ValueSerializationType>::New(
159 buf);
160 if (result) {
161 Array<KeyWrapperType> keys;
162 Array<ValueWrapperType> values;
163 input.DecomposeMapTo(&keys, &values);
164 SerializeArray_<internal::MapKeyValidateParams<KeyWrapperType>>(
165 keys.Pass(), buf, &result->first.ptr);
166 SerializeArray_<ValueValidateParams>(
167 values.Pass(), buf, &result->second.ptr);
168 }
169 *output = result;
170 } else {
171 *output = nullptr;
172 }
173 }
174
175 template <typename KeyWrapperType,
176 typename ValueWrapperType,
177 typename KeySerializationType,
178 typename ValueSerializationType>
179 inline void Deserialize_(
180 internal::Map_Data<KeySerializationType, ValueSerializationType>* input,
181 Map<KeyWrapperType, ValueWrapperType>* output) {
182 if (input) {
183 Array<KeyWrapperType> keys;
184 Array<ValueWrapperType> values;
185
186 Deserialize_(input->first.ptr, &keys);
187 Deserialize_(input->second.ptr, &values);
188
189 *output = Map<KeyWrapperType, ValueWrapperType>(keys.Pass(), values.Pass());
190 } else {
191 output->reset();
192 }
193 }
194
195 } // namespace mojo
196
197 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698