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

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: Moved test classes to their own shared file. 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 GetBaseArraySize(size_t count) {
34 return Align(count * sizeof(DataType));
35 }
36 static size_t GetItemSize(const MapType& item) { return 0; }
37 };
38
39 template <>
40 struct MapSerializer<bool, bool, false> {
41 static size_t GetBaseArraySize(size_t count) {
42 return Align((count + 7) / 8);
43 }
44 static size_t GetItemSize(bool item) { return 0; }
45 };
46
47 template <typename H>
48 struct MapSerializer<ScopedHandleBase<H>, H, true> {
49 static size_t GetBaseArraySize(size_t count) {
50 return Align(count * sizeof(H));
51 }
52 static size_t GetItemSize(const H& item) { return 0; }
53 };
54
55 template <typename S>
56 struct MapSerializer<S, typename S::Data_*, true> {
57 static size_t GetBaseArraySize(size_t count) {
58 return count * sizeof(internal::StructPointer<typename S::Data_>);
59 }
60 static size_t GetItemSize(const S& item) { return GetSerializedSize_(item); }
61 };
62
63 template <>
64 struct MapSerializer<String, String_Data*, false> {
65 static size_t GetBaseArraySize(size_t count) {
66 return count * sizeof(internal::StringPointer);
67 }
68 static size_t GetItemSize(const String& item) {
69 return GetSerializedSize_(item);
70 }
71 };
72
73 } // namespace internal
74
75 // TODO(erg): This can't go away yet. We still need to calculate out the size
76 // of a struct header, and two arrays.
77 template <typename MapKey, typename MapValue>
78 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) {
79 if (!input)
80 return 0;
81 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey;
82 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue;
83
84 size_t count = input.size();
85 size_t struct_overhead = sizeof(mojo::internal::Map_Data<DataKey, DataValue>);
86 size_t key_base_size =
87 sizeof(internal::ArrayHeader) +
88 internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count);
89 size_t value_base_size =
90 sizeof(internal::ArrayHeader) +
91 internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count);
92
93 size_t key_data_size = 0;
94 size_t value_data_size = 0;
95 for (auto it = input.begin(); it != input.end(); ++it) {
96 key_data_size +=
97 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey());
98 value_data_size +=
99 internal::MapSerializer<MapValue, DataValue>::GetItemSize(
100 it.GetValue());
101 }
102
103 return struct_overhead + key_base_size + key_data_size + value_base_size +
104 value_data_size;
105 }
106
107 // We don't need a KeyValidateParams, because we konw exactly what params are
108 // needed. (Keys are primitive types or non-nullable strings.)
109 template <typename ValueValidateParams,
110 typename MapKey,
111 typename MapValue,
112 typename DataKey,
113 typename DataValue>
114 inline void SerializeMap_(Map<MapKey, MapValue> input,
115 internal::Buffer* buf,
116 internal::Map_Data<DataKey, DataValue>** output) {
117 if (input) {
118 internal::Map_Data<DataKey, DataValue>* result =
119 internal::Map_Data<DataKey, DataValue>::New(buf);
120 if (result) {
121 Array<MapKey> keys;
122 Array<MapValue> values;
123 input.DecomposeMapTo(&keys, &values);
124 SerializeArray_<internal::MapKeyValidateParams<DataKey>>(
125 keys.Pass(), buf, &result->keys.ptr);
126 SerializeArray_<ValueValidateParams>(
127 values.Pass(), buf, &result->values.ptr);
128 }
129 *output = result;
130 } else {
131 *output = nullptr;
132 }
133 }
134
135 template <typename MapKey,
136 typename MapValue,
137 typename DataKey,
138 typename DataValue>
139 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input,
140 Map<MapKey, MapValue>* output) {
141 if (input) {
142 Array<MapKey> keys;
143 Array<MapValue> values;
144
145 Deserialize_(input->keys.ptr, &keys);
146 Deserialize_(input->values.ptr, &values);
147
148 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass());
149 } else {
150 output->reset();
151 }
152 }
153
154 } // namespace mojo
155
156 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_internal.h ('k') | mojo/public/cpp/bindings/lib/validate_params.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698