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

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: 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
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 111 }
112 static void DeserializeElements( 112 static void DeserializeElements(
113 Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) { 113 Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) {
114 Array<ScopedHandleBase<H> > result(input->size()); 114 Array<ScopedHandleBase<H> > result(input->size());
115 for (size_t i = 0; i < input->size(); ++i) 115 for (size_t i = 0; i < input->size(); ++i)
116 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); 116 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i)));
117 output->Swap(&result); 117 output->Swap(&result);
118 } 118 }
119 }; 119 };
120 120
121 template <typename S> struct ArraySerializer<S, typename S::Data_*, true> { 121 template <typename S>
122 struct ArraySerializer<S,
123 typename internal::WrapperTraits<S>::PlainDataType*,
viettrungluu 2014/10/01 17:18:23 internal::WrapperTraits<S>::PlainDataType* is just
qsr 2014/10/06 08:20:26 enable_if, as well as remove_pointer are in type_t
124 true> {
125 typedef typename internal::WrapperTraits<S>::PlainDataType F;
viettrungluu 2014/10/01 17:18:23 nit: A more descriptive name (even "S_Data") than
qsr 2014/10/06 08:20:26 Done.
122 static size_t GetSerializedSize(const Array<S>& input) { 126 static size_t GetSerializedSize(const Array<S>& input) {
123 size_t size = sizeof(Array_Data<typename S::Data_*>) + 127 size_t size = sizeof(Array_Data<F*>) +
124 input.size() * sizeof(internal::StructPointer<typename S::Data_>); 128 input.size() * sizeof(internal::StructPointer<F>);
125 for (size_t i = 0; i < input.size(); ++i) 129 for (size_t i = 0; i < input.size(); ++i)
126 size += GetSerializedSize_(input[i]); 130 size += GetSerializedSize_(input[i]);
127 return size; 131 return size;
128 } 132 }
129 template <bool element_is_nullable, typename ElementValidateParams> 133 template <bool element_is_nullable, typename ElementValidateParams>
130 static void SerializeElements(Array<S> input, 134 static void SerializeElements(Array<S> input,
131 Buffer* buf, 135 Buffer* buf,
132 Array_Data<typename S::Data_*>* output) { 136 Array_Data<F*>* output) {
133 for (size_t i = 0; i < input.size(); ++i) { 137 for (size_t i = 0; i < input.size(); ++i) {
134 typename S::Data_* element; 138 F* element;
135 SerializeCaller<S, ElementValidateParams>::Run( 139 SerializeCaller<S, ElementValidateParams>::Run(
136 input[i].Pass(), buf, &element); 140 input[i].Pass(), buf, &element);
137 output->at(i) = element; 141 output->at(i) = element;
138 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 142 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
139 !element_is_nullable && !element, 143 !element_is_nullable && !element,
140 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 144 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
141 MakeMessageWithArrayIndex( 145 MakeMessageWithArrayIndex(
142 "null in array expecting valid pointers", input.size(), i)); 146 "null in array expecting valid pointers", input.size(), i));
143 } 147 }
144 } 148 }
145 static void DeserializeElements( 149 static void DeserializeElements(
146 Array_Data<typename S::Data_*>* input, Array<S>* output) { 150 Array_Data<F*>* input, Array<S>* output) {
147 Array<S> result(input->size()); 151 Array<S> result(input->size());
148 for (size_t i = 0; i < input->size(); ++i) { 152 for (size_t i = 0; i < input->size(); ++i) {
149 S element; 153 S element;
150 Deserialize_(input->at(i), &element); 154 Deserialize_(input->at(i), &element);
151 result[i] = element.Pass(); 155 result[i] = element.Pass();
152 } 156 }
153 output->Swap(&result); 157 output->Swap(&result);
154 } 158 }
155 159
156 private: 160 private:
157 template <typename T, typename Params> 161 template <typename T, typename Params>
158 struct SerializeCaller { 162 struct SerializeCaller {
159 static void Run(T input, Buffer* buf, typename T::Data_** output) { 163 static void Run(
164 T input,
165 Buffer* buf,
166 typename internal::WrapperTraits<T>::PlainDataType** output) {
160 MOJO_COMPILE_ASSERT((IsSame<Params, NoValidateParams>::value), 167 MOJO_COMPILE_ASSERT((IsSame<Params, NoValidateParams>::value),
161 Struct_type_should_not_have_array_validate_params); 168 Struct_type_should_not_have_array_validate_params);
162 169
163 Serialize_(input.Pass(), buf, output); 170 Serialize_(input.Pass(), buf, output);
164 } 171 }
165 }; 172 };
166 173
167 template <typename T, typename Params> 174 template <typename T, typename Params>
168 struct SerializeCaller<Array<T>, Params> { 175 struct SerializeCaller<Array<T>, Params> {
169 static void Run(Array<T> input, 176 static void Run(Array<T> input,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 if (input) { 260 if (input) {
254 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 261 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
255 } else { 262 } else {
256 output->reset(); 263 output->reset();
257 } 264 }
258 } 265 }
259 266
260 } // namespace mojo 267 } // namespace mojo
261 268
262 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 269 #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') | mojo/public/cpp/bindings/lib/bindings_internal.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698