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

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

Issue 622593002: mojo: Allow circular dependencies between structs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase 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
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/bindings_internal.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_ARRAY_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
7 7
8 #include <string.h> // For |memcpy()|. 8 #include <string.h> // For |memcpy()|.
9 9
10 #include <vector> 10 #include <vector>
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 125 }
126 static void DeserializeElements(Array_Data<H>* input, 126 static void DeserializeElements(Array_Data<H>* input,
127 Array<ScopedHandleBase<H>>* output) { 127 Array<ScopedHandleBase<H>>* output) {
128 Array<ScopedHandleBase<H>> result(input->size()); 128 Array<ScopedHandleBase<H>> result(input->size());
129 for (size_t i = 0; i < input->size(); ++i) 129 for (size_t i = 0; i < input->size(); ++i)
130 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); 130 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i)));
131 output->Swap(&result); 131 output->Swap(&result);
132 } 132 }
133 }; 133 };
134 134
135 // This template must only apply to pointer mojo entity (structs and arrays).
136 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer.
135 template <typename S> 137 template <typename S>
136 struct ArraySerializer<S, typename S::Data_*, true> { 138 struct ArraySerializer<S,
139 typename internal::EnableIf<
140 internal::IsPointer<typename internal::WrapperTraits<
141 S>::DataType>::value,
142 typename internal::WrapperTraits<S>::DataType>::type,
143 true> {
144 typedef typename internal::RemovePointer<
145 typename internal::WrapperTraits<S>::DataType>::type S_Data;
137 static size_t GetSerializedSize(const Array<S>& input) { 146 static size_t GetSerializedSize(const Array<S>& input) {
138 size_t size = 147 size_t size = sizeof(Array_Data<S_Data*>) +
139 sizeof(Array_Data<typename S::Data_*>) + 148 input.size() * sizeof(internal::StructPointer<S_Data>);
140 input.size() * sizeof(internal::StructPointer<typename S::Data_>);
141 for (size_t i = 0; i < input.size(); ++i) 149 for (size_t i = 0; i < input.size(); ++i)
142 size += GetSerializedSize_(input[i]); 150 size += GetSerializedSize_(input[i]);
143 return size; 151 return size;
144 } 152 }
145 template <bool element_is_nullable, typename ElementValidateParams> 153 template <bool element_is_nullable, typename ElementValidateParams>
146 static void SerializeElements(Array<S> input, 154 static void SerializeElements(Array<S> input,
147 Buffer* buf, 155 Buffer* buf,
148 Array_Data<typename S::Data_*>* output) { 156 Array_Data<S_Data*>* output) {
149 for (size_t i = 0; i < input.size(); ++i) { 157 for (size_t i = 0; i < input.size(); ++i) {
150 typename S::Data_* element; 158 S_Data* element;
151 SerializeCaller<S, ElementValidateParams>::Run( 159 SerializeCaller<S, ElementValidateParams>::Run(
152 input[i].Pass(), buf, &element); 160 input[i].Pass(), buf, &element);
153 output->at(i) = element; 161 output->at(i) = element;
154 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 162 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
155 !element_is_nullable && !element, 163 !element_is_nullable && !element,
156 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 164 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
157 MakeMessageWithArrayIndex( 165 MakeMessageWithArrayIndex(
158 "null in array expecting valid pointers", input.size(), i)); 166 "null in array expecting valid pointers", input.size(), i));
159 } 167 }
160 } 168 }
161 static void DeserializeElements(Array_Data<typename S::Data_*>* input, 169 static void DeserializeElements(Array_Data<S_Data*>* input,
162 Array<S>* output) { 170 Array<S>* output) {
163 Array<S> result(input->size()); 171 Array<S> result(input->size());
164 for (size_t i = 0; i < input->size(); ++i) { 172 for (size_t i = 0; i < input->size(); ++i) {
165 S element; 173 S element;
166 Deserialize_(input->at(i), &element); 174 Deserialize_(input->at(i), &element);
167 result[i] = element.Pass(); 175 result[i] = element.Pass();
168 } 176 }
169 output->Swap(&result); 177 output->Swap(&result);
170 } 178 }
171 179
172 private: 180 private:
173 template <typename T, typename Params> 181 template <typename T, typename Params>
174 struct SerializeCaller { 182 struct SerializeCaller {
175 static void Run(T input, Buffer* buf, typename T::Data_** output) { 183 static void Run(T input,
184 Buffer* buf,
185 typename internal::WrapperTraits<T>::DataType* output) {
176 static_assert((IsSame<Params, NoValidateParams>::value), 186 static_assert((IsSame<Params, NoValidateParams>::value),
177 "Struct type should not have array validate params"); 187 "Struct type should not have array validate params");
178 188
179 Serialize_(input.Pass(), buf, output); 189 Serialize_(input.Pass(), buf, output);
180 } 190 }
181 }; 191 };
182 192
183 template <typename T, typename Params> 193 template <typename T, typename Params>
184 struct SerializeCaller<Array<T>, Params> { 194 struct SerializeCaller<Array<T>, Params> {
185 static void Run(Array<T> input, 195 static void Run(Array<T> input,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 if (input) { 290 if (input) {
281 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 291 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
282 } else { 292 } else {
283 output->reset(); 293 output->reset();
284 } 294 }
285 } 295 }
286 296
287 } // namespace mojo 297 } // namespace mojo
288 298
289 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 299 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/bindings_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698