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

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

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_internal.h ('k') | mojo/public/cpp/bindings/lib/message.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // This template must only apply to pointer mojo entity (structs and arrays).
56 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer.
57 template <typename S>
58 struct MapSerializer<
59 S,
60 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value,
61 typename WrapperTraits<S>::DataType>::type,
62 true> {
63 typedef
64 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data;
65 static size_t GetBaseArraySize(size_t count) {
66 return count * sizeof(StructPointer<S_Data>);
67 }
68 static size_t GetItemSize(const S& item) { return GetSerializedSize_(item); }
69 };
70
71 template <>
72 struct MapSerializer<String, String_Data*, false> {
73 static size_t GetBaseArraySize(size_t count) {
74 return count * sizeof(StringPointer);
75 }
76 static size_t GetItemSize(const String& item) {
77 return GetSerializedSize_(item);
78 }
79 };
80
81 } // namespace internal
82
83 // TODO(erg): This can't go away yet. We still need to calculate out the size
84 // of a struct header, and two arrays.
85 template <typename MapKey, typename MapValue>
86 inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) {
87 if (!input)
88 return 0;
89 typedef typename internal::WrapperTraits<MapKey>::DataType DataKey;
90 typedef typename internal::WrapperTraits<MapValue>::DataType DataValue;
91
92 size_t count = input.size();
93 size_t struct_overhead = sizeof(mojo::internal::Map_Data<DataKey, DataValue>);
94 size_t key_base_size =
95 sizeof(internal::ArrayHeader) +
96 internal::MapSerializer<MapKey, DataKey>::GetBaseArraySize(count);
97 size_t value_base_size =
98 sizeof(internal::ArrayHeader) +
99 internal::MapSerializer<MapValue, DataValue>::GetBaseArraySize(count);
100
101 size_t key_data_size = 0;
102 size_t value_data_size = 0;
103 for (auto it = input.begin(); it != input.end(); ++it) {
104 key_data_size +=
105 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey());
106 value_data_size +=
107 internal::MapSerializer<MapValue, DataValue>::GetItemSize(
108 it.GetValue());
109 }
110
111 return struct_overhead + key_base_size + key_data_size + value_base_size +
112 value_data_size;
113 }
114
115 // We don't need a KeyValidateParams, because we konw exactly what params are
116 // needed. (Keys are primitive types or non-nullable strings.)
117 template <typename ValueValidateParams,
118 typename MapKey,
119 typename MapValue,
120 typename DataKey,
121 typename DataValue>
122 inline void SerializeMap_(Map<MapKey, MapValue> input,
123 internal::Buffer* buf,
124 internal::Map_Data<DataKey, DataValue>** output) {
125 if (input) {
126 internal::Map_Data<DataKey, DataValue>* result =
127 internal::Map_Data<DataKey, DataValue>::New(buf);
128 if (result) {
129 Array<MapKey> keys;
130 Array<MapValue> values;
131 input.DecomposeMapTo(&keys, &values);
132 SerializeArray_<internal::MapKeyValidateParams<DataKey>>(
133 keys.Pass(), buf, &result->keys.ptr);
134 SerializeArray_<ValueValidateParams>(
135 values.Pass(), buf, &result->values.ptr);
136 }
137 *output = result;
138 } else {
139 *output = nullptr;
140 }
141 }
142
143 template <typename MapKey,
144 typename MapValue,
145 typename DataKey,
146 typename DataValue>
147 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input,
148 Map<MapKey, MapValue>* output) {
149 if (input) {
150 Array<MapKey> keys;
151 Array<MapValue> values;
152
153 Deserialize_(input->keys.ptr, &keys);
154 Deserialize_(input->values.ptr, &values);
155
156 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass());
157 } else {
158 output->reset();
159 }
160 }
161
162 } // namespace mojo
163
164 #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/message.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698